Blame view
src/util/const-integer-set-test.cc
4.65 KB
8dcb6dfcb first commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
// util/const-integer-set-test.cc // Copyright 2009-2011 Microsoft Corporation // See ../../COPYING for clarification regarding multiple authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, // MERCHANTABLITY OR NON-INFRINGEMENT. // See the Apache 2 License for the specific language governing permissions and // limitations under the License. #include "util/const-integer-set.h" #include <set> // for baseline. #include <cassert> #include <cstdlib> #include <iostream> #include "util/kaldi-io.h" namespace kaldi { template<class Int> void TestSetOfNumbers(bool binary) { std::set<Int> baseline_set; size_t n_in_set = (Rand() % 3) * 50 + (Rand() % 4); // may be less than this. size_t max = (Int) (Rand() % 100) + 1; for (size_t i = 0; i < n_in_set; i++) { Int to_add((Int) (Rand() % max)); baseline_set.insert(to_add); } std::vector<Int> vector_set; for (typename std::set<Int>::iterator iter = baseline_set.begin(); iter!= baseline_set.end();iter++) vector_set.push_back(*iter); if (vector_set.size() != 0) { for (size_t i = 0;i < 10;i++) // randomize order. std::swap(vector_set[Rand()%vector_set.size()], vector_set[Rand()%vector_set.size()]); } ConstIntegerSet<Int> my_set1(baseline_set); ConstIntegerSet<Int> my_set2(vector_set); ConstIntegerSet<Int> my_set3; my_set3.Init(baseline_set); ConstIntegerSet<Int> my_set4; my_set4.Init(vector_set); { my_set4.Write(Output("tmpf", binary).Stream(), binary); } ConstIntegerSet<Int> my_set5; { bool binary_in; Input ki("tmpf", &binary_in); my_set5.Read(ki.Stream(), binary_in); } // if (enable_iterators) { size_t sz = baseline_set.size(), sz1 = my_set1.size(), sz2 = my_set2.size(), sz3 = my_set3.size(), sz4 = my_set4.size(), sz5 = my_set5.size(); KALDI_ASSERT(sz == sz1 && sz == sz2 && sz == sz3 && sz == sz4 && sz == sz5); // } for (size_t i = 0;i < 100;i++) { Int some_int; if (i%2 == 0 && vector_set.size() != 0) some_int = vector_set[Rand()%vector_set.size()]; else some_int = Rand() % max; bool in_baseline = (baseline_set.count(some_int) != 0); bool in_my_set1 = (my_set1.count(some_int) != 0); bool in_my_set2 = (my_set2.count(some_int) != 0); bool in_my_set3 = (my_set3.count(some_int) != 0); bool in_my_set4 = (my_set4.count(some_int) != 0); bool in_my_set5 = (my_set5.count(some_int) != 0); if (in_baseline) { KALDI_ASSERT(in_my_set1&&in_my_set2&&in_my_set3&&in_my_set4&&in_my_set5); } else { KALDI_ASSERT(!in_my_set1&&!in_my_set2&&!in_my_set3&&!in_my_set4&& !in_my_set5); } } // if (enable_iterators) { typename std::set<Int>::iterator baseline_iter = baseline_set.begin(); typename ConstIntegerSet<Int>::iterator my_iter1 = my_set1.begin(); typename ConstIntegerSet<Int>::iterator my_iter2 = my_set2.begin(); typename ConstIntegerSet<Int>::iterator my_iter3 = my_set3.begin(); typename ConstIntegerSet<Int>::iterator my_iter4 = my_set4.begin(); typename ConstIntegerSet<Int>::iterator my_iter5 = my_set5.begin(); while (baseline_iter != baseline_set.end()) { KALDI_ASSERT(my_iter1 != my_set1.end()); KALDI_ASSERT(my_iter2 != my_set2.end()); KALDI_ASSERT(my_iter3 != my_set3.end()); KALDI_ASSERT(my_iter4 != my_set4.end()); KALDI_ASSERT(my_iter5 != my_set5.end()); KALDI_ASSERT(*baseline_iter == *my_iter1); KALDI_ASSERT(*baseline_iter == *my_iter2); KALDI_ASSERT(*baseline_iter == *my_iter3); KALDI_ASSERT(*baseline_iter == *my_iter4); KALDI_ASSERT(*baseline_iter == *my_iter5); baseline_iter++; my_iter1++; my_iter2++; my_iter3++; my_iter4++; my_iter5++; } KALDI_ASSERT(my_iter1 == my_set1.end()); KALDI_ASSERT(my_iter2 == my_set2.end()); KALDI_ASSERT(my_iter3 == my_set3.end()); KALDI_ASSERT(my_iter4 == my_set4.end()); KALDI_ASSERT(my_iter5 == my_set5.end()); // } } } // end namespace kaldi int main() { using namespace kaldi; for (size_t i = 0;i < 10;i++) { TestSetOfNumbers<int>(Rand()%2); TestSetOfNumbers<unsigned int>(Rand()%2); TestSetOfNumbers<int16>(Rand()%2); TestSetOfNumbers<int16>(Rand()%2); TestSetOfNumbers<char>(Rand()%2); TestSetOfNumbers<unsigned char>(Rand()%2); } std::cout << "Test OK. "; } |