Line data Source code
1 : //
2 : // Author: Vladimir Migashko <migashko@gmail.com>, (C) 2012
3 : //
4 : // Copyright: See COPYING file that comes with this distribution
5 : //
6 : #include <iostream>
7 : #include <vset/buffer/buffer.hpp>
8 : #include <vset/buffer/persistent_buffer.hpp>
9 : #include <vset/buffer/strategy.hpp>
10 : #include <fas/testing.hpp>
11 :
12 : template<typename T, typename B>
13 3 : void first_test(T& t, B& buff)
14 : {
15 : using namespace fas::testing;
16 :
17 3 : t << equal<expect, size_t>( 0, buff.size() ) << " first test: " << FAS_TESTING_FILE_LINE;
18 3 : t << equal<expect, size_t>( 0, buff.capacity() ) << " first test: "<< FAS_TESTING_FILE_LINE;
19 3 : buff.resize(10);
20 3 : buff.reserve(20);
21 3 : t << equal<expect, size_t>( 10, buff.size() ) << buff.size() << " first test: "<< FAS_TESTING_FILE_LINE;
22 3 : t << equal<expect, size_t>( 20, buff.capacity() )<< buff.capacity() << " first test: "<< FAS_TESTING_FILE_LINE;
23 3 : buff.data()[0]='0';
24 3 : buff.data()[1]='1';
25 3 : t << equal<expect, char>( buff.data()[0], '0' )<< " first test: "<< FAS_TESTING_FILE_LINE;
26 3 : t << equal<expect, char>( buff.data()[1], '1' )<< " first test: "<< FAS_TESTING_FILE_LINE;
27 3 : buff.clear();
28 3 : t << equal<expect, size_t>( 0, buff.size() )<< " first test: "<< FAS_TESTING_FILE_LINE;
29 3 : t << equal<expect, size_t>( 20, buff.capacity() )<< buff.capacity() << " first test: "<< FAS_TESTING_FILE_LINE;
30 3 : buff.resize(11);
31 3 : buff.truncate(7);
32 3 : t << equal<expect, size_t>( 7, buff.size() )<< " first test: "<< FAS_TESTING_FILE_LINE;
33 3 : t << equal<expect, size_t>( 7, buff.capacity() )<< " first test: "<< FAS_TESTING_FILE_LINE;
34 3 : buff.reserve(14);
35 24 : for (int i = 0; i < 7; ++i)
36 : {
37 21 : buff.data()[i] = static_cast<char>('0' + i);
38 : }
39 3 : }
40 :
41 : template<typename T, typename B>
42 9 : void second_test(T& t, B& buff, const std::string& text)
43 : {
44 : using namespace fas::testing;
45 9 : t << equal<expect, size_t>( 7, buff.size() ) << "second test (" << text << ")" << FAS_TESTING_FILE_LINE;
46 9 : t << equal<expect, size_t>( 14, buff.capacity() ) << "second test (" << text << ")"<< FAS_TESTING_FILE_LINE;
47 :
48 72 : for (int i = 0 ; i < 7; ++i)
49 63 : t << equal<expect, char>( buff.data()[i], '0'+i ) << "second test (" << text << "[" << i << "]!=" << buff.data()[i] << ")"<< FAS_TESTING_FILE_LINE;
50 9 : }
51 :
52 3 : UNIT(test_unit, "")
53 : {
54 : using namespace fas::testing;
55 : using namespace vset::buffer;
56 1 : buffer< strategy::inmem > buff;
57 1 : first_test( t, buff);
58 1 : second_test( t, buff, "simple");
59 1 : t << nothing;
60 1 : }
61 :
62 3 : UNIT(persistent_unit, "")
63 : {
64 : using namespace fas::testing;
65 : using namespace vset::buffer;
66 1 : persistent_buffer< strategy::filesync > pbuf;
67 1 : pbuf.open("test.bin");
68 1 : pbuf.truncate(0);
69 :
70 1 : first_test( t, pbuf);
71 1 : pbuf.sync();
72 1 : second_test( t, pbuf, "after clear");
73 1 : pbuf.close();
74 1 : pbuf.open("test.bin");
75 1 : second_test( t, pbuf, "after close/open");
76 1 : pbuf.open("test.bin");
77 1 : second_test( t, pbuf, "after reopen");
78 2 : persistent_buffer< strategy::filesync > pbuf2;
79 1 : pbuf2.open("test.bin");
80 1 : second_test( t, pbuf2, "open exist");
81 1 : pbuf.close();
82 1 : pbuf2.close();
83 2 : t << nothing;
84 1 : }
85 :
86 3 : UNIT(mmap_unit, "")
87 : {
88 : using namespace fas::testing;
89 : using namespace vset::buffer;
90 1 : persistent_buffer< strategy::mmap > pbuf;
91 1 : pbuf.open("test_mmap.bin");
92 1 : pbuf.truncate(0);
93 :
94 1 : first_test( t, pbuf);
95 1 : pbuf.sync();
96 1 : second_test( t, pbuf, "after clear");
97 1 : pbuf.close();
98 1 : pbuf.open("test_mmap.bin");
99 1 : second_test( t, pbuf, "after close/open");
100 1 : pbuf.open("test_mmap.bin");
101 1 : second_test( t, pbuf, "after reopen");
102 2 : persistent_buffer< strategy::mmap > pbuf2;
103 1 : pbuf2.open("test_mmap.bin");
104 1 : second_test( t, pbuf2, "open exist");
105 1 : pbuf.close();
106 1 : pbuf2.close();
107 2 : t << nothing;
108 1 : }
109 :
110 :
111 1 : BEGIN_SUITE(basic_suite, "")
112 : ADD_UNIT(test_unit)
113 : ADD_UNIT(persistent_unit)
114 : ADD_UNIT(mmap_unit)
115 7 : END_SUITE(basic_suite)
|