stl-utils-test.cc
Go to the documentation of this file.
1 // util/stl-utils-test.cc
2 
3 // Copyright 2009-2012 Microsoft Corporation; Saarland University
4 // Johns Hopkins University (Author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 
22 #include "base/kaldi-common.h"
23 #include "util/stl-utils.h"
24 
25 namespace kaldi {
26 static void TestIsSorted() {
27  for (int i = 0;i < 100;i++) {
28  std::vector<int> vec, vec2;
29  int len = Rand()%5;
30  for (int i = 0;i < len;i++)
31  vec.push_back(Rand() % 10);
32  vec2 = vec;
33  std::sort(vec2.begin(), vec2.end());
34  KALDI_ASSERT(IsSorted(vec) == (vec == vec2));
35  }
36 }
37 
38 static void TestIsSortedAndUniq() {
39  for (int i = 0;i < 100;i++) {
40  std::vector<int> vec, vec2;
41  int len = Rand()%5;
42  for (int i = 0;i < len;i++)
43  vec.push_back(Rand() % 10);
44 
45  if (!IsSortedAndUniq(vec)) {
46  bool ok = false;
47  for (size_t i = 0; i+1 < (size_t)len; i++)
48  if (vec[i] >= vec[i+1]) ok = true; // found out-of-order or dup.
49  KALDI_ASSERT(ok);
50  } else { // is sorted + uniq.
51  for (size_t i = 0; i+1 < (size_t)len; i++)
52  KALDI_ASSERT(vec[i] < vec[i+1]);
53  }
54  }
55 }
56 
57 
58 static void TestUniq() {
59  for (int i = 0;i < 100;i++) {
60  std::vector<int> vec;
61 
62  int cur = 1; // sorted order.
63  int len = Rand()%5;
64  for (int i = 0;i < len;i++) {
65  cur += 1 + (Rand() % 100);
66  vec.push_back(cur);
67  }
68  std::vector<int> vec2;
69  for (int i = 0;i < len;i++) {
70  int count = 1 + Rand()%5;
71  for (int j = 0;j < count;j++) vec2.push_back(vec[i]);
72  }
73  Uniq(&vec2);
74  KALDI_ASSERT(vec2 == vec);
75  }
76 }
77 
78 
79 static void TestSortAndUniq() {
80  for (int i = 0;i < 100;i++) {
81  std::vector<int> vec;
82 
83  int len = Rand()%5;
84  for (int i = 0;i < len;i++) {
85  int n = Rand() % 100;
86  bool ok = true;
87  for (size_t j = 0;j < vec.size();j++) if (vec[j] == n) ok = false;
88  if (ok) vec.push_back(n);
89  }
90  // don't sort.
91  std::vector<int> vec2(vec); // make sure all things in "vec" represented
92  // in vec2.
93  int len2 = Rand()%10;
94  if (vec.size() > 0) // add more, randomly.
95  for (int i = 0;i < len2;i++)
96  vec2.push_back(vec[Rand()%vec.size()]);
97  SortAndUniq(&vec2);
98  std::sort(vec.begin(), vec.end());
99  KALDI_ASSERT(vec == vec2);
100  }
101 }
102 
104  for (int p = 0; p < 100; p++) {
105  std::set<int> st;
106  int sz = Rand() % 20;
107  for (int i = 0;i < sz;i++) st.insert(Rand() % 10);
108  std::vector<int> v;
109  CopySetToVector(st, &v);
110  KALDI_ASSERT(st.size() == v.size());
111  for (size_t i = 0;i < v.size();i++) KALDI_ASSERT(st.count(v[i]) != 0);
112  }
113 }
114 
115 
117  for (int p = 0; p < 100; p++) {
118  std::map<int, int> mp;
119  int sz = Rand() % 20;
120  for (int i = 0;i < sz;i++) mp[Rand() % 10] = Rand() % 20;
121  std::vector<std::pair<int, int> > v;
122  CopyMapToVector(mp, &v);
123  KALDI_ASSERT(mp.size() == v.size());
124  for (size_t i = 0;i < v.size();i++)
125  KALDI_ASSERT(mp[v[i].first] == v[i].second);
126  }
127 }
128 
129 
131  for (int p = 0; p < 100; p++) {
132  std::map<int, int> mp;
133  int sz = Rand() % 20;
134  for (int i = 0;i < sz;i++) mp[Rand() % 10] = Rand() % 20;
135  std::vector<int> v;
136  CopyMapKeysToVector(mp, &v);
137  KALDI_ASSERT(mp.size() == v.size());
138  for (size_t i = 0;i < v.size();i++) KALDI_ASSERT(mp.count(v[i]) == 1);
139  }
140 }
141 
143  for (int p = 0; p < 100; p++) {
144  std::map<int, int> mp;
145  int sz = Rand() % 20;
146  for (int i = 0;i < sz;i++) mp[Rand() % 10] = Rand() % 20;
147  std::vector<int> v;
148  CopyMapValuesToVector(mp, &v);
149  KALDI_ASSERT(mp.size() == v.size());
150  int i = 0;
151  for (std::map<int, int>::iterator iter = mp.begin(); iter != mp.end();
152  iter++) {
153  KALDI_ASSERT(v[i++] == iter->second);
154  }
155  }
156 }
157 
159  for (int p = 0; p < 100; p++) {
160  std::map<int, int> mp;
161  int sz = Rand() % 20;
162  for (int i = 0;i < sz;i++) mp[Rand() % 10] = Rand() % 20;
163  std::vector<int> v;
164  std::set<int> s;
165  CopyMapKeysToVector(mp, &v);
166  CopyMapKeysToSet(mp, &s);
167  std::set<int> s2;
168  CopyVectorToSet(v, &s2);
169  KALDI_ASSERT(s == s2);
170  }
171 }
172 
173 
175  for (int p = 0; p < 100; p++) {
176  std::map<int, int> mp;
177  int sz = Rand() % 20;
178  for (int i = 0;i < sz;i++) mp[Rand() % 10] = Rand() % 20;
179  std::vector<int> v;
180  std::set<int> s;
181  CopyMapValuesToVector(mp, &v);
182  CopyMapValuesToSet(mp, &s);
183  std::set<int> s2;
184  CopyVectorToSet(v, &s2);
185  KALDI_ASSERT(s == s2);
186  }
187 }
188 
189 
191  for (int p = 0; p < 100; p++) {
192  std::vector<char*> vec;
193  int sz = Rand() % 3;
194  bool is_null = false;
195  for (int i = 0;i < sz;i++) {
196  vec.push_back(reinterpret_cast<char*>(static_cast<intptr_t>(Rand() % 2)));
197  if (vec.back() == NULL)
198  is_null = true;
199  }
200  KALDI_ASSERT(is_null == ContainsNullPointers(vec));
201  }
202 }
203 
205  for (int p = 0; p < 100; p++) {
206  std::vector<int> vec;
207  int sz = Rand() % 5;
208  for (int i = 0;i < sz;i++)
209  vec.push_back(Rand() % 4);
210  std::vector<int> vec2(vec), vec3(vec);
211  ReverseVector(&vec2);
212  ReverseVector(&vec2);
213  KALDI_ASSERT(vec2 == vec);
214  ReverseVector(&vec3);
215  for (size_t i = 0; i < vec.size(); i++)
216  KALDI_ASSERT(vec[i] == vec3[vec.size()-1-i]);
217  }
218 }
219 
221  for (int p = 0; p < 100; p++) {
222  std::vector<std::pair<int32, int16> > v;
223  std::map<int32, int16> m;
224  int sz = Rand() % 10;
225  for (size_t i = 0; i < sz; i++) {
226  int32 key = Rand() % 10;
227  int16 val = (Rand() % 5) - 2;
228  v.push_back(std::make_pair(key, val));
229  if (m.count(key) == 0) m[key] = val;
230  else
231  m[key] += val;
232  }
235  for (size_t i = 0; i < v.size(); i++) {
236  KALDI_ASSERT(v[i].second == m[v[i].first]);
237  KALDI_ASSERT(v[i].second != 0.0);
238  if (i > 0) KALDI_ASSERT(v[i].first > v[i-1].first);
239  }
240  for (std::map<int32, int16>::const_iterator iter = m.begin();
241  iter != m.end(); ++iter) {
242  if (iter->second != 0) {
243  size_t i;
244  for (i = 0; i < v.size(); i++)
245  if (v[i].first == iter->first) break;
246  KALDI_ASSERT(i != v.size()); // Or we didn't find this
247  // key in v.
248  }
249  }
250  }
251 }
252 
253 } // end namespace kaldi
254 
255 int main() {
256  using namespace kaldi;
257  TestIsSorted();
259  TestUniq();
260  TestSortAndUniq();
270  // CopyVectorToSet implicitly tested by last 2.
271  std::cout << "Test OK\n";
272 }
273 
274 
275 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void CopySetToVector(const std::set< T > &s, std::vector< T > *v)
Copies the elements of a set to a vector.
Definition: stl-utils.h:86
void CopyMapValuesToSet(const std::map< A, B > &m, std::set< B > *s)
Copies the values in a map to a set.
Definition: stl-utils.h:161
void CopyMapKeysToVector(const std::map< A, B > &m, std::vector< A > *v)
Copies the keys in a map to a vector.
Definition: stl-utils.h:126
void Uniq(std::vector< T > *vec)
Removes duplicate elements from a sorted list.
Definition: stl-utils.h:78
static void TestIsSorted()
int main()
kaldi::int32 int32
bool ContainsNullPointers(const std::vector< A *> &v)
Returns true if the vector of pointers contains NULL pointers.
Definition: stl-utils.h:197
void CopyVectorToSet(const std::vector< A > &v, std::set< A > *s)
Copies the contents of a vector to a set.
Definition: stl-utils.h:172
void SortAndUniq(std::vector< T > *vec)
Sorts and uniq&#39;s (removes duplicates) from a vector.
Definition: stl-utils.h:39
void TestCopyMapKeysToSet()
static void TestIsSortedAndUniq()
const size_t count
void TestCopyMapToVector()
static void TestUniq()
void CopyMapKeysToSet(const std::map< A, B > &m, std::set< A > *s)
Copies the keys in a map to a set.
Definition: stl-utils.h:150
struct rnnlm::@11::@12 n
void TestCopyMapValuesToVector()
void TestCopyMapValuesToSet()
void CopyMapValuesToVector(const std::map< A, B > &m, std::vector< B > *v)
Copies the values in a map to a vector.
Definition: stl-utils.h:138
void TestReverseVector()
void ReverseVector(std::vector< T > *vec)
Reverses the contents of a vector.
Definition: stl-utils.h:264
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
void TestMergePairVectorSumming()
bool IsSorted(const std::vector< T > &vec)
Returns true if the vector is sorted.
Definition: stl-utils.h:47
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void MergePairVectorSumming(std::vector< std::pair< I, F > > *vec)
For a vector of pair<I, F> where I is an integer and F a floating-point or integer type...
Definition: stl-utils.h:288
void TestCopySetToVector()
void TestContainsNullPointers()
bool IsSortedAndUniq(const std::vector< T > &vec)
Returns true if the vector is sorted and contains each element only once.
Definition: stl-utils.h:63
void CopyMapToVector(const std::map< A, B > &m, std::vector< std::pair< A, B > > *v)
Copies the (key, value) pairs in a map to a vector of pairs.
Definition: stl-utils.h:112
static void TestSortAndUniq()
void TestCopyMapKeysToVector()