TSetTest.cpp revision 848b9af52dd545afb1fa37df910bcc87bf657843
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() and find() 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 copy constructor too. 64 SkTSet<int> set1 = set0; 65 66 REPORTER_ASSERT(reporter, set0.count() == set1.count()); 67 REPORTER_ASSERT(reporter, !set1.contains(-1000)); 68 69 for (int i = 0; i < COUNT; i++) { 70 REPORTER_ASSERT(reporter, set1.contains(f(i))); 71 } 72 73 // Test operator= too. 74 SkTSet<int> set2; 75 set2 = set0; 76 77 REPORTER_ASSERT(reporter, set0.count() == set2.count()); 78 REPORTER_ASSERT(reporter, !set2.contains(-1000)); 79 80 for (int i = 0; i < COUNT; i++) { 81 REPORTER_ASSERT(reporter, set2.contains(f(i))); 82 } 83 84#ifdef SK_DEBUG 85 set0.validate(); 86 set1.validate(); 87 set2.validate(); 88#endif 89} 90 91static void TestTSet_merge(skiatest::Reporter* reporter) { 92 SkTSet<int> set; 93 SkTSet<int> setOdd; 94 95 for (int i = 0; i < COUNT; i++) { 96 REPORTER_ASSERT(reporter, set.add(2 * i)); 97 REPORTER_ASSERT(reporter, setOdd.add(2 * i + 1)); 98 } 99 // mergeInto returns the number of duplicates. Expected 0. 100 REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == 0); 101 REPORTER_ASSERT(reporter, set.count() == 2 * COUNT); 102 103 // mergeInto should now find all new numbers duplicate. 104 REPORTER_ASSERT(reporter, set.mergeInto(setOdd) == setOdd.count()); 105 REPORTER_ASSERT(reporter, set.count() == 2 * COUNT); 106 107 for (int i = 0; i < 2 * COUNT; i++) { 108 REPORTER_ASSERT(reporter, set.contains(i)); 109 } 110 111#ifdef SK_DEBUG 112 set.validate(); 113 setOdd.validate(); 114#endif 115} 116 117static void TestTSet(skiatest::Reporter* reporter) { 118 TestTSet_basic(reporter); 119 TestTSet_advanced(reporter); 120 TestTSet_merge(reporter); 121} 122 123#include "TestClassDef.h" 124DEFINE_TESTCLASS("TSet", TSetTest, TestTSet) 125 126