Goby v2
base255.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/base_convert.h"
23 #include "goby/util/binary.h"
24 
25 #include <cassert>
26 #include <cstdlib>
27 #include <iomanip>
28 #include <iostream>
29 #include <sstream>
30 
31 void intprint(const std::string& s)
32 {
33  for (int i = s.size() - 1, n = 0; i >= n; --i)
34  std::cout << std::hex << int(s[i] & 0xFF) << " " << std::dec;
35  std::cout << std::endl;
36 }
37 
38 void test255(const std::string& in, bool output = true)
39 {
40  if (output)
41  {
42  std::cout << "in: ";
43  intprint(in);
44  }
45 
46  std::string out;
47 
48  goby::util::base_convert(in, &out, 256, 255);
49  if (output)
50  {
51  std::cout << "out: ";
52  intprint(out);
53  }
54 
55  std::string in2;
56 
57  goby::util::base_convert(out, &in2, 255, 256);
58  if (output)
59  {
60  std::cout << "in2: ";
61  intprint(in2);
62  }
63 
64  std::cout << "Encoded string is " << (int)out.size() - (int)in.size()
65  << " bytes larger than original string (" << in.size() << " bytes)" << std::endl;
66 
67  assert(in == in2);
68 }
69 
70 std::string randstring(int size)
71 {
72  std::string test(size, 0);
73  for (int i = 0; i < size; ++i) { test[i] = rand() % 256; }
74  return test;
75 }
76 
77 int main()
78 {
79  {
80  char chin[10] = {2, 1, 0, 0, 2, 2, 2, 0, 1, 1};
81  std::string in(chin, 10);
82  char chout[5] = {5, 0, 8, 2, 4};
83  std::string out;
84 
85  goby::util::base_convert(in, &out, 3, 9);
86 
87  intprint(in);
88  intprint(out);
89 
90  assert(out == std::string(chout, 5));
91  }
92 
93  test255("TOMcat");
94  test255(std::string(4, 0xff));
95 
96  test255(randstring(125));
97  test255(randstring(255));
98  test255(randstring(1500));
99  test255(randstring(15000), false);
100 
101  test255(goby::util::hex_decode("01020000"));
102 
103  test255(goby::util::hex_decode(
104  "080e100a300138016040680172400ecf026800793cac69341a8d46a3d16834da376bcf2f0f21fef979e3000000"
105  "d700eec35f2e82010000fcfce0e5e939e4984a6c62ff7a94584eb71cc471e1f53efd364000"));
106 
107  std::cout << "all tests passed" << std::endl;
108 
109  return 0;
110 }
Definition: as.cpp:24