diff options
author | reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2009-02-27 22:06:06 +0000 |
---|---|---|
committer | reed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2009-02-27 22:06:06 +0000 |
commit | d8730ea8b25d692c0656f8cf03f02aecfab2a17c (patch) | |
tree | 7a1c8cac5a1433a8ced46bf6dc0f71675e168d76 /tests | |
parent | ed673310e2551e64d8196f7776d7d4c92085f8c2 (diff) |
more tests (need more meat in there)
git-svn-id: http://skia.googlecode.com/svn/trunk@97 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'tests')
-rw-r--r-- | tests/GeometryTest.cpp | 16 | ||||
-rw-r--r-- | tests/MathTest.cpp | 27 | ||||
-rw-r--r-- | tests/MatrixTest.cpp | 25 | ||||
-rw-r--r-- | tests/PackBitsTest.cpp | 27 | ||||
-rw-r--r-- | tests/Sk64Test.cpp | 200 | ||||
-rw-r--r-- | tests/StringTest.cpp | 54 | ||||
-rw-r--r-- | tests/TestClassDef.h | 24 | ||||
-rw-r--r-- | tests/TestXCode/Tests.xcodeproj/project.pbxproj | 18 | ||||
-rw-r--r-- | tests/UtilsTest.cpp | 25 |
9 files changed, 321 insertions, 95 deletions
diff --git a/tests/GeometryTest.cpp b/tests/GeometryTest.cpp new file mode 100644 index 0000000000..5b05952c84 --- /dev/null +++ b/tests/GeometryTest.cpp @@ -0,0 +1,16 @@ +#include "Test.h" +#include "SkGeometry.h" + +static void TestGeometry(skiatest::Reporter* reporter) { + SkPoint pts[3], dst[5]; + + pts[0].set(0, 0); + pts[1].set(100, 50); + pts[2].set(0, 100); + + int count = SkChopQuadAtMaxCurvature(pts, dst); + REPORTER_ASSERT(reporter, count == 1 || count == 2); +} + +#include "TestClassDef.h" +DEFINE_TESTCLASS("Geometry", GeometryTestClass, TestGeometry) diff --git a/tests/MathTest.cpp b/tests/MathTest.cpp index a759ab19a1..50d59cfd19 100644 --- a/tests/MathTest.cpp +++ b/tests/MathTest.cpp @@ -213,8 +213,6 @@ static void TestMath(skiatest::Reporter* reporter) { REPORTER_ASSERT(reporter, x == i); } - REPORTER_ASSERT(reporter, !"test the reporter"); - x = SkFixedSqrt(SK_Fixed1); REPORTER_ASSERT(reporter, x == SK_Fixed1); x = SkFixedSqrt(SK_Fixed1/4); @@ -375,26 +373,5 @@ static void TestMath(skiatest::Reporter* reporter) { #endif } -/////////////////////////////////////////////////////////////////////////////// - -namespace skiatest { - - class MathTest : public Test { - public: - static Test* Factory(void*) { - return SkNEW(MathTest); - } - - protected: - virtual void onGetName(SkString* name) { - name->set("Math"); - } - - virtual void onRun(Reporter* reporter) { - TestMath(reporter); - } - }; - - static TestRegistry gReg(MathTest::Factory); -} - +#include "TestClassDef.h" +DEFINE_TESTCLASS("Math", MathTestClass, TestMath) diff --git a/tests/MatrixTest.cpp b/tests/MatrixTest.cpp index 1e4e341f2b..659672a109 100644 --- a/tests/MatrixTest.cpp +++ b/tests/MatrixTest.cpp @@ -94,26 +94,5 @@ void TestMatrix(skiatest::Reporter* reporter) { } } -/////////////////////////////////////////////////////////////////////////////// - -namespace skiatest { - - class MatrixTest : public Test { - public: - static Test* Factory(void*) { - return SkNEW(MatrixTest); - } - - protected: - virtual void onGetName(SkString* name) { - name->set("Matrix"); - } - - virtual void onRun(Reporter* reporter) { - TestMatrix(reporter); - } - }; - - static TestRegistry gReg(MatrixTest::Factory); -} - +#include "TestClassDef.h" +DEFINE_TESTCLASS("Matrix", MatrixTestClass, TestMatrix) diff --git a/tests/PackBitsTest.cpp b/tests/PackBitsTest.cpp index 6e69f825a4..fc11fb26a9 100644 --- a/tests/PackBitsTest.cpp +++ b/tests/PackBitsTest.cpp @@ -118,27 +118,10 @@ static void test_pack8(skiatest::Reporter* reporter) { } } -/////////////////////////////////////////////////////////////////////////////// - -namespace skiatest { - - class PackBitsTest : public Test { - public: - static Test* Factory(void*) { - return SkNEW(PackBitsTest); - } - - protected: - virtual void onGetName(SkString* name) { - name->set("PackBits"); - } - - virtual void onRun(Reporter* reporter) { - test_pack8(reporter); - test_pack16(reporter); - } - }; - - static TestRegistry gReg(PackBitsTest::Factory); +static void TestPackBits(skiatest::Reporter* reporter) { + test_pack8(reporter); + test_pack16(reporter); } +#include "TestClassDef.h" +DEFINE_TESTCLASS("PackBits", PackBitsTestClass, TestPackBits) diff --git a/tests/Sk64Test.cpp b/tests/Sk64Test.cpp new file mode 100644 index 0000000000..1829c647e1 --- /dev/null +++ b/tests/Sk64Test.cpp @@ -0,0 +1,200 @@ +#include "Test.h" +#include "SkRandom.h" +#include <math.h> + +#ifdef SK_SUPPORT_UNITTEST +struct BoolTable { + int8_t zero, pos, neg, toBool, sign; +}; + +static void bool_table_test(skiatest::Reporter* reporter, + const Sk64& a, const BoolTable& table) +{ + REPORTER_ASSERT(reporter, a.isZero() != a.nonZero()); + + REPORTER_ASSERT(reporter, !a.isZero() == !table.zero); + REPORTER_ASSERT(reporter, !a.isPos() == !table.pos); + REPORTER_ASSERT(reporter, !a.isNeg() == !table.neg); + REPORTER_ASSERT(reporter, a.getSign() == table.sign); +} + +#ifdef SkLONGLONG + static SkLONGLONG asLL(const Sk64& a) + { + return ((SkLONGLONG)a.fHi << 32) | a.fLo; + } +#endif +#endif + +static void TestSk64(skiatest::Reporter* reporter) { + enum BoolTests { + kZero_BoolTest, + kPos_BoolTest, + kNeg_BoolTest + }; + static const BoolTable gBoolTable[] = { + { 1, 0, 0, 0, 0 }, + { 0, 1, 0, 1, 1 }, + { 0, 0, 1, 1, -1 } + }; + + Sk64 a, b, c; + + a.fHi = a.fLo = 0; + b.set(0); + c.setZero(); + REPORTER_ASSERT(reporter, a == b); + REPORTER_ASSERT(reporter, a == c); + bool_table_test(reporter, a, gBoolTable[kZero_BoolTest]); + + a.fHi = 0; a.fLo = 5; + b.set(5); + REPORTER_ASSERT(reporter, a == b); + REPORTER_ASSERT(reporter, a.is32() && a.get32() == 5 && !a.is64()); + bool_table_test(reporter, a, gBoolTable[kPos_BoolTest]); + + a.fHi = -1; a.fLo = (uint32_t)-5; + b.set(-5); + REPORTER_ASSERT(reporter, a == b); + REPORTER_ASSERT(reporter, a.is32() && a.get32() == -5 && !a.is64()); + bool_table_test(reporter, a, gBoolTable[kNeg_BoolTest]); + + a.setZero(); + b.set(6); + c.set(-6); + REPORTER_ASSERT(reporter, a != b && b != c && a != c); + REPORTER_ASSERT(reporter, !(a == b) && !(a == b) && !(a == b)); + REPORTER_ASSERT(reporter, a < b && b > a && a <= b && b >= a); + REPORTER_ASSERT(reporter, c < a && a > c && c <= a && a >= c); + REPORTER_ASSERT(reporter, c < b && b > c && c <= b && b >= c); + + // Now test add/sub + + SkRandom rand; + int i; + + for (i = 0; i < 1000; i++) + { + int aa = rand.nextS() >> 1; + int bb = rand.nextS() >> 1; + a.set(aa); + b.set(bb); + REPORTER_ASSERT(reporter, a.get32() == aa && b.get32() == bb); + c = a; c.add(bb); + REPORTER_ASSERT(reporter, c.get32() == aa + bb); + c = a; c.add(-bb); + REPORTER_ASSERT(reporter, c.get32() == aa - bb); + c = a; c.add(b); + REPORTER_ASSERT(reporter, c.get32() == aa + bb); + c = a; c.sub(b); + REPORTER_ASSERT(reporter, c.get32() == aa - bb); + } + +#ifdef SkLONGLONG + for (i = 0; i < 1000; i++) + { + rand.next64(&a); //a.fHi >>= 1; // avoid overflow + rand.next64(&b); //b.fHi >>= 1; // avoid overflow + + if (!(i & 3)) // want to explicitly test these cases + { + a.fLo = 0; + b.fLo = 0; + } + else if (!(i & 7)) // want to explicitly test these cases + { + a.fHi = 0; + b.fHi = 0; + } + + SkLONGLONG aa = asLL(a); + SkLONGLONG bb = asLL(b); + + REPORTER_ASSERT(reporter, (a < b) == (aa < bb)); + REPORTER_ASSERT(reporter, (a <= b) == (aa <= bb)); + REPORTER_ASSERT(reporter, (a > b) == (aa > bb)); + REPORTER_ASSERT(reporter, (a >= b) == (aa >= bb)); + REPORTER_ASSERT(reporter, (a == b) == (aa == bb)); + REPORTER_ASSERT(reporter, (a != b) == (aa != bb)); + + c = a; c.add(b); + REPORTER_ASSERT(reporter, asLL(c) == aa + bb); + c = a; c.sub(b); + REPORTER_ASSERT(reporter, asLL(c) == aa - bb); + c = a; c.rsub(b); + REPORTER_ASSERT(reporter, asLL(c) == bb - aa); + c = a; c.negate(); + REPORTER_ASSERT(reporter, asLL(c) == -aa); + + int bits = rand.nextU() & 63; + c = a; c.shiftLeft(bits); + REPORTER_ASSERT(reporter, asLL(c) == (aa << bits)); + c = a; c.shiftRight(bits); + REPORTER_ASSERT(reporter, asLL(c) == (aa >> bits)); + c = a; c.roundRight(bits); + + SkLONGLONG tmp; + + tmp = aa; + if (bits > 0) + tmp += (SkLONGLONG)1 << (bits - 1); + REPORTER_ASSERT(reporter, asLL(c) == (tmp >> bits)); + + c.setMul(a.fHi, b.fHi); + tmp = (SkLONGLONG)a.fHi * b.fHi; + REPORTER_ASSERT(reporter, asLL(c) == tmp); + } + + + for (i = 0; i < 100000; i++) + { + Sk64 wide; + int32_t denom = rand.nextS(); + + while (denom == 0) + denom = rand.nextS(); + wide.setMul(rand.nextS(), rand.nextS()); + SkLONGLONG check = wide.getLongLong(); + + wide.div(denom, Sk64::kTrunc_DivOption); + check /= denom; + SkLONGLONG w = wide.getLongLong(); + + REPORTER_ASSERT(reporter, check == w); + +#ifdef SK_CAN_USE_FLOAT + wide.setMul(rand.nextS(), rand.nextS()); + wide.abs(); + denom = wide.getSqrt(); + int32_t ck = (int32_t)sqrt((double)wide.getLongLong()); + int diff = denom - ck; + REPORTER_ASSERT(reporter, SkAbs32(diff) <= 1); + + wide.setMul(rand.nextS(), rand.nextS()); + Sk64 dwide; + dwide.setMul(rand.nextS(), rand.nextS()); + SkFixed fixdiv = wide.getFixedDiv(dwide); + double dnumer = (double)wide.getLongLong(); + double ddenom = (double)dwide.getLongLong(); + double ddiv = dnumer / ddenom; + SkFixed dfixdiv; + if (ddiv >= (double)SK_MaxS32 / (double)SK_Fixed1) + dfixdiv = SK_MaxS32; + else if (ddiv <= -(double)SK_MaxS32 / (double)SK_Fixed1) + dfixdiv = SK_MinS32; + else + dfixdiv = SkFloatToFixed(dnumer / ddenom); + diff = fixdiv - dfixdiv; + + if (SkAbs32(diff) > 1) { + SkDebugf(" %d === numer %g denom %g div %g xdiv %x fxdiv %x\n", + i, dnumer, ddenom, ddiv, dfixdiv, fixdiv); + } + REPORTER_ASSERT(reporter, SkAbs32(diff) <= 1); +#endif + } +#endif +} + +#include "TestClassDef.h" +DEFINE_TESTCLASS("Sk64", Sk64TestClass, TestSk64) diff --git a/tests/StringTest.cpp b/tests/StringTest.cpp new file mode 100644 index 0000000000..344c752c8c --- /dev/null +++ b/tests/StringTest.cpp @@ -0,0 +1,54 @@ +#include "Test.h" +#include "SkString.h" + +static void TestString(skiatest::Reporter* reporter) { + SkString a; + SkString b((size_t)0); + SkString c(""); + SkString d(NULL, 0); + + REPORTER_ASSERT(reporter, a.isEmpty()); + REPORTER_ASSERT(reporter, a == b && a == c && a == d); + + a.set("hello"); + b.set("hellox", 5); + c.set(a); + d.resize(5); + memcpy(d.writable_str(), "helloz", 5); + + REPORTER_ASSERT(reporter, !a.isEmpty()); + REPORTER_ASSERT(reporter, a.size() == 5); + REPORTER_ASSERT(reporter, a == b && a == c && a == d); + REPORTER_ASSERT(reporter, a.equals("hello", 5)); + REPORTER_ASSERT(reporter, a.equals("hello")); + REPORTER_ASSERT(reporter, !a.equals("help")); + + SkString e(a); + SkString f("hello"); + SkString g("helloz", 5); + + REPORTER_ASSERT(reporter, a == e && a == f && a == g); + + b.set("world"); + c = b; + REPORTER_ASSERT(reporter, a != b && a != c && b == c); + + a.append(" world"); + e.append("worldz", 5); + e.insert(5, " "); + f.set("world"); + f.prepend("hello "); + REPORTER_ASSERT(reporter, a.equals("hello world") && a == e && a == f); + + a.reset(); + b.resize(0); + REPORTER_ASSERT(reporter, a.isEmpty() && b.isEmpty() && a == b); + + a.set("a"); + a.set("ab"); + a.set("abc"); + a.set("abcd"); +} + +#include "TestClassDef.h" +DEFINE_TESTCLASS("String", StringTestClass, TestString) diff --git a/tests/TestClassDef.h b/tests/TestClassDef.h new file mode 100644 index 0000000000..77f48b3b46 --- /dev/null +++ b/tests/TestClassDef.h @@ -0,0 +1,24 @@ +/* This file is meant be including by .cpp files, so it can spew out a + customized class + global definition. + + e.g. + #include "TestClassDef.h" + DEFINE_TESTCLASS("MyTest", MyTestClass, MyTestFunction) + + where MyTestFunction is declared as + + void MyTestFunction(skiatest::Reporter*) +*/ + +#define DEFINE_TESTCLASS(uiname, classname, function) \ + namespace skiatest { \ + class classname : public Test { \ + public: \ + static Test* Factory(void*) { return SkNEW(classname); } \ + protected: \ + virtual void onGetName(SkString* name) { name->set(uiname); } \ + virtual void onRun(Reporter* reporter) { function(reporter); } \ + }; \ + static TestRegistry gReg(classname::Factory); \ + } + diff --git a/tests/TestXCode/Tests.xcodeproj/project.pbxproj b/tests/TestXCode/Tests.xcodeproj/project.pbxproj index 004fb2247e..7d7b924435 100644 --- a/tests/TestXCode/Tests.xcodeproj/project.pbxproj +++ b/tests/TestXCode/Tests.xcodeproj/project.pbxproj @@ -15,6 +15,9 @@ 008634DC0F579B7A0044DA64 /* PackBitsTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 008634DB0F579B7A0044DA64 /* PackBitsTest.cpp */; }; 008634F10F579E410044DA64 /* MatrixTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 008634F00F579E410044DA64 /* MatrixTest.cpp */; }; 0086350F0F57A3140044DA64 /* UtilsTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 0086350E0F57A3140044DA64 /* UtilsTest.cpp */; }; + 00A9BF860F584CF30091AD2D /* Sk64Test.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00A9BF850F584CF30091AD2D /* Sk64Test.cpp */; }; + 00A9BFA30F584E150091AD2D /* StringTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00A9BFA20F584E150091AD2D /* StringTest.cpp */; }; + 00A9BFBC0F5851570091AD2D /* GeometryTest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 00A9BFBB0F5851570091AD2D /* GeometryTest.cpp */; }; 8DD76F6A0486A84900D96B5E /* Tests.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = C6859E8B029090EE04C91782 /* Tests.1 */; }; /* End PBXBuildFile section */ @@ -37,14 +40,14 @@ isa = PBXContainerItemProxy; containerPortal = 00857F6B0F56F71B0078BE26 /* core.xcodeproj */; proxyType = 1; - remoteGlobalIDString = D2AAC045055464E500DB518D /* core */; + remoteGlobalIDString = D2AAC045055464E500DB518D; remoteInfo = core; }; 0086351E0F57A5200044DA64 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = 00857F890F56F9150078BE26 /* maccore.xcodeproj */; proxyType = 1; - remoteGlobalIDString = D2AAC045055464E500DB518D /* maccore */; + remoteGlobalIDString = D2AAC045055464E500DB518D; remoteInfo = maccore; }; /* End PBXContainerItemProxy section */ @@ -72,6 +75,10 @@ 008634DB0F579B7A0044DA64 /* PackBitsTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = PackBitsTest.cpp; path = ../PackBitsTest.cpp; sourceTree = SOURCE_ROOT; }; 008634F00F579E410044DA64 /* MatrixTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = MatrixTest.cpp; path = ../MatrixTest.cpp; sourceTree = SOURCE_ROOT; }; 0086350E0F57A3140044DA64 /* UtilsTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = UtilsTest.cpp; path = ../UtilsTest.cpp; sourceTree = SOURCE_ROOT; }; + 00A9BF850F584CF30091AD2D /* Sk64Test.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = Sk64Test.cpp; path = ../Sk64Test.cpp; sourceTree = SOURCE_ROOT; }; + 00A9BFA20F584E150091AD2D /* StringTest.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = StringTest.cpp; path = ../StringTest.cpp; sourceTree = SOURCE_ROOT; }; + 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; }; 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 */ @@ -120,12 +127,16 @@ 08FB7795FE84155DC02AAC07 /* Source */ = { isa = PBXGroup; children = ( + 00A9BF850F584CF30091AD2D /* Sk64Test.cpp */, + 00A9BFBB0F5851570091AD2D /* GeometryTest.cpp */, + 00A9BFA20F584E150091AD2D /* StringTest.cpp */, 00857FA80F56F9620078BE26 /* Test.cpp */, 00857FA90F56F9620078BE26 /* main.cpp */, 00857F630F56F4220078BE26 /* Test.h */, 00857FB60F56FD340078BE26 /* MathTest.cpp */, 0086350E0F57A3140044DA64 /* UtilsTest.cpp */, 008634F00F579E410044DA64 /* MatrixTest.cpp */, + 00A9BFA60F584F200091AD2D /* TestClassDef.h */, 008634DB0F579B7A0044DA64 /* PackBitsTest.cpp */, ); name = Source; @@ -225,6 +236,9 @@ 008634DC0F579B7A0044DA64 /* PackBitsTest.cpp in Sources */, 008634F10F579E410044DA64 /* MatrixTest.cpp in Sources */, 0086350F0F57A3140044DA64 /* UtilsTest.cpp in Sources */, + 00A9BF860F584CF30091AD2D /* Sk64Test.cpp in Sources */, + 00A9BFA30F584E150091AD2D /* StringTest.cpp in Sources */, + 00A9BFBC0F5851570091AD2D /* GeometryTest.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/tests/UtilsTest.cpp b/tests/UtilsTest.cpp index 810cb51650..f6b02112f0 100644 --- a/tests/UtilsTest.cpp +++ b/tests/UtilsTest.cpp @@ -105,26 +105,5 @@ static void TestUTF(skiatest::Reporter* reporter) { test_search(reporter); } -/////////////////////////////////////////////////////////////////////////////// - -namespace skiatest { - - class UtfTest : public Test { - public: - static Test* Factory(void*) { - return SkNEW(UtfTest); - } - - protected: - virtual void onGetName(SkString* name) { - name->set("UTF"); - } - - virtual void onRun(Reporter* reporter) { - TestUTF(reporter); - } - }; - - static TestRegistry gReg(UtfTest::Factory); -} - +#include "TestClassDef.h" +DEFINE_TESTCLASS("UTF", UtfTestClass, TestUTF) |