hash-list-test.cc
Go to the documentation of this file.
1 // util/hash-list-test.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2013 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 "util/hash-list.h"
23 #include <map> // for baseline.
24 #include <cstdlib>
25 #include <iostream>
26 
27 namespace kaldi {
28 
29 template<class Int, class T> void TestHashList() {
30  typedef typename HashList<Int, T>::Elem Elem;
31 
32  HashList<Int, T> hash;
33  hash.SetSize(200); // must be called before use.
34  std::map<Int, T> m1;
35  for (size_t j = 0; j < 50; j++) {
36  Int key = Rand() % 200;
37  T val = Rand() % 50;
38  m1[key] = val;
39  Elem *e = hash.Find(key);
40  if (e) e->val = val;
41  else hash.Insert(key, val);
42  }
43 
44 
45  std::map<Int, T> m2;
46 
47  for (int i = 0; i < 100; i++) {
48  m2.clear();
49  for (typename std::map<Int, T>::const_iterator iter = m1.begin();
50  iter != m1.end();
51  iter++) {
52  m2[iter->first + 1] = iter->second;
53  }
54  std::swap(m1, m2);
55 
56  Elem *h = hash.Clear(), *tmp;
57 
58  hash.SetSize(100 + Rand() % 100); // note, SetSize is relatively cheap
59  // operation as long as we are not increasing the size more than it's ever
60  // previously been increased to.
61 
62  for (; h != NULL; h = tmp) {
63  hash.Insert(h->key + 1, h->val);
64  tmp = h->tail;
65  hash.Delete(h); // think of this like calling delete.
66  }
67 
68  // Now make sure h and m2 are the same.
69  const Elem *list = hash.GetList();
70  size_t count = 0;
71  for (; list != NULL; list = list->tail, count++) {
72  KALDI_ASSERT(m1[list->key] == list->val);
73  }
74 
75  for (size_t j = 0; j < 10; j++) {
76  Int key = Rand() % 200;
77  bool found_m1 = (m1.find(key) != m1.end());
78  if (found_m1) m1[key];
79  Elem *e = hash.Find(key);
80  KALDI_ASSERT((e != NULL) == found_m1);
81  if (found_m1)
82  KALDI_ASSERT(m1[key] == e->val);
83  }
84 
85  KALDI_ASSERT(m1.size() == count);
86  }
87 }
88 
89 
90 
91 
92 } // end namespace kaldi
93 
94 
95 
96 int main() {
97  using namespace kaldi;
98  for (size_t i = 0;i < 3;i++) {
99  TestHashList<int, unsigned int>();
100  TestHashList<unsigned int, int>();
101  TestHashList<int16, int32>();
102  TestHashList<int16, int32>();
103  TestHashList<char, unsigned char>();
104  TestHashList<unsigned char, int>();
105  }
106  std::cout << "Test OK.\n";
107 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Elem * Insert(I key, T val)
Insert inserts a new element into the hashtable/stored list.
void TestHashList()
void swap(basic_filebuf< CharT, Traits > &x, basic_filebuf< CharT, Traits > &y)
const size_t count
void SetSize(size_t sz)
SetSize tells the object how many hash buckets to allocate (should typically be at least twice the nu...
Definition: hash-list-inl.h:37
const Elem * GetList() const
Gives the head of the current list to the user.
Definition: hash-list-inl.h:61
int main()
Elem * Clear()
Clears the hash and gives the head of the current list to the user; ownership is transferred to the u...
Definition: hash-list-inl.h:46
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Elem * Find(I key)
Find tries to find this element in the current list using the hashtable.
Definition: hash-list-inl.h:72
void Delete(Elem *e)
Think of this like delete().
Definition: hash-list-inl.h:66