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