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