00001
00002
00003 #include <boost/test/unit_test.hpp>
00004 #include <boost/filesystem.hpp>
00005
00006 #include "util/systemio.hpp"
00007 #include "value/quadruple_traits.hpp"
00008 #include "value/triple_traits.hpp"
00009 #include "value/pair_traits.hpp"
00010
00011 using namespace aitools;
00012 namespace bfs = boost::filesystem;
00013
00014 static const bfs::path k_tmp_file("test_value_traits.tmp");
00015
00016
00017 template <typename T>
00018 void
00019 test(unsigned record_count)
00020 {
00021 typedef T value_type;
00022 typedef value::value_traits<value_type> traits_type;
00023
00024
00025 value_type test_value;
00026 BOOST_REQUIRE_EQUAL(sizeof(value_type), traits_type::size_of(test_value));
00027
00028
00029 FILE* fs(util::fopen(k_tmp_file, "wb"));
00030 for (unsigned i(0); i != record_count; ++i)
00031 {
00032 test_value = i;
00033 BOOST_REQUIRE(traits_type::write_to(test_value, fs));
00034 }
00035 util::fclose(fs);
00036
00037
00038 value_type expected_value;
00039 fs = util::fopen(k_tmp_file, "rb");
00040 for (unsigned i(0); i != record_count; ++i)
00041 {
00042 expected_value = i;
00043 BOOST_REQUIRE(traits_type::read_from(test_value, fs));
00044 BOOST_REQUIRE_EQUAL(expected_value, test_value);
00045 }
00046 BOOST_REQUIRE(!traits_type::read_from(test_value, fs));
00047 util::fclose(fs);
00048 bfs::remove(k_tmp_file);
00049
00050
00051
00052 const size_t buffer_size(record_count * sizeof(value_type));
00053 char* buffer_begin(new char[buffer_size]);
00054 char* buffer_pos_old(buffer_begin);
00055 const char* buffer_pos_new(buffer_begin);
00056 for (unsigned i(0); i != record_count; ++i)
00057 {
00058 test_value = i;
00059 buffer_pos_new = traits_type::copy_to(test_value, buffer_pos_old);
00060 BOOST_REQUIRE_EQUAL(traits_type::size_of(test_value),
00061 buffer_pos_new - buffer_pos_old);
00062 buffer_pos_old = const_cast<char*>(buffer_pos_new);
00063 }
00064 BOOST_REQUIRE_EQUAL(buffer_size, buffer_pos_old - buffer_begin);
00065
00066
00067 buffer_pos_old = buffer_begin;
00068 buffer_pos_new = buffer_begin;
00069 for (unsigned i(0); i != record_count; ++i)
00070 {
00071 expected_value = i;
00072 buffer_pos_new = traits_type::copy_from(test_value, buffer_pos_old);
00073 BOOST_REQUIRE_EQUAL(traits_type::size_of(expected_value),
00074 buffer_pos_new - buffer_pos_old);
00075 BOOST_REQUIRE_EQUAL(expected_value, test_value);
00076 buffer_pos_old = const_cast<char*>(buffer_pos_new);
00077 }
00078 BOOST_REQUIRE_EQUAL(buffer_size, buffer_pos_old - buffer_begin);
00079 delete[] buffer_begin;
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 BOOST_AUTO_TEST_SUITE(test_value_traits)
00150
00151 static const size_t k_record_count(100000);
00152
00153 BOOST_AUTO_TEST_CASE(test_i16)
00154 {
00155 test<int16_t>(k_record_count);
00156 }
00157
00158 BOOST_AUTO_TEST_CASE(test_ui16)
00159 {
00160 test<uint16_t>(k_record_count);
00161 }
00162
00163 BOOST_AUTO_TEST_CASE(test_i32)
00164 {
00165 test<int32_t>(k_record_count);
00166 }
00167
00168 BOOST_AUTO_TEST_CASE(test_ui32)
00169 {
00170 test<uint32_t>(k_record_count);
00171 }
00172
00173 BOOST_AUTO_TEST_CASE(test_i64)
00174 {
00175 test<int64_t>(k_record_count);
00176 }
00177
00178 BOOST_AUTO_TEST_CASE(test_ui64)
00179 {
00180 test<uint64_t>(k_record_count);
00181 }
00182
00183 BOOST_AUTO_TEST_CASE(test_dbl)
00184 {
00185 test<double>(k_record_count);
00186 }
00187
00188
00189
00190
00191
00192
00193 BOOST_AUTO_TEST_SUITE_END()