Blame view
src/util/kaldi-io-test.cc
12.9 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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
// util/kaldi-io-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. #ifndef _MSC_VER #include <unistd.h> #endif #include "base/io-funcs.h" #include "util/kaldi-io.h" #include "base/kaldi-math.h" #include "base/kaldi-utils.h" namespace kaldi { void UnitTestClassifyRxfilename() { KALDI_ASSERT(ClassifyRxfilename("") == kStandardInput); KALDI_ASSERT(ClassifyRxfilename(" ") == kNoInput); KALDI_ASSERT(ClassifyRxfilename(" a ") == kNoInput); KALDI_ASSERT(ClassifyRxfilename("a ") == kNoInput); KALDI_ASSERT(ClassifyRxfilename("a") == kFileInput); KALDI_ASSERT(ClassifyRxfilename("-") == kStandardInput); KALDI_ASSERT(ClassifyRxfilename("b|") == kPipeInput); KALDI_ASSERT(ClassifyRxfilename("|b") == kNoInput); KALDI_ASSERT(ClassifyRxfilename("b c|") == kPipeInput); KALDI_ASSERT(ClassifyRxfilename(" b c|") == kPipeInput); KALDI_ASSERT(ClassifyRxfilename("a b c:123") == kOffsetFileInput); KALDI_ASSERT(ClassifyRxfilename("a b c:3") == kOffsetFileInput); KALDI_ASSERT(ClassifyRxfilename("a b c:") == kFileInput); KALDI_ASSERT(ClassifyRxfilename("a b c/3") == kFileInput); KALDI_ASSERT(ClassifyRxfilename("ark,s,cs:a b c") == kNoInput); KALDI_ASSERT(ClassifyRxfilename("scp:a b c") == kNoInput); } void UnitTestClassifyWxfilename() { KALDI_ASSERT(ClassifyWxfilename("") == kStandardOutput); KALDI_ASSERT(ClassifyWxfilename(" ") == kNoOutput); KALDI_ASSERT(ClassifyWxfilename(" a ") == kNoOutput); KALDI_ASSERT(ClassifyWxfilename("a ") == kNoOutput); KALDI_ASSERT(ClassifyWxfilename("a") == kFileOutput); KALDI_ASSERT(ClassifyWxfilename("-") == kStandardOutput); KALDI_ASSERT(ClassifyWxfilename("b|") == kNoOutput); KALDI_ASSERT(ClassifyWxfilename("|b") == kPipeOutput); KALDI_ASSERT(ClassifyWxfilename("| b ") == kPipeOutput); KALDI_ASSERT(ClassifyWxfilename("b c|") == kNoOutput); KALDI_ASSERT(ClassifyWxfilename("a b c:123") == kNoOutput); KALDI_ASSERT(ClassifyWxfilename("ark,s,cs:a b c") == kNoOutput); KALDI_ASSERT(ClassifyWxfilename("scp:a b c") == kNoOutput); KALDI_ASSERT(ClassifyWxfilename("a b c:3") == kNoOutput); KALDI_ASSERT(ClassifyWxfilename("a b c:") == kFileOutput); KALDI_ASSERT(ClassifyWxfilename("a b c/3") == kFileOutput); } void UnitTestIoNew(bool binary) { { const char *filename = "tmpf"; Output ko(filename, binary); std::ostream &outfile = ko.Stream(); if (!binary) outfile << "\t"; int64 i1 = Rand() % 10000; WriteBasicType(outfile, binary, i1); uint16 i2 = Rand() % 10000; WriteBasicType(outfile, binary, i2); if (!binary) outfile << "\t"; char c = Rand(); WriteBasicType(outfile, binary, c); if (!binary && Rand()%2 == 0) outfile << " "; std::vector<int32> vec1; WriteIntegerVector(outfile, binary, vec1); if (!binary && Rand()%2 == 0) outfile << " "; std::vector<uint16> vec2; for (size_t i = 0; i < 10; i++) vec2.push_back(Rand()%100 - 10); WriteIntegerVector(outfile, binary, vec2); if (!binary) outfile << " "; std::vector<char> vec3; for (size_t i = 0; i < 10; i++) vec3.push_back(Rand()%100); WriteIntegerVector(outfile, binary, vec3); if (!binary && Rand()%2 == 0) outfile << " "; const char *token1 = "Hi"; WriteToken(outfile, binary, token1); if (!binary) outfile << " "; std::string token2 = "There."; WriteToken(outfile, binary, token2); if (!binary && Rand()%2 == 0) outfile << " "; std::string token3 = "You."; WriteToken(outfile, binary, token3); if (!binary && Rand()%2 == 0) outfile << " "; float f1 = RandUniform(); WriteBasicType(outfile, binary, f1); if (!binary && Rand()%2 == 0) outfile << "\t"; float f2 = RandUniform(); WriteBasicType(outfile, binary, f2); double d1 = RandUniform(); WriteBasicType(outfile, binary, d1); if (!binary && Rand()%2 == 0) outfile << "\t"; double d2 = RandUniform(); WriteBasicType(outfile, binary, d2); if (!binary && Rand()%2 == 0) outfile << "\t"; ko.Close(); { bool binary_in; Input ki(filename, &binary_in); std::istream &infile = ki.Stream(); int64 i1_in; ReadBasicType(infile, binary_in, &i1_in); KALDI_ASSERT(i1_in == i1); uint16 i2_in; ReadBasicType(infile, binary_in, &i2_in); KALDI_ASSERT(i2_in == i2); char c_in; ReadBasicType(infile, binary_in, &c_in); KALDI_ASSERT(c_in == c); std::vector<int32> vec1_in; ReadIntegerVector(infile, binary_in, &vec1_in); KALDI_ASSERT(vec1_in == vec1); std::vector<uint16> vec2_in; ReadIntegerVector(infile, binary_in, &vec2_in); KALDI_ASSERT(vec2_in == vec2); std::vector<char> vec3_in; ReadIntegerVector(infile, binary_in, &vec3_in); KALDI_ASSERT(vec3_in == vec3); std::string token1_in, token2_in; KALDI_ASSERT(Peek(infile, binary_in) == static_cast<int>(*token1)); ReadToken(infile, binary_in, &token1_in); KALDI_ASSERT(token1_in == (std::string)token1); ReadToken(infile, binary_in, &token2_in); KALDI_ASSERT(token2_in == token2); if (Rand() % 2 == 0) ExpectToken(infile, binary_in, token3.c_str()); else ExpectToken(infile, binary_in, token3); float f1_in; // same type. ReadBasicType(infile, binary_in, &f1_in); AssertEqual(f1_in, f1); double f2_in; // wrong type. ReadBasicType(infile, binary_in, &f2_in); AssertEqual(f2_in, f2); double d1_in; // same type. ReadBasicType(infile, binary_in, &d1_in); AssertEqual(d1_in, d1); float d2_in; // wrong type. ReadBasicType(infile, binary_in, &d2_in); AssertEqual(d2_in, d2); KALDI_ASSERT(Peek(infile, binary_in) == -1); } unlink(filename); } } void UnitTestIoPipe(bool binary) { // This is as UnitTestIoNew except with different filenames. { #if defined(_MSC_VER) && !defined(KALDI_CYGWIN_COMPAT) // self-invocation on Windows that emulates cat(1) const char *filename_out = "|kaldi-io-test cat > tmpf.gz", *filename_in = "kaldi-io-test cat tmpf.gz|"; #else const char *filename_out = "|gzip -c > tmpf.gz", *filename_in = "gunzip -c tmpf.gz |"; #endif Output ko(filename_out, binary); std::ostream &outfile = ko.Stream(); if (!binary) outfile << "\t"; int64 i1 = Rand() % 10000; WriteBasicType(outfile, binary, i1); uint16 i2 = Rand() % 10000; WriteBasicType(outfile, binary, i2); if (!binary) outfile << "\t"; char c = Rand(); WriteBasicType(outfile, binary, c); if (!binary && Rand()%2 == 0) outfile << " "; std::vector<int32> vec1; WriteIntegerVector(outfile, binary, vec1); if (!binary && Rand()%2 == 0) outfile << " "; std::vector<uint16> vec2; for (size_t i = 0; i < 10; i++) vec2.push_back(Rand()%100 - 10); WriteIntegerVector(outfile, binary, vec2); if (!binary) outfile << " "; WriteToken(outfile, binary, "<foo>"); std::vector<char> vec3; for (size_t i = 0; i < 10; i++) vec3.push_back(Rand()%100); WriteIntegerVector(outfile, binary, vec3); if (!binary && Rand()%2 == 0) outfile << " "; const char *token1 = "Hi"; WriteToken(outfile, binary, token1); if (!binary) outfile << " "; std::string token2 = "There."; WriteToken(outfile, binary, token2); if (!binary && Rand()%2 == 0) outfile << " "; std::string token3 = "You."; WriteToken(outfile, binary, token3); if (!binary && Rand()%2 == 0) outfile << " "; float f1 = RandUniform(); WriteBasicType(outfile, binary, f1); if (!binary && Rand()%2 == 0) outfile << "\t"; float f2 = RandUniform(); WriteBasicType(outfile, binary, f2); double d1 = RandUniform(); WriteBasicType(outfile, binary, d1); if (!binary && Rand()%2 == 0) outfile << "\t"; double d2 = RandUniform(); WriteBasicType(outfile, binary, d2); if (!binary && Rand()%2 == 0) outfile << "\t"; bool ans = ko.Close(); KALDI_ASSERT(ans); #ifndef _MSC_VER Sleep(1); // This test does not work without this sleep: // seems to be some kind of file-system latency. #endif { bool binary_in; Input ki(filename_in, &binary_in); std::istream &infile = ki.Stream(); int64 i1_in; ReadBasicType(infile, binary_in, &i1_in); KALDI_ASSERT(i1_in == i1); uint16 i2_in; ReadBasicType(infile, binary_in, &i2_in); KALDI_ASSERT(i2_in == i2); char c_in; ReadBasicType(infile, binary_in, &c_in); KALDI_ASSERT(c_in == c); std::vector<int32> vec1_in; ReadIntegerVector(infile, binary_in, &vec1_in); KALDI_ASSERT(vec1_in == vec1); std::vector<uint16> vec2_in; ReadIntegerVector(infile, binary_in, &vec2_in); KALDI_ASSERT(vec2_in == vec2); std::vector<char> vec3_in; KALDI_ASSERT(PeekToken(infile, binary_in) == static_cast<int>('f')); ExpectToken(infile, binary_in, "<foo>"); ReadIntegerVector(infile, binary_in, &vec3_in); KALDI_ASSERT(vec3_in == vec3); std::string token1_in, token2_in; KALDI_ASSERT(Peek(infile, binary_in) == static_cast<int>(*token1)); ReadToken(infile, binary_in, &token1_in); KALDI_ASSERT(token1_in == (std::string)token1); ReadToken(infile, binary_in, &token2_in); KALDI_ASSERT(token2_in == token2); if (Rand() % 2 == 0) ExpectToken(infile, binary_in, token3.c_str()); else ExpectToken(infile, binary_in, token3); float f1_in; // same type. ReadBasicType(infile, binary_in, &f1_in); AssertEqual(f1_in, f1); double f2_in; // wrong type. ReadBasicType(infile, binary_in, &f2_in); AssertEqual(f2_in, f2); double d1_in; // same type. ReadBasicType(infile, binary_in, &d1_in); AssertEqual(d1_in, d1); float d2_in; // wrong type. ReadBasicType(infile, binary_in, &d2_in); AssertEqual(d2_in, d2); KALDI_ASSERT(Peek(infile, binary_in) == -1); } } unlink("tmpf.txt"); unlink("tmpf.gz"); } void UnitTestIoStandard() { /* Don't do the the following part because it requires to pipe from an empty file, for it to not hang. { Input inp("", NULL); // standard input. KALDI_ASSERT(inp.Stream().get() == -1); } { Input inp("-", NULL); // standard input. KALDI_ASSERT(inp.Stream().get() == -1); }*/ { std::cout << "Should see: foo "; Output out("", false); out.Stream() << "foo "; } { std::cout << "Should see: bar "; Output out("-", false); out.Stream() << "bar "; } } // This is Windows-specific. void UnitTestNativeFilename() { #ifdef KALDI_CYGWIN_COMPAT extern std::string MapCygwinPath(const std::string &filename); KALDI_ASSERT(MapCygwinPath("") == ""); KALDI_ASSERT(MapCygwinPath(".") == "."); KALDI_ASSERT(MapCygwinPath("..") == ".."); KALDI_ASSERT(MapCygwinPath("/dev/null")[0] != '/'); KALDI_ASSERT(MapCygwinPath("/tmp")[1] == ':'); KALDI_ASSERT(MapCygwinPath("/tmp/")[1] == ':'); KALDI_ASSERT(MapCygwinPath("/tmp/foo")[1] == ':'); KALDI_ASSERT(MapCygwinPath("/cygdrive/c") == "c:/"); KALDI_ASSERT(MapCygwinPath("/cygdrive/c/") == "c:/"); KALDI_ASSERT(MapCygwinPath("/cygdrive/c/foo") == "c:/foo"); #endif } } // end namespace kaldi. #if defined(_MSC_VER) && !defined(KALDI_CYGWIN_COMPAT) // Windows has no cat! There is probably no suitable tool to test popen I/O on // Windows, so we emulate a lame version of cat(1). static int TinyCat(int argc, const char** argv) { const char* name_in = argc > 0 && strcmp(argv[0], "-") ? argv[0] : NULL; int fd_in = name_in ? _open(name_in, _O_RDONLY) : _fileno(stdin); if (fd_in < 0) return 1; int fd_out = _fileno(stdout); _setmode(fd_in, _O_BINARY); _setmode(fd_out, _O_BINARY); char buffer[100]; int last_read; while ((last_read = _read(fd_in, buffer, sizeof(buffer))) > 0) _write(fd_out, buffer, last_read); if (name_in) _close(fd_in); return 0; } #endif int main(int argc, const char** argv) { using namespace kaldi; #if defined(_MSC_VER) && !defined(KALDI_CYGWIN_COMPAT) if (argc > 1 && strcmp(argv[1], "cat") == 0) return TinyCat(argc - 2, argv + 2); #endif UnitTestNativeFilename(); UnitTestIoNew(false); UnitTestIoNew(true); UnitTestIoPipe(true); UnitTestIoPipe(false); UnitTestIoStandard(); UnitTestClassifyRxfilename(); UnitTestClassifyWxfilename(); KALDI_ASSERT(1); // just wanted to check that KALDI_ASSERT does not fail // for 1. return 0; } |