TSetTest.cpp revision a7aa810894ae85306541ed949848a4dd7f907a0b
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 13static 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. 40static int f(int i) { 41 return (long(i) * PRIME1) % PRIME2; 42} 43 44// Will expose contains() too. 45static 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 63 // Test deterministic output 64 for (int i = 0; i < COUNT; i++) { 65 REPORTER_ASSERT(reporter, set0[i] == f(i)); 66 } 67 68 // Test copy constructor too. 69 SkTSet<int> set1 = set0; 70 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))); 76 REPORTER_ASSERT(reporter, set1[i] == f(i)); 77 } 78 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))); 88 REPORTER_ASSERT(reporter, set2[i] == f(i)); 89 } 90 91#ifdef SK_DEBUG 92 set0.validate(); 93 set1.validate(); 94 set2.validate(); 95#endif 96} 97 98static void TestTSet_merge(skiatest::Reporter* reporter) { 99 SkTSet<int> set; 100 SkTSet<int> setOdd; 101 102 for (int i = 0; i < COUNT; i++) { 103 REPORTER_ASSERT(reporter, set.add(2 * i)); 104 REPORTER_ASSERT(reporter, setOdd.add(2 * i + 1)); 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); 113 114 for (int i = 0; i < 2 * COUNT; i++) { 115 REPORTER_ASSERT(reporter, set.contains(i)); 116 } 117 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 124#ifdef SK_DEBUG 125 set.validate(); 126 setOdd.validate(); 127#endif 128} 129 130static void TestTSet(skiatest::Reporter* reporter) { 131 TestTSet_basic(reporter); 132 TestTSet_advanced(reporter); 133 TestTSet_merge(reporter); 134} 135 136#include "TestClassDef.h" 137DEFINE_TESTCLASS("TSet", TSetTest, TestTSet) 138