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