Fix leak in SkAutoSTArray

https://codereview.chromium.org/18915010/



git-svn-id: http://skia.googlecode.com/svn/trunk@10053 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tests/UtilsTest.cpp b/tests/UtilsTest.cpp
index bdabb57..63b905a 100644
--- a/tests/UtilsTest.cpp
+++ b/tests/UtilsTest.cpp
@@ -48,6 +48,75 @@
     REPORTER_ASSERT(reporter, 1 == obj.getRefCnt());
 }
 
+static void test_autostarray(skiatest::Reporter* reporter) {
+    RefClass obj0(0);
+    RefClass obj1(1);
+    REPORTER_ASSERT(reporter, 1 == obj0.getRefCnt());
+    REPORTER_ASSERT(reporter, 1 == obj1.getRefCnt());
+
+    {
+        SkAutoSTArray<2, SkRefPtr<RefClass> > tmp;
+        REPORTER_ASSERT(reporter, 0 == tmp.count());
+
+        tmp.reset(0);   // test out reset(0) when already at 0
+        tmp.reset(4);   // this should force a new allocation
+        REPORTER_ASSERT(reporter, 4 == tmp.count());
+        tmp[0] = &obj0;
+        tmp[1] = &obj1;
+        REPORTER_ASSERT(reporter, 2 == obj0.getRefCnt());
+        REPORTER_ASSERT(reporter, 2 == obj1.getRefCnt());
+
+        // test out reset with data in the array (and a new allocation)
+        tmp.reset(0);
+        REPORTER_ASSERT(reporter, 0 == tmp.count());
+        REPORTER_ASSERT(reporter, 1 == obj0.getRefCnt());
+        REPORTER_ASSERT(reporter, 1 == obj1.getRefCnt());
+        
+        tmp.reset(2);   // this should use the preexisting allocation
+        REPORTER_ASSERT(reporter, 2 == tmp.count());
+        tmp[0] = &obj0;
+        tmp[1] = &obj1;
+    }
+
+    // test out destructor with data in the array (and using existing allocation)
+    REPORTER_ASSERT(reporter, 1 == obj0.getRefCnt());
+    REPORTER_ASSERT(reporter, 1 == obj1.getRefCnt());
+
+    {
+        // test out allocating ctor (this should allocate new memory)
+        SkAutoSTArray<2, SkRefPtr<RefClass> > tmp(4);
+        REPORTER_ASSERT(reporter, 4 == tmp.count());
+
+        tmp[0] = &obj0;
+        tmp[1] = &obj1;
+        REPORTER_ASSERT(reporter, 2 == obj0.getRefCnt());
+        REPORTER_ASSERT(reporter, 2 == obj1.getRefCnt());
+
+        // Test out resut with data in the array and malloced storage
+        tmp.reset(0);
+        REPORTER_ASSERT(reporter, 1 == obj0.getRefCnt());
+        REPORTER_ASSERT(reporter, 1 == obj1.getRefCnt());
+
+        tmp.reset(2);   // this should use the preexisting storage
+        tmp[0] = &obj0;
+        tmp[1] = &obj1;
+        REPORTER_ASSERT(reporter, 2 == obj0.getRefCnt());
+        REPORTER_ASSERT(reporter, 2 == obj1.getRefCnt());
+
+        tmp.reset(4);   // this should force a new malloc
+        REPORTER_ASSERT(reporter, 1 == obj0.getRefCnt());
+        REPORTER_ASSERT(reporter, 1 == obj1.getRefCnt());
+
+        tmp[0] = &obj0;
+        tmp[1] = &obj1;
+        REPORTER_ASSERT(reporter, 2 == obj0.getRefCnt());
+        REPORTER_ASSERT(reporter, 2 == obj1.getRefCnt());
+    }
+
+    REPORTER_ASSERT(reporter, 1 == obj0.getRefCnt());
+    REPORTER_ASSERT(reporter, 1 == obj1.getRefCnt());
+}
+
 /////////////////////////////////////////////////////////////////////////////
 
 #define kSEARCH_COUNT   91
@@ -150,6 +219,7 @@
     test_utf16(reporter);
     test_search(reporter);
     test_autounref(reporter);
+    test_autostarray(reporter);
 }
 
 #include "TestClassDef.h"