base_converter_test.cc
Go to the documentation of this file.
2 #include <cstdlib>
3 #include <iostream>
4 #include <string>
5 
7 
8 void
9 ensure(int which, bool claim)
10 {
11  if (not claim)
12  std::exit(which);
13 }
14 
15 bool
16 hex2dec(std::string const& input, std::string const& wanted)
17 {
19 
20  try {
21  result = base_converter::hex_to_dec(input);
22  }
23  catch (...) {
24  return false;
25  }
26 
27  if (result == wanted)
28  return true;
29 
30  std::cerr << "Input: " << input << '\n'
31  << "Wanted: " << wanted << '\n'
32  << "Result: " << result << '\n';
33  return false;
34 }
35 
36 bool
37 dec2hex(std::string const& input, std::string const& wanted)
38 {
40 
41  try {
42  result = base_converter::dec_to_hex(input);
43  }
44  catch (...) {
45  return false;
46  }
47 
48  if (result == wanted)
49  return true;
50 
51  std::cerr << "Input: " << input << '\n'
52  << "Wanted: " << wanted << '\n'
53  << "Result: " << result << '\n';
54  return false;
55 }
56 
57 bool
58 bin2dec(std::string const& input, std::string const& wanted)
59 {
61 
62  try {
63  result = base_converter::bin_to_dec(input);
64  }
65  catch (...) {
66  return false;
67  }
68 
69  if (result == wanted)
70  return true;
71 
72  std::cerr << "Input: " << input << '\n'
73  << "Wanted: " << wanted << '\n'
74  << "Result: " << result << '\n';
75  return false;
76 }
77 
78 bool
79 dec2bin(std::string const& input, std::string const& wanted)
80 {
82 
83  try {
84  result = base_converter::dec_to_bin(input);
85  }
86  catch (...) {
87  return false;
88  }
89 
90  if (result == wanted)
91  return true;
92 
93  std::cerr << "Input: " << input << '\n'
94  << "Wanted: " << wanted << '\n'
95  << "Result: " << result << '\n';
96  return false;
97 }
98 
99 bool
100 any2any(std::string const& source_set,
101  std::string const& target_set,
102  std::string const& input,
103  std::string const& wanted)
104 {
106 
107  try {
108  base_converter converter(source_set, target_set);
109  result = converter.convert(input);
110  }
111  catch (...) {
112  return false;
113  }
114 
115  if (result == wanted)
116  return true;
117 
118  std::cerr << "SourceBaseSet: " << source_set << '\n'
119  << "TargetBaseSet: " << target_set << '\n'
120  << "Input: " << input << '\n'
121  << "Wanted: " << wanted << '\n'
122  << "Result: " << result << '\n';
123  return false;
124 }
125 
126 int
128 {
129  ensure(1, hex2dec("0", "0"));
130  ensure(2, hex2dec("A", "10"));
131  ensure(3, hex2dec("0F", "15"));
132  ensure(4, hex2dec("FF", "255"));
133  ensure(5, hex2dec("ABCD", "43981"));
134  ensure(6, hex2dec("ABCDEFABCDEFABCD", "12379814471884843981"));
135 
136  ensure(11, dec2hex("0", "0"));
137  ensure(12, dec2hex("010", "A"));
138  ensure(13, dec2hex("15", "F"));
139  ensure(14, dec2hex("255", "FF"));
140  ensure(15, dec2hex("43981", "ABCD"));
141  ensure(16, dec2hex("12379814471884843981", "ABCDEFABCDEFABCD"));
142 
143  ensure(21, bin2dec("0", "0"));
144  ensure(22, bin2dec("1", "1"));
145  ensure(23, bin2dec("0101", "5"));
146  ensure(24, bin2dec("100000010000101001011110110011", "541235123"));
147 
148  ensure(31, dec2bin("0", "0"));
149  ensure(32, dec2bin("1", "1"));
150  ensure(33, dec2bin("05", "101"));
151  ensure(34, dec2bin("541235123", "100000010000101001011110110011"));
152 
153  ensure(41, any2any("0123456789", "abcd", "0", "a"));
154  ensure(42, any2any("0123456789", "abcd", "4", "ba"));
155  ensure(43, any2any("0123456789", "abcd", "9", "cb"));
156 
157  ensure(51, !hex2dec("abc", ""));
158  ensure(52, !hex2dec("12XY", ""));
159  ensure(53, !bin2dec("102", ""));
160 
161  return 0;
162 
163 } // main()
bool dec2hex(std::string const &input, std::string const &wanted)
bool any2any(std::string const &source_set, std::string const &target_set, std::string const &input, std::string const &wanted)
static QCString result
std::string string
Definition: nybbler.cc:12
void ensure(int which, bool claim)
std::string convert(std::string value) const
int main()
static int input(void)
Definition: code.cpp:15695
bool dec2bin(std::string const &input, std::string const &wanted)
bool hex2dec(std::string const &input, std::string const &wanted)
bool bin2dec(std::string const &input, std::string const &wanted)