1 | /* |
---|---|

2 | Copyright (c) Marshall Clow 2011-2012. |

3 | |

4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |

5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |

6 | |

7 | For more information, see http://www.boost.org |

8 | |

9 | Test non-string cases; vector and list |

10 | */ |

11 | |

12 | #include <boost/config.hpp> |

13 | #include <boost/algorithm/hex.hpp> |

14 | |

15 | #define BOOST_TEST_MAIN |

16 | #include <boost/test/unit_test.hpp> |

17 | |

18 | #include <string> |

19 | #include <iostream> |

20 | #include <deque> |

21 | #include <list> |

22 | |

23 | |

24 | const char *tohex [] = { |

25 | "", |

26 | "a", |

27 | "\001", |

28 | "12", |

29 | "asdfadsfsad", |

30 | "01234567890ABCDEF", |

31 | NULL // End of the list |

32 | }; |

33 | |

34 | void test_to_hex () { |

35 | for ( const char **p = tohex; *p; p++ ) { |

36 | std::deque<char> arg, argh; |

37 | std::list<char> one, two, three; |

38 | arg.assign ( *p, *p + strlen (*p)); |

39 | boost::algorithm::hex ( *p, std::back_inserter ( one )); |

40 | boost::algorithm::hex ( arg, std::back_inserter ( two )); |

41 | boost::algorithm::hex ( arg.begin (), arg.end (), std::back_inserter ( three )); |

42 | BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |

43 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |

44 | |

45 | std::copy ( one.begin (), one.end (), std::back_inserter ( argh )); |

46 | one.clear (); two.clear (); three.clear (); |

47 | |

48 | // boost::algorithm::unhex ( argh.c_str (), std::back_inserter ( one )); |

49 | boost::algorithm::unhex ( argh, std::back_inserter ( two )); |

50 | boost::algorithm::unhex ( argh.begin (), argh.end (), std::back_inserter ( three )); |

51 | // BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |

52 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |

53 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.begin ())); |

54 | } |

55 | |

56 | // Again, with a front_inserter |

57 | for ( const char **p = tohex; *p; p++ ) { |

58 | std::deque<char> arg, argh; |

59 | std::list<char> one, two, three; |

60 | arg.assign ( *p, *p + strlen (*p)); |

61 | boost::algorithm::hex ( *p, std::front_inserter ( one )); |

62 | boost::algorithm::hex ( arg, std::front_inserter ( two )); |

63 | boost::algorithm::hex ( arg.begin (), arg.end (), std::front_inserter ( three )); |

64 | BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |

65 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |

66 | |

67 | // Copy, reversing |

68 | std::copy ( one.begin (), one.end (), std::front_inserter ( argh )); |

69 | one.clear (); two.clear (); three.clear (); |

70 | |

71 | // boost::algorithm::unhex ( argh.c_str (), std::front_inserter ( one )); |

72 | boost::algorithm::unhex ( argh, std::front_inserter ( two )); |

73 | boost::algorithm::unhex ( argh.begin (), argh.end (), std::front_inserter ( three )); |

74 | // BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |

75 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |

76 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.rbegin ())); // reverse |

77 | } |

78 | } |

79 | |

80 | const char *fromhex [] = { |

81 | "20", |

82 | "2122234556FF", |

83 | NULL // End of the list |

84 | }; |

85 | |

86 | |

87 | void test_from_hex_success () { |

88 | for ( const char **p = fromhex; *p; p++ ) { |

89 | std::deque<char> arg, argh; |

90 | std::list<char> one, two, three; |

91 | arg.assign ( *p, *p + strlen (*p)); |

92 | boost::algorithm::unhex ( *p, std::back_inserter ( one )); |

93 | boost::algorithm::unhex ( arg, std::back_inserter ( two )); |

94 | boost::algorithm::unhex ( arg.begin (), arg.end (), std::back_inserter ( three )); |

95 | BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |

96 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |

97 | |

98 | std::copy ( one.begin (), one.end (), std::back_inserter ( argh )); |

99 | one.clear (); two.clear (); three.clear (); |

100 | |

101 | // boost::algorithm::hex ( argh.c_str (), std::back_inserter ( one )); |

102 | boost::algorithm::hex ( argh, std::back_inserter ( two )); |

103 | boost::algorithm::hex ( argh.begin (), argh.end (), std::back_inserter ( three )); |

104 | // BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |

105 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |

106 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.begin ())); |

107 | } |

108 | |

109 | // Again, with a front_inserter |

110 | for ( const char **p = fromhex; *p; p++ ) { |

111 | std::deque<char> arg, argh; |

112 | std::list<char> one, two, three; |

113 | arg.assign ( *p, *p + strlen (*p)); |

114 | boost::algorithm::unhex ( *p, std::front_inserter ( one )); |

115 | boost::algorithm::unhex ( arg, std::front_inserter ( two )); |

116 | boost::algorithm::unhex ( arg.begin (), arg.end (), std::front_inserter ( three )); |

117 | BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |

118 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |

119 | |

120 | // Copy, reversing |

121 | std::copy ( one.begin (), one.end (), std::front_inserter ( argh )); |

122 | one.clear (); two.clear (); three.clear (); |

123 | |

124 | // boost::algorithm::hex ( argh.c_str (), std::front_inserter ( one )); |

125 | boost::algorithm::hex ( argh, std::front_inserter ( two )); |

126 | boost::algorithm::hex ( argh.begin (), argh.end (), std::front_inserter ( three )); |

127 | // BOOST_CHECK ( std::equal ( one.begin (), one.end (), two.begin ())); |

128 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), three.begin ())); |

129 | BOOST_CHECK ( std::equal ( two.begin (), two.end (), arg.rbegin ())); // reversed |

130 | } |

131 | } |

132 | |

133 | |

134 | BOOST_AUTO_TEST_CASE( test_main ) |

135 | { |

136 | test_to_hex (); |

137 | test_from_hex_success (); |

138 | } |

139 |