kaldi-error-test.cc
Go to the documentation of this file.
1 // base/kaldi-error-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 #include "base/kaldi-common.h"
21 
22 // testing that we get the stack trace.
23 namespace kaldi {
24 
25 void MyFunction2() { KALDI_ERR << "Ignore this error"; }
26 
27 void MyFunction1() { MyFunction2(); }
28 
29 void UnitTestError() {
30  {
31  std::cerr << "Ignore next error:\n";
32  MyFunction1();
33  }
34 }
35 
36 void VerifySymbolRange(const std::string &trace, const bool want_found,
37  const std::string &want_symbol) {
38  size_t begin, end;
39  const bool found = internal::LocateSymbolRange(trace, &begin, &end);
40  if (found != want_found) {
41  KALDI_ERR << "Found mismatch, got " << found << " want " << want_found;
42  }
43  if (!found) {
44  return;
45  }
46  const std::string symbol = trace.substr(begin, end - begin);
47  if (symbol != want_symbol) {
48  KALDI_ERR << "Symbol mismatch, got " << symbol << " want " << want_symbol;
49  }
50 }
51 
53  VerifySymbolRange("", false, "");
55  R"TRACE(./kaldi-error-test(_ZN5kaldi13UnitTestErrorEv+0xb) [0x804965d])TRACE",
56  true, "_ZN5kaldi13UnitTestErrorEv");
57  // It is ok thread_start is not found because it is a C symbol.
59  R"TRACE(31 libsystem_pthread.dylib 0x00007fff6fe4e40d thread_start + 13)TRACE",
60  false, "");
62  R"TRACE(0 server 0x000000010f67614d _ZNK5kaldi13MessageLogger10LogMessageEv + 813)TRACE",
63  true, "_ZNK5kaldi13MessageLogger10LogMessageEv");
65  R"TRACE(29 libsystem_pthread.dylib 0x00007fff6fe4f2eb _pthread_body + 126)TRACE",
66  true, "_pthread_body");
67 }
68 
69 } // namespace kaldi
70 
71 int main() {
73 
74  kaldi::SetProgramName("/foo/bar/kaldi-error-test");
75  try {
77  KALDI_ASSERT(0); // should not happen.
78  exit(1);
79  } catch (kaldi::KaldiFatalError &e) {
80  std::cout << "The error we generated was: '" << e.KaldiMessage() << "'\n";
81  }
82 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void SetProgramName(const char *basename)
Called by ParseOptions to set base name (no directory) of the executing program.
Definition: kaldi-error.cc:50
Kaldi fatal runtime error exception.
Definition: kaldi-error.h:89
void TestLocateSymbolRange()
void MyFunction1()
void VerifySymbolRange(const std::string &trace, const bool want_found, const std::string &want_symbol)
const char * KaldiMessage() const
Returns the Kaldi error message logged by KALDI_ERR.
Definition: kaldi-error.h:101
void MyFunction2()
#define KALDI_ERR
Definition: kaldi-error.h:147
void UnitTestError()
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int main()
bool LocateSymbolRange(const std::string &trace_name, size_t *begin, size_t *end)
Definition: kaldi-error.cc:79