io-funcs-test.cc
Go to the documentation of this file.
1 // base/io-funcs-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 #include "base/io-funcs.h"
20 #include "base/kaldi-math.h"
21 
22 namespace kaldi {
23 
24 void UnitTestIo(bool binary) {
25  {
26  const char *filename = "tmpf";
27  std::ofstream outfile(filename, std::ios_base::out | std::ios_base::binary);
28  InitKaldiOutputStream(outfile, binary);
29  if (!binary) outfile << "\t";
30  int64 i1 = Rand() % 10000;
31  WriteBasicType(outfile, binary, i1);
32  uint16 i2 = Rand() % 10000;
33  WriteBasicType(outfile, binary, i2);
34  if (!binary) outfile << "\t";
35  char c = Rand();
36  WriteBasicType(outfile, binary, c);
37  if (!binary && Rand()%2 == 0) outfile << " \n";
38  std::vector<int32> vec1;
39  WriteIntegerVector(outfile, binary, vec1);
40  if (!binary && Rand()%2 == 0) outfile << " \n";
41  std::vector<uint16> vec2;
42  for (size_t i = 0; i < 10; i++) vec2.push_back(Rand()%100 - 10);
43  WriteIntegerVector(outfile, binary, vec2);
44  if (!binary) outfile << " \n";
45  std::vector<char> vec3;
46 
47  int32 size = RandInt(0, 10);
48  for (size_t i = 0; i < size; i++) vec3.push_back(Rand()%100);
49  WriteIntegerVector(outfile, binary, vec3);
50  std::vector<std::pair<int32, int32> > vec4;
51  WriteIntegerPairVector(outfile, binary, vec4);
52  if (!binary && Rand()%2 == 0) outfile << " \n";
53  std::vector<std::pair<uint16, uint16> > vec5;
54  for (size_t i = 0; i < size; i++) vec5.push_back(std::make_pair<uint16, uint16>(Rand()%100 - 10, Rand()%100 - 10));
55  WriteIntegerPairVector(outfile, binary, vec5);
56  if (!binary) outfile << " \n";
57  std::vector<std::pair<char, char> > vec6;
58  for (size_t i = 0; i < size; i++) vec6.push_back(std::make_pair<char, char>(Rand()%100, Rand()%100));
59  WriteIntegerPairVector(outfile, binary, vec6);
60  if (!binary && Rand()%2 == 0) outfile << " \n";
61  const char *token1 = "Hi";
62  WriteToken(outfile, binary, token1);
63  if (!binary) outfile << " \n";
64  std::string token2 = "There.";
65  WriteToken(outfile, binary, token2);
66  if (!binary && Rand()%2 == 0) outfile << " \n";
67  std::string token3 = "You.";
68  WriteToken(outfile, binary, token3);
69  if (!binary && Rand()%2 == 0) outfile << " ";
70  float f1 = RandUniform();
71  WriteBasicType(outfile, binary, f1);
72  if (!binary && Rand()%2 == 0) outfile << "\t";
73  float f2 = RandUniform();
74  WriteBasicType(outfile, binary, f2);
75  double d1 = RandUniform();
76  WriteBasicType(outfile, binary, d1);
77  if (!binary && Rand()%2 == 0) outfile << "\t";
78  double d2 = RandUniform();
79  WriteBasicType(outfile, binary, d2);
80  if (!binary && Rand()%2 == 0) outfile << "\t";
81  outfile.close();
82 
83  {
84  std::ifstream infile(filename, std::ios_base::in | std::ios_base::binary);
85  bool binary_in;
86  InitKaldiInputStream(infile, &binary_in);
87  int64 i1_in;
88  ReadBasicType(infile, binary_in, &i1_in);
89  KALDI_ASSERT(i1_in == i1);
90  uint16 i2_in;
91  ReadBasicType(infile, binary_in, &i2_in);
92  KALDI_ASSERT(i2_in == i2);
93  char c_in;
94  ReadBasicType(infile, binary_in, &c_in);
95  KALDI_ASSERT(c_in == c);
96  std::vector<int32> vec1_in;
97  ReadIntegerVector(infile, binary_in, &vec1_in);
98  KALDI_ASSERT(vec1_in == vec1);
99  std::vector<uint16> vec2_in;
100  ReadIntegerVector(infile, binary_in, &vec2_in);
101  KALDI_ASSERT(vec2_in == vec2);
102  std::vector<char> vec3_in;
103  ReadIntegerVector(infile, binary_in, &vec3_in);
104  KALDI_ASSERT(vec3_in == vec3);
105  std::vector<std::pair<int32, int32> > vec4_in;
106  ReadIntegerPairVector(infile, binary_in, &vec4_in);
107  KALDI_ASSERT(vec4_in == vec4);
108  std::vector<std::pair<uint16, uint16> > vec5_in;
109  ReadIntegerPairVector(infile, binary_in, &vec5_in);
110  KALDI_ASSERT(vec5_in == vec5);
111  std::vector<std::pair<char, char> > vec6_in;
112  ReadIntegerPairVector(infile, binary_in, &vec6_in);
113  KALDI_ASSERT(vec6_in == vec6);
114  std::string token1_in, token2_in;
115  KALDI_ASSERT(Peek(infile, binary_in) == static_cast<int>(*token1));
116  KALDI_ASSERT(PeekToken(infile, binary_in) == static_cast<int>(*token1));
117  // Note:
118  // the stuff with skipping over '<' is tested in ../util/kaldi-io-test.cc,
119  // since we need to make sure it works with pipes.
120  ReadToken(infile, binary_in, &token1_in);
121  KALDI_ASSERT(token1_in == std::string(token1));
122  ReadToken(infile, binary_in, &token2_in);
123  KALDI_ASSERT(token2_in == std::string(token2));
124  if (Rand() % 2 == 0)
125  ExpectToken(infile, binary_in, token3.c_str());
126  else
127  ExpectToken(infile, binary_in, token3);
128  float f1_in; // same type.
129  ReadBasicType(infile, binary_in, &f1_in);
130  AssertEqual(f1_in, f1);
131  double f2_in; // wrong type.
132  ReadBasicType(infile, binary_in, &f2_in);
133  AssertEqual(f2_in, f2);
134  double d1_in; // same type.
135  ReadBasicType(infile, binary_in, &d1_in);
136  AssertEqual(d1_in, d1);
137  float d2_in; // wrong type.
138  ReadBasicType(infile, binary_in, &d2_in);
139  AssertEqual(d2_in, d2);
140  KALDI_ASSERT(Peek(infile, binary_in) == -1);
141  KALDI_ASSERT(PeekToken(infile, binary_in) == -1);
142  }
143  unlink(filename);
144  }
145 }
146 
147 
148 
149 } // end namespace kaldi.
150 
151 int main() {
152  using namespace kaldi;
153  for (size_t i = 0; i < 10; i++) {
154  UnitTestIo(false);
155  UnitTestIo(true);
156  }
157  KALDI_ASSERT(1); // just to check that KALDI_ASSERT does not fail for 1.
158  return 0;
159 }
160 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool InitKaldiInputStream(std::istream &is, bool *binary)
Initialize an opened stream for reading by detecting the binary header and.
Definition: io-funcs-inl.h:306
void WriteIntegerPairVector(std::ostream &os, bool binary, const std::vector< std::pair< T, T > > &v)
Function for writing STL vectors of pairs of integer types.
Definition: io-funcs-inl.h:93
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
kaldi::int32 int32
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
void UnitTestIo(bool binary)
void ReadIntegerPairVector(std::istream &is, bool binary, std::vector< std::pair< T, T > > *v)
Function for reading STL vector of pairs of integer types.
Definition: io-funcs-inl.h:131
int Peek(std::istream &is, bool binary)
Peek consumes whitespace (if binary == false) and then returns the peek() value of the stream...
Definition: io-funcs.cc:145
int main()
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
Definition: io-funcs-inl.h:232
void ExpectToken(std::istream &is, bool binary, const char *token)
ExpectToken tries to read in the given token, and throws an exception on failure. ...
Definition: io-funcs.cc:191
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
int PeekToken(std::istream &is, bool binary)
PeekToken will return the first character of the next token, or -1 if end of file.
Definition: io-funcs.cc:170
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
Definition: io-funcs-inl.h:198
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34
void InitKaldiOutputStream(std::ostream &os, bool binary)
InitKaldiOutputStream initializes an opened stream for writing by writing an optional binary header a...
Definition: io-funcs-inl.h:291
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95