edisonn@google.com | 04115a1 | 2013-02-25 20:07:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | #include "Test.h" |
| 8 | #include "SkTSet.h" |
| 9 | |
| 10 | // Tests the SkTSet<T> class template. |
| 11 | // Functions that just call SkTDArray are not tested. |
| 12 | |
| 13 | static void TestTSet_basic(skiatest::Reporter* reporter) { |
| 14 | SkTSet<int> set0; |
| 15 | REPORTER_ASSERT(reporter, set0.isEmpty()); |
| 16 | REPORTER_ASSERT(reporter, !set0.contains(-1)); |
| 17 | REPORTER_ASSERT(reporter, !set0.contains(0)); |
| 18 | REPORTER_ASSERT(reporter, !set0.contains(1)); |
| 19 | REPORTER_ASSERT(reporter, set0.count() == 0); |
| 20 | |
| 21 | REPORTER_ASSERT(reporter, set0.add(0)); |
| 22 | REPORTER_ASSERT(reporter, !set0.isEmpty()); |
| 23 | REPORTER_ASSERT(reporter, !set0.contains(-1)); |
| 24 | REPORTER_ASSERT(reporter, set0.contains(0)); |
| 25 | REPORTER_ASSERT(reporter, !set0.contains(1)); |
| 26 | REPORTER_ASSERT(reporter, set0.count() == 1); |
| 27 | REPORTER_ASSERT(reporter, !set0.add(0)); |
| 28 | REPORTER_ASSERT(reporter, set0.count() == 1); |
| 29 | |
| 30 | #ifdef SK_DEBUG |
| 31 | set0.validate(); |
| 32 | #endif |
| 33 | } |
| 34 | |
| 35 | #define COUNT 1732 |
| 36 | #define PRIME1 10007 |
| 37 | #define PRIME2 1733 |
| 38 | |
| 39 | // Generates a series of positive unique pseudo-random numbers. |
| 40 | static int f(int i) { |
| 41 | return (long(i) * PRIME1) % PRIME2; |
| 42 | } |
| 43 | |
commit-bot@chromium.org | a7aa810 | 2013-07-24 01:51:08 +0000 | [diff] [blame^] | 44 | // Will expose contains() too. |
edisonn@google.com | 04115a1 | 2013-02-25 20:07:24 +0000 | [diff] [blame] | 45 | static void TestTSet_advanced(skiatest::Reporter* reporter) { |
| 46 | SkTSet<int> set0; |
| 47 | |
| 48 | for (int i = 0; i < COUNT; i++) { |
| 49 | REPORTER_ASSERT(reporter, !set0.contains(f(i))); |
| 50 | if (i > 0) { |
| 51 | REPORTER_ASSERT(reporter, set0.contains(f(0))); |
| 52 | REPORTER_ASSERT(reporter, set0.contains(f(i / 2))); |
| 53 | REPORTER_ASSERT(reporter, set0.contains(f(i - 1))); |
| 54 | } |
| 55 | REPORTER_ASSERT(reporter, !set0.contains(f(i))); |
| 56 | REPORTER_ASSERT(reporter, set0.count() == i); |
| 57 | REPORTER_ASSERT(reporter, set0.add(f(i))); |
| 58 | REPORTER_ASSERT(reporter, set0.contains(f(i))); |
| 59 | REPORTER_ASSERT(reporter, set0.count() == i + 1); |
| 60 | REPORTER_ASSERT(reporter, !set0.add(f(i))); |
| 61 | } |
| 62 | |
commit-bot@chromium.org | a7aa810 | 2013-07-24 01:51:08 +0000 | [diff] [blame^] | 63 | // Test deterministic output |
| 64 | for (int i = 0; i < COUNT; i++) { |
| 65 | REPORTER_ASSERT(reporter, set0[i] == f(i)); |
| 66 | } |
| 67 | |
edisonn@google.com | 04115a1 | 2013-02-25 20:07:24 +0000 | [diff] [blame] | 68 | // Test copy constructor too. |
| 69 | SkTSet<int> set1 = set0; |
skia.committer@gmail.com | 5ca3bd0 | 2013-02-26 07:01:22 +0000 | [diff] [blame] | 70 | |
edisonn@google.com | 04115a1 | 2013-02-25 20:07:24 +0000 | [diff] [blame] | 71 | REPORTER_ASSERT(reporter, set0.count() == set1.count()); |
| 72 | REPORTER_ASSERT(reporter, !set1.contains(-1000)); |
| 73 | |
| 74 | for (int i = 0; i < COUNT; i++) { |
| 75 | REPORTER_ASSERT(reporter, set1.contains(f(i))); |
commit-bot@chromium.org | a7aa810 | 2013-07-24 01:51:08 +0000 | [diff] [blame^] | 76 | REPORTER_ASSERT(reporter, set1[i] == f(i)); |
edisonn@google.com | 04115a1 | 2013-02-25 20:07:24 +0000 | [diff] [blame] | 77 | } |
skia.committer@gmail.com | 5ca3bd0 | 2013-02-26 07:01:22 +0000 | [diff] [blame] | 78 | |
edisonn@google.com | 04115a1 | 2013-02-25 20:07:24 +0000 | [diff] [blame] | 79 | // Test operator= too. |
| 80 | SkTSet<int> set2; |
| 81 | set2 = set0; |
| 82 | |
| 83 | REPORTER_ASSERT(reporter, set0.count() == set2.count()); |
| 84 | REPORTER_ASSERT(reporter, !set2.contains(-1000)); |
| 85 | |
| 86 | for (int i = 0; i < COUNT; i++) { |
| 87 | REPORTER_ASSERT(reporter, set2.contains(f(i))); |
commit-bot@chromium.org | a7aa810 | 2013-07-24 01:51:08 +0000 | [diff] [blame^] | 88 | REPORTER_ASSERT(reporter, set2[i] == f(i)); |
edisonn@google.com | 04115a1 | 2013-02-25 20:07:24 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | #ifdef SK_DEBUG |
| 92 | set0.validate(); |
| 93 | set1.validate(); |
| 94 | set2.validate(); |
| 95 | #endif |
| 96 | } |
| 97 | |
| 98 | static void TestTSet_merge(skiatest::Reporter* reporter) { |
| 99 | SkTSet<int> set; |
| 100 | SkTSet<int> setOdd; |
skia.committer@gmail.com | 5ca3bd0 | 2013-02-26 07:01:22 +0000 | [diff] [blame] | 101 | |
edisonn@google.com | 04115a1 | 2013-02-25 20:07:24 +0000 | [diff] [blame] | 102 | for (int i = 0; i < COUNT; i++) { |
| 103 | REPORTER_ASSERT(reporter, set.add(2 * i)); |
skia.committer@gmail.com | 5ca3bd0 | 2013-02-26 07:01:22 +0000 | [diff] [blame] | 104 | REPORTER_ASSERT(reporter, setOdd.add(2 * i + 1)); |
edisonn@google.com | 04115a1 | 2013-02-25 20:07:24 +0000 | [diff] [blame] | 105 | } |
| 106 | // mergeInto returns the number of duplicates. Expected 0. |
| 107 | REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == 0); |
| 108 | REPORTER_ASSERT(reporter, set.count() == 2 * COUNT); |
| 109 | |
| 110 | // mergeInto should now find all new numbers duplicate. |
| 111 | REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == setOdd.count()); |
| 112 | REPORTER_ASSERT(reporter, set.count() == 2 * COUNT); |
skia.committer@gmail.com | 5ca3bd0 | 2013-02-26 07:01:22 +0000 | [diff] [blame] | 113 | |
edisonn@google.com | 04115a1 | 2013-02-25 20:07:24 +0000 | [diff] [blame] | 114 | for (int i = 0; i < 2 * COUNT; i++) { |
| 115 | REPORTER_ASSERT(reporter, set.contains(i)); |
| 116 | } |
| 117 | |
commit-bot@chromium.org | a7aa810 | 2013-07-24 01:51:08 +0000 | [diff] [blame^] | 118 | // check deterministic output |
| 119 | for (int i = 0; i < COUNT; i++) { |
| 120 | REPORTER_ASSERT(reporter, set[i] == 2 * i); |
| 121 | REPORTER_ASSERT(reporter, set[COUNT + i] == 2 * i + 1); |
| 122 | } |
| 123 | |
edisonn@google.com | 04115a1 | 2013-02-25 20:07:24 +0000 | [diff] [blame] | 124 | #ifdef SK_DEBUG |
| 125 | set.validate(); |
| 126 | setOdd.validate(); |
| 127 | #endif |
| 128 | } |
| 129 | |
| 130 | static void TestTSet(skiatest::Reporter* reporter) { |
| 131 | TestTSet_basic(reporter); |
| 132 | TestTSet_advanced(reporter); |
| 133 | TestTSet_merge(reporter); |
| 134 | } |
| 135 | |
| 136 | #include "TestClassDef.h" |
| 137 | DEFINE_TESTCLASS("TSet", TSetTest, TestTSet) |