Goby v2
hex_codec.cpp
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 //
5 //
6 // This file is part of the Goby Underwater Autonomy Project Binaries
7 // ("The Goby Binaries").
8 //
9 // The Goby Binaries are free software: you can redistribute them and/or modify
10 // them under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // The Goby Binaries are distributed in the hope that they will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
21 
22 #include "goby/util/binary.h"
23 
24 #include <iomanip>
25 #include <iostream>
26 #include <sstream>
27 
28 int main()
29 {
30  using goby::util::hex_decode;
31  using goby::util::hex_encode;
32 
33  {
34  std::cout << "testing lowercase even" << std::endl;
35 
36  std::stringstream ss;
37  for (int i = 0; i <= 255; ++i) ss << std::setfill('0') << std::setw(2) << std::hex << i;
38 
39  std::string hex1 = ss.str();
40  std::string hex2;
41 
42  std::string bytes;
43  hex_decode(hex1, &bytes);
44  hex_encode(bytes, &hex2);
45 
46  std::cout << "hex1: " << hex1 << std::endl;
47  std::cout << "hex2: " << hex2 << std::endl;
48  assert(hex1 == hex2.substr(hex2.size() - hex1.size()));
49  }
50  {
51  std::cout << "testing uppercase even" << std::endl;
52 
53  std::stringstream ss;
54  for (int i = 0; i <= 255; ++i)
55  ss << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << i;
56 
57  std::string hex1 = ss.str();
58  std::string hex2;
59 
60  std::string bytes;
61  hex_decode(hex1, &bytes);
62  hex_encode(bytes, &hex2, true);
63 
64  std::cout << "hex1: " << hex1 << std::endl;
65  std::cout << "hex2: " << hex2 << std::endl;
66  assert(hex1 == hex2.substr(hex2.size() - hex1.size()));
67  }
68 
69  {
70  std::cout << "testing lowercase odd" << std::endl;
71 
72  std::stringstream ss;
73  for (int i = 0; i <= 255; ++i) ss << std::setfill('0') << std::setw(2) << std::hex << i;
74 
75  ss << "d";
76 
77  std::string hex1 = ss.str();
78  std::string hex2;
79 
80  std::string bytes;
81  hex_decode(hex1, &bytes);
82  hex_encode(bytes, &hex2);
83 
84  std::cout << "hex1: " << hex1 << std::endl;
85  std::cout << "hex2: " << hex2 << std::endl;
86  assert(hex1 == hex2.substr(hex2.size() - hex1.size()));
87  }
88  {
89  std::cout << "testing uppercase odd" << std::endl;
90 
91  std::stringstream ss;
92  for (int i = 0; i <= 255; ++i)
93  ss << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << i;
94  ss << "D";
95 
96  std::string hex1 = ss.str();
97  std::string hex2;
98 
99  std::string bytes;
100  hex_decode(hex1, &bytes);
101  hex_encode(bytes, &hex2, true);
102 
103  std::cout << "hex1: " << hex1 << std::endl;
104  std::cout << "hex2: " << hex2 << std::endl;
105  assert(hex1 == hex2.substr(hex2.size() - hex1.size()));
106  }
107 
108  std::cout << "all tests passed" << std::endl;
109 
110  return 0;
111 }