Yabin Li
2023-11-07 702ec03ad89d5c62e97eed770a6882d6412f8d58
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
// util/simple-options-test.cc
 
// Copyright 2013  Tanel Alumae, Tallinn University of Technology
 
// 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.
#include "util/simple-options.h"
 
namespace kaldi {
 
 
void UnitTestSimpleOptions() {
  std::string str="default_for_str";
  int32 num = 1;
  uint32 unum = 2;
  float realnum = 0.1;
  bool flag = false;
  bool rval;
  SimpleOptions so;
 
  so.Register("num", &num, "Description of num");
  so.Register("unum", &unum, "Description of unum");
  so.Register("str", &str, "Description of str");
  so.Register("flag", &flag, "Description of flag");
  so.Register("realnum", &realnum, "Description of realnum");
 
  rval = so.SetOption("num", 42);
  KALDI_ASSERT(rval);
  so.SetOption("unum", static_cast<uint32>(43));
  KALDI_ASSERT(rval);
  rval = so.SetOption("str", (std::string)"foo");
  KALDI_ASSERT(rval);
  rval = so.SetOption("flag", false);
  KALDI_ASSERT(rval);
 
  KALDI_ASSERT(num == 42);
  KALDI_ASSERT(unum == 43);
  KALDI_ASSERT(str == "foo");
  KALDI_ASSERT(flag == false);
 
  rval = so.SetOption("str", "foo2");
  KALDI_ASSERT(rval);
  KALDI_ASSERT(str == "foo2");
 
  // test automatic conversion between int and uint
  rval = so.SetOption("unum", 44);
  KALDI_ASSERT(rval);
  KALDI_ASSERT(unum == 44);
 
  // test automatic conversion between float and double
  rval = so.SetOption("realnum", static_cast<float>(0.2));
  KALDI_ASSERT(rval);
  KALDI_ASSERT(realnum - 0.2 < 0.000001);
  rval = so.SetOption("realnum", static_cast<double>(0.3));
  KALDI_ASSERT(rval);
  KALDI_ASSERT(realnum - 0.3 < 0.000001);
 
  SimpleOptions::OptionType type;
  rval = so.GetOptionType("num", &type);
  KALDI_ASSERT(rval);
  KALDI_ASSERT(type == SimpleOptions::kInt32);
 
  rval = so.GetOptionType("xxxx", &type);
  KALDI_ASSERT(rval == false);
}
 
 
}  // end namespace kaldi.
 
int main() {
  using namespace kaldi;
  UnitTestSimpleOptions();
  return 0;
}