]> granicus.if.org Git - taglib/commitdiff
Expand the internal buffer of ByteVector only if really needed.
authorTsuda Kageyu <tsuda.kageyu@gmail.com>
Thu, 14 May 2015 02:20:35 +0000 (11:20 +0900)
committerTsuda Kageyu <tsuda.kageyu@gmail.com>
Thu, 14 May 2015 02:20:35 +0000 (11:20 +0900)
Add tests for all execution paths of ByteVector::resize().

taglib/toolkit/tbytevector.cpp
tests/test_bytevector.cpp

index bf6c525ff56a60e4a1db4220fe40d2344c12afb8..7aeaea9211f87c246d09c91a2946eaf71e9a6e24 100644 (file)
@@ -702,8 +702,13 @@ ByteVector &ByteVector::resize(uint size, char padding)
 {
   if(size != d->length) {
     detach();
-    d->data->data.resize(d->offset + d->length);
-    d->data->data.resize(d->offset + size, padding);
+
+    if(size > d->data->data.size() - d->offset)
+      d->data->data.resize(d->offset + size);
+
+    if(size > d->length)
+      ::memset(DATA(d) + d->offset + d->length, padding, size - d->length);
+
     d->length = size;
   }
 
index e9d43c0520a55d0eee5087ae70618f47bec55012..de6820843701941d14619ee69628849fcf4f9138 100644 (file)
@@ -42,6 +42,7 @@ class TestByteVector : public CppUnit::TestFixture
   CPPUNIT_TEST(testNumericCoversion);
   CPPUNIT_TEST(testReplace);
   CPPUNIT_TEST(testIterator);
+  CPPUNIT_TEST(testResize);
   CPPUNIT_TEST_SUITE_END();
 
 public:
@@ -123,12 +124,6 @@ public:
     CPPUNIT_ASSERT(i.containsAt(j, 5, 0));
     CPPUNIT_ASSERT(i.containsAt(j, 6, 1));
     CPPUNIT_ASSERT(i.containsAt(j, 6, 1, 3));
-
-    ByteVector k = ByteVector("0123456789").mid(3, 4);
-    k.resize(6, 'A');
-    CPPUNIT_ASSERT_EQUAL(uint(6), k.size());
-    CPPUNIT_ASSERT_EQUAL('6', k[3]);
-    CPPUNIT_ASSERT_EQUAL('A', k[4]);
   }
 
   void testFind1()
@@ -340,6 +335,50 @@ public:
     CPPUNIT_ASSERT_EQUAL('3', *it4);
   }
 
+  void testResize()
+  {
+    ByteVector a = ByteVector("0123456789");
+    ByteVector b = a.mid(3, 4);
+    b.resize(6, 'A');
+    CPPUNIT_ASSERT_EQUAL(uint(6), b.size());
+    CPPUNIT_ASSERT_EQUAL('6', b[3]);
+    CPPUNIT_ASSERT_EQUAL('A', b[4]);
+    CPPUNIT_ASSERT_EQUAL('A', b[5]);
+    b.resize(10, 'B');
+    CPPUNIT_ASSERT_EQUAL(uint(10), b.size());
+    CPPUNIT_ASSERT_EQUAL('6', b[3]);
+    CPPUNIT_ASSERT_EQUAL('B', b[6]);
+    CPPUNIT_ASSERT_EQUAL('B', b[9]);
+    b.resize(3, 'C');
+    CPPUNIT_ASSERT_EQUAL(uint(3), b.size());
+    CPPUNIT_ASSERT_EQUAL(-1, b.find('C'));
+    b.resize(3);
+    CPPUNIT_ASSERT_EQUAL(uint(3), b.size());
+
+    // Check if a and b were properly detached.
+
+    CPPUNIT_ASSERT_EQUAL(uint(10), a.size());
+    CPPUNIT_ASSERT_EQUAL('3', a[3]);
+    CPPUNIT_ASSERT_EQUAL('5', a[5]);
+
+    // Special case that refCount == 1 and d->offset != 0.
+
+    ByteVector c = ByteVector("0123456789").mid(3, 4);
+    c.resize(6, 'A');
+    CPPUNIT_ASSERT_EQUAL(uint(6), c.size());
+    CPPUNIT_ASSERT_EQUAL('6', c[3]);
+    CPPUNIT_ASSERT_EQUAL('A', c[4]);
+    CPPUNIT_ASSERT_EQUAL('A', c[5]);
+    c.resize(10, 'B');
+    CPPUNIT_ASSERT_EQUAL(uint(10), c.size());
+    CPPUNIT_ASSERT_EQUAL('6', c[3]);
+    CPPUNIT_ASSERT_EQUAL('B', c[6]);
+    CPPUNIT_ASSERT_EQUAL('B', c[9]);
+    c.resize(3, 'C');
+    CPPUNIT_ASSERT_EQUAL(uint(3), c.size());
+    CPPUNIT_ASSERT_EQUAL(-1, c.find('C'));
+  }
+
 };
 
 CPPUNIT_TEST_SUITE_REGISTRATION(TestByteVector);