| 1 | // Copyright (c) 2001-2011 Hartmut Kaiser |
| 2 | // |
| 3 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 5 | |
| 6 | #include <boost/spirit/include/karma_binary.hpp> |
| 7 | |
| 8 | #include <boost/spirit/include/karma_generate.hpp> |
| 9 | |
| 10 | #include <boost/predef/other/endian.h> |
| 11 | |
| 12 | #include "test.hpp" |
| 13 | |
| 14 | using namespace spirit_test; |
| 15 | |
| 16 | /////////////////////////////////////////////////////////////////////////////// |
| 17 | int |
| 18 | main() |
| 19 | { |
| 20 | using namespace boost::spirit; |
| 21 | |
| 22 | { // test native endian binaries |
| 23 | #if BOOST_ENDIAN_LITTLE_BYTE |
| 24 | BOOST_TEST(binary_test("\x01" , 1, byte_, 0x01)); |
| 25 | BOOST_TEST(binary_test("\x80" , 1, byte_, 0x80)); |
| 26 | BOOST_TEST(binary_test("\x01\x82" , 2, word, 0x8201)); |
| 27 | BOOST_TEST(binary_test("\x81\x02" , 2, word, 0x0281)); |
| 28 | BOOST_TEST(binary_test("\x01\x02\x03\x84" , 4, dword, 0x84030201)); |
| 29 | BOOST_TEST(binary_test("\x81\x02\x03\x04" , 4, dword, 0x04030281)); |
| 30 | #ifdef BOOST_HAS_LONG_LONG |
| 31 | BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x88" , 8, qword, |
| 32 | 0x8807060504030201LL)); |
| 33 | BOOST_TEST(binary_test("\x81\x02\x03\x04\x05\x06\x07\x08" , 8, qword, |
| 34 | 0x0807060504030281LL)); |
| 35 | #endif |
| 36 | BOOST_TEST(binary_test("\x00\x00\x80\x3f" , 4, bin_float, 1.0f)); |
| 37 | BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f" , 8, |
| 38 | bin_double, 1.0)); |
| 39 | |
| 40 | BOOST_TEST(binary_test_delimited("\x01\x00\x00\x00" , 4, byte_, 0x01, pad(4))); |
| 41 | BOOST_TEST(binary_test_delimited("\x01\x02\x00\x00" , 4, word, 0x0201, pad(4))); |
| 42 | BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04" , 4, dword, 0x04030201, pad(4))); |
| 43 | #ifdef BOOST_HAS_LONG_LONG |
| 44 | BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00" , 10, |
| 45 | qword, 0x0807060504030201LL, pad(10))); |
| 46 | #endif |
| 47 | BOOST_TEST(binary_test_delimited("\x00\x00\x80\x3f" , 4, bin_float, |
| 48 | 1.0f, pad(4))); |
| 49 | BOOST_TEST(binary_test_delimited("\x00\x00\x00\x00\x00\x00\xf0\x3f" , 8, |
| 50 | bin_double, 1.0, pad(8))); |
| 51 | |
| 52 | #else // BOOST_ENDIAN_LITTLE_BYTE |
| 53 | |
| 54 | BOOST_TEST(binary_test("\x01" , 1, byte_, 0x01)); |
| 55 | BOOST_TEST(binary_test("\x80" , 1, byte_, 0x80)); |
| 56 | BOOST_TEST(binary_test("\x01\x82" , 2, word, 0x0182)); |
| 57 | BOOST_TEST(binary_test("\x81\x02" , 2, word, 0x8102)); |
| 58 | BOOST_TEST(binary_test("\x01\x02\x03\x84" , 4, dword, 0x01020384)); |
| 59 | BOOST_TEST(binary_test("\x81\x02\x03\x04" , 4, dword, 0x81020304)); |
| 60 | #ifdef BOOST_HAS_LONG_LONG |
| 61 | BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x88" , 8, qword, |
| 62 | 0x0102030405060788LL)); |
| 63 | BOOST_TEST(binary_test("\x81\x02\x03\x04\x05\x06\x07\x08" , 8, qword, |
| 64 | 0x8102030405060708LL)); |
| 65 | #endif |
| 66 | BOOST_TEST(binary_test("\x3f\x80\x00\x00" , 4, bin_float, 1.0f)); |
| 67 | BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00" , 8, |
| 68 | bin_double, 1.0)); |
| 69 | |
| 70 | BOOST_TEST(binary_test_delimited("\x01\x00\x00\x00" , 4, byte_, 0x01, pad(4))); |
| 71 | BOOST_TEST(binary_test_delimited("\x01\x02\x00\x00" , 4, word, 0x0102, pad(4))); |
| 72 | BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04" , 4, dword, 0x01020304, pad(4))); |
| 73 | #ifdef BOOST_HAS_LONG_LONG |
| 74 | BOOST_TEST(binary_test_delimited("\x01\x02\x03\x04\x05\x06\x07\x08\x00\x00" , 10, |
| 75 | qword, 0x0102030405060708LL, pad(10))); |
| 76 | #endif |
| 77 | BOOST_TEST(binary_test_delimited("\x3f\x80\x00\x00" , 4, bin_float, |
| 78 | 1.0f, pad(4))); |
| 79 | BOOST_TEST(binary_test_delimited("\x3f\xf0\x00\x00\x00\x00\x00\x00" , 8, |
| 80 | bin_double, 1.0, pad(8))); |
| 81 | #endif |
| 82 | } |
| 83 | |
| 84 | { // test native endian binaries |
| 85 | #if BOOST_ENDIAN_LITTLE_BYTE |
| 86 | BOOST_TEST(binary_test("\x01" , 1, byte_(0x01))); |
| 87 | BOOST_TEST(binary_test("\x01\x02" , 2, word(0x0201))); |
| 88 | BOOST_TEST(binary_test("\x01\x02\x03\x04" , 4, dword(0x04030201))); |
| 89 | #ifdef BOOST_HAS_LONG_LONG |
| 90 | BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08" , 8, |
| 91 | qword(0x0807060504030201LL))); |
| 92 | #endif |
| 93 | BOOST_TEST(binary_test("\x00\x00\x80\x3f" , 4, bin_float(1.0f))); |
| 94 | BOOST_TEST(binary_test("\x00\x00\x00\x00\x00\x00\xf0\x3f" , 8, |
| 95 | bin_double(1.0))); |
| 96 | #else |
| 97 | BOOST_TEST(binary_test("\x01" , 1, byte_(0x01))); |
| 98 | BOOST_TEST(binary_test("\x01\x02" , 2, word(0x0102))); |
| 99 | BOOST_TEST(binary_test("\x01\x02\x03\x04" , 4, dword(0x01020304))); |
| 100 | #ifdef BOOST_HAS_LONG_LONG |
| 101 | BOOST_TEST(binary_test("\x01\x02\x03\x04\x05\x06\x07\x08" , 8, |
| 102 | qword(0x0102030405060708LL))); |
| 103 | #endif |
| 104 | BOOST_TEST(binary_test("\x3f\x80\x00\x00" , 4, bin_float(1.0f))); |
| 105 | BOOST_TEST(binary_test("\x3f\xf0\x00\x00\x00\x00\x00\x00" , 8, |
| 106 | bin_double(1.0))); |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | return boost::report_errors(); |
| 111 | } |
| 112 | |