migrate more legacy unittests into tests/
SkParse yet to be cleaned up



git-svn-id: http://skia.googlecode.com/svn/trunk@113 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tests/PathMeasureTest.cpp b/tests/PathMeasureTest.cpp
new file mode 100644
index 0000000..de18764
--- /dev/null
+++ b/tests/PathMeasureTest.cpp
@@ -0,0 +1,42 @@
+#include "Test.h"
+#include "SkPathMeasure.h"
+
+static void TestPathMeasure(skiatest::Reporter* reporter) {
+    SkPath  path;
+
+    path.moveTo(0, 0);
+    path.lineTo(SK_Scalar1, 0);
+    path.lineTo(SK_Scalar1, SK_Scalar1);
+    path.lineTo(0, SK_Scalar1);
+
+    SkPathMeasure   meas(path, true);
+    SkScalar        length = meas.getLength();
+    SkASSERT(length == SK_Scalar1*4);
+
+    path.reset();
+    path.moveTo(0, 0);
+    path.lineTo(SK_Scalar1*3, SK_Scalar1*4);
+    meas.setPath(&path, false);
+    length = meas.getLength();
+    REPORTER_ASSERT(reporter, length == SK_Scalar1*5);
+
+    path.reset();
+    path.addCircle(0, 0, SK_Scalar1);
+    meas.setPath(&path, true);
+    length = meas.getLength();
+//    SkDebugf("circle arc-length = %g\n", length);
+
+    for (int i = 0; i < 8; i++) {
+        SkScalar    d = length * i / 8;
+        SkPoint     p;
+        SkVector    v;
+        meas.getPosTan(d, &p, &v);
+#if 0
+        SkDebugf("circle arc-length=%g, pos[%g %g] tan[%g %g]\n",
+                 d, p.fX, p.fY, v.fX, v.fY);
+#endif
+    }
+}
+
+#include "TestClassDef.h"
+DEFINE_TESTCLASS("PathMeasure", PathMeasureTestClass, TestPathMeasure)
diff --git a/tests/SortTest.cpp b/tests/SortTest.cpp
new file mode 100644
index 0000000..6d287c5
--- /dev/null
+++ b/tests/SortTest.cpp
@@ -0,0 +1,30 @@
+#include "Test.h"
+#include "SkRandom.h"
+#include "SkTSearch.h"
+
+extern "C" {
+    int compare_int(const void* a, const void* b) {
+        return *(const int*)a - *(const int*)b;
+    }
+}
+
+static void TestSort(skiatest::Reporter* reporter) {
+    int         array[100];
+    SkRandom    rand;
+
+    for (int i = 0; i < 1000; i++) {
+        int j, count = rand.nextRangeU(1, SK_ARRAY_COUNT(array));
+        for (j = 0; j < count; j++) {
+            array[j] = rand.nextS() & 0xFF;
+        }
+        SkQSort(array, count, sizeof(int), compare_int);
+        for (j = 1; j < count; j++) {
+            REPORTER_ASSERT(reporter, array[j-1] <= array[j]);
+        }
+    }
+}
+
+// need tests for SkStrSearch
+
+#include "TestClassDef.h"
+DEFINE_TESTCLASS("Sort", SortTestClass, TestSort)
diff --git a/tests/StreamTest.cpp b/tests/StreamTest.cpp
new file mode 100644
index 0000000..704ab4a
--- /dev/null
+++ b/tests/StreamTest.cpp
@@ -0,0 +1,111 @@
+#include "Test.h"
+#include "SkRandom.h"
+#include "SkStream.h"
+
+#define MAX_SIZE    (256 * 1024)
+
+static void random_fill(SkRandom& rand, void* buffer, size_t size) {
+    char* p = (char*)buffer;
+    char* stop = p + size;
+    while (p < stop) {
+        *p++ = (char)(rand.nextU() >> 8);
+    }
+}
+
+static void test_buffer(skiatest::Reporter* reporter) {
+    SkRandom rand;
+    SkAutoMalloc am(MAX_SIZE * 2);
+    char* storage = (char*)am.get();
+    char* storage2 = storage + MAX_SIZE;
+
+    random_fill(rand, storage, MAX_SIZE);
+
+    for (int sizeTimes = 0; sizeTimes < 100; sizeTimes++) {
+        int size = rand.nextU() % MAX_SIZE;
+        if (size == 0) {
+            size = MAX_SIZE;
+        }
+        for (int times = 0; times < 100; times++) {
+            int bufferSize = 1 + (rand.nextU() & 0xFFFF);
+            SkMemoryStream mstream(storage, size);
+            SkBufferStream bstream(&mstream, bufferSize);
+            
+            int bytesRead = 0;
+            while (bytesRead < size) {
+                int s = 17 + (rand.nextU() & 0xFFFF);
+                int ss = bstream.read(storage2, s);
+                REPORTER_ASSERT(reporter, ss > 0 && ss <= s);
+                REPORTER_ASSERT(reporter, bytesRead + ss <= size);
+                REPORTER_ASSERT(reporter, memcmp(storage + bytesRead, storage2, ss) == 0);
+                bytesRead += ss;
+            }
+            REPORTER_ASSERT(reporter, bytesRead == size);
+        }
+    }
+}
+
+static void TestRStream(skiatest::Reporter* reporter) {
+    static const char s[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+    char            copy[sizeof(s)];
+    SkRandom        rand;
+    
+    for (int i = 0; i < 65; i++)
+    {
+        char*           copyPtr = copy;
+        SkMemoryStream  mem(s, sizeof(s));
+        SkBufferStream  buff(&mem, i);
+        
+        do {
+            copyPtr += buff.read(copyPtr, rand.nextU() & 15);
+        } while (copyPtr < copy + sizeof(s));
+        REPORTER_ASSERT(reporter, copyPtr == copy + sizeof(s));
+        REPORTER_ASSERT(reporter, memcmp(s, copy, sizeof(s)) == 0);
+    }
+    test_buffer(reporter);
+}
+
+static void TestWStream(skiatest::Reporter* reporter)
+{
+    if (false) {
+        SkDebugWStream  s;
+
+        s.writeText("compare: 0 ");         s.writeDecAsText(0);    s.newline();
+        s.writeText("compare: 591 ");       s.writeDecAsText(591);  s.newline();
+        s.writeText("compare: -9125 ");     s.writeDecAsText(-9125);    s.newline();
+        s.writeText("compare: 0 ");         s.writeHexAsText(0, 0); s.newline();
+        s.writeText("compare: 03FA ");      s.writeHexAsText(0x3FA, 4); s.newline();
+        s.writeText("compare: DEADBEEF ");  s.writeHexAsText(0xDEADBEEF, 4);    s.newline();
+        s.writeText("compare: 0 ");         s.writeScalarAsText(SkIntToScalar(0));  s.newline();
+        s.writeText("compare: 27 ");        s.writeScalarAsText(SkIntToScalar(27)); s.newline();
+        s.writeText("compare: -119 ");      s.writeScalarAsText(SkIntToScalar(-119));   s.newline();
+        s.writeText("compare: 851.3333 ");  s.writeScalarAsText(SkIntToScalar(851) + SK_Scalar1/3); s.newline();
+        s.writeText("compare: -0.08 ");     s.writeScalarAsText(-SK_Scalar1*8/100); s.newline();
+    }
+
+    {
+        SkDynamicMemoryWStream  ds;
+        const char s[] = "abcdefghijklmnopqrstuvwxyz";
+        int i;
+        for (i = 0; i < 100; i++) {
+            REPORTER_ASSERT(reporter, ds.write(s, 26));
+        }
+        REPORTER_ASSERT(reporter, ds.getOffset() == 100 * 26);
+        char* dst = new char[100 * 26 + 1];
+        dst[100*26] = '*';
+        ds.copyTo(dst);
+        REPORTER_ASSERT(reporter, dst[100*26] == '*');
+   //     char* p = dst;
+        for (i = 0; i < 100; i++)
+            REPORTER_ASSERT(reporter, memcmp(&dst[i * 26], s, 26) == 0);
+        REPORTER_ASSERT(reporter, memcmp(dst, ds.getStream(), 100*26) == 0);
+        delete[] dst;
+    }
+}
+
+static void TestStreams(skiatest::Reporter* reporter) {
+    TestRStream(reporter);
+    TestWStream(reporter);
+}
+
+#include "TestClassDef.h"
+DEFINE_TESTCLASS("Stream", StreamTestClass, TestStreams)
diff --git a/tests/TestXCode/Tests.xcodeproj/project.pbxproj b/tests/TestXCode/Tests.xcodeproj/project.pbxproj
index ec2c171..af9dfd7 100644
--- a/tests/TestXCode/Tests.xcodeproj/project.pbxproj
+++ b/tests/TestXCode/Tests.xcodeproj/project.pbxproj
@@ -21,6 +21,9 @@
 		00A9BFA30F584E150091AD2D /* StringTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00A9BFA20F584E150091AD2D /* StringTest.cpp */; };
 		00A9BFBC0F5851570091AD2D /* GeometryTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00A9BFBB0F5851570091AD2D /* GeometryTest.cpp */; };
 		276D93080F5B9FEA0081B3B9 /* PathTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 276D93070F5B9FEA0081B3B9 /* PathTest.cpp */; };
+		27C9A9C70F6222EE00E9C93D /* PathMeasureTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C9A9C40F6222EE00E9C93D /* PathMeasureTest.cpp */; };
+		27C9A9C80F6222EE00E9C93D /* SortTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C9A9C50F6222EE00E9C93D /* SortTest.cpp */; };
+		27C9A9C90F6222EE00E9C93D /* StreamTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 27C9A9C60F6222EE00E9C93D /* StreamTest.cpp */; };
 		8DD76F6A0486A84900D96B5E /* Tests.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = C6859E8B029090EE04C91782 /* Tests.1 */; };
 /* End PBXBuildFile section */
 
@@ -85,6 +88,9 @@
 		00A9BFA60F584F200091AD2D /* TestClassDef.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TestClassDef.h; path = ../TestClassDef.h; sourceTree = SOURCE_ROOT; };
 		00A9BFBB0F5851570091AD2D /* GeometryTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = GeometryTest.cpp; path = ../GeometryTest.cpp; sourceTree = SOURCE_ROOT; };
 		276D93070F5B9FEA0081B3B9 /* PathTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PathTest.cpp; path = ../PathTest.cpp; sourceTree = SOURCE_ROOT; };
+		27C9A9C40F6222EE00E9C93D /* PathMeasureTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PathMeasureTest.cpp; path = ../PathMeasureTest.cpp; sourceTree = SOURCE_ROOT; };
+		27C9A9C50F6222EE00E9C93D /* SortTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = SortTest.cpp; path = ../SortTest.cpp; sourceTree = SOURCE_ROOT; };
+		27C9A9C60F6222EE00E9C93D /* StreamTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = StreamTest.cpp; path = ../StreamTest.cpp; sourceTree = SOURCE_ROOT; };
 		8DD76F6C0486A84900D96B5E /* Tests */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Tests; sourceTree = BUILT_PRODUCTS_DIR; };
 		C6859E8B029090EE04C91782 /* Tests.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = Tests.1; sourceTree = "<group>"; };
 /* End PBXFileReference section */
@@ -133,6 +139,9 @@
 		08FB7795FE84155DC02AAC07 /* Source */ = {
 			isa = PBXGroup;
 			children = (
+				27C9A9C40F6222EE00E9C93D /* PathMeasureTest.cpp */,
+				27C9A9C50F6222EE00E9C93D /* SortTest.cpp */,
+				27C9A9C60F6222EE00E9C93D /* StreamTest.cpp */,
 				009CC7870F5DAF16002185BE /* ClipCubicTest.cpp */,
 				276D93070F5B9FEA0081B3B9 /* PathTest.cpp */,
 				00A9BF850F584CF30091AD2D /* Sk64Test.cpp */,
@@ -251,6 +260,9 @@
 				276D93080F5B9FEA0081B3B9 /* PathTest.cpp in Sources */,
 				009CC7840F5DAE2B002185BE /* SrcOverTest.cpp in Sources */,
 				009CC7880F5DAF16002185BE /* ClipCubicTest.cpp in Sources */,
+				27C9A9C70F6222EE00E9C93D /* PathMeasureTest.cpp in Sources */,
+				27C9A9C80F6222EE00E9C93D /* SortTest.cpp in Sources */,
+				27C9A9C90F6222EE00E9C93D /* StreamTest.cpp in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
diff --git a/tests/main.cpp b/tests/main.cpp
index 0b82af4..7975de0 100644
--- a/tests/main.cpp
+++ b/tests/main.cpp
@@ -1,5 +1,5 @@
 #include <iostream>
-
+#include "SkGraphics.h"
 #include "Test.h"
 
 using namespace skiatest;
@@ -46,7 +46,19 @@
     virtual void onEnd(Test* test) {}
 };
 
+class SkAutoGraphics {
+public:
+    SkAutoGraphics() {
+        SkGraphics::Init();
+    }
+    ~SkAutoGraphics() {
+        SkGraphics::Term();
+    }
+};
+
 int main (int argc, char * const argv[]) {
+    SkAutoGraphics ag;
+
     PrintfReporter reporter;
     Iter iter(&reporter);
     Test* test;