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 :
7 : #include <fas/testing.hpp>
8 : #include <vset/memory/manager.hpp>
9 : #include <vset/memory/provider.hpp>
10 : #include <vset/memory/allocator.hpp>
11 : #include <vset/memory/fsb/aspect.hpp>
12 : #include <vset/buffer/persistent/filesync/aspect.hpp>
13 :
14 : size_t off2ptr(size_t off);
15 : size_t ptr2off(size_t ptr);
16 :
17 : const size_t CHAIN_HEAD = 16;
18 : const size_t CHANK_HEAD = 8;
19 : const size_t CHANK_COUNT = 64;
20 : const size_t VALUE_SIZE = 8;
21 : const size_t VALUE_BLOCK = VALUE_SIZE*64;
22 : const size_t CHANK_SIZE = CHANK_HEAD + VALUE_BLOCK ;
23 :
24 10000 : size_t off2ptr(size_t off)
25 : {
26 10000 : size_t result = CHAIN_HEAD;
27 10000 : result += CHANK_HEAD + (off / CHANK_COUNT) * CHANK_HEAD;
28 10000 : result += VALUE_SIZE*off;
29 10000 : return result ;
30 : }
31 :
32 10000 : size_t ptr2off(size_t ptr)
33 : {
34 10000 : size_t result = ptr - CHAIN_HEAD;
35 10000 : result -= (result/CHANK_SIZE) * CHANK_HEAD;
36 10000 : result -= CHANK_HEAD;
37 10000 : return result/VALUE_SIZE;
38 : }
39 :
40 3 : UNIT(test_offset, "")
41 : {
42 : using namespace fas::testing;
43 : // t << is_true<expect>(false) << FAS_FL;
44 : // t << stop;
45 : //return;
46 10001 : for (size_t i = 0; i < 10000; ++i )
47 : {
48 10000 : size_t ptr = off2ptr(i);
49 10000 : size_t off = ptr2off(ptr);
50 : //t << message("i=") << i << " off=" << off << " ptr=" << ptr << " > " << (ptr - CHAIN_HEAD) / CHANK_SIZE ;
51 10000 : t << equal<crash>(i, off) << " ptr=" << ptr << FAS_FL;
52 10000 : t << stop;
53 : //return;
54 : }
55 1 : }
56 :
57 : #define MAX_TEST 777
58 : const size_t CAPACITY = (777/64)*64 + (777%64!=0)*64;
59 :
60 : //typedef char value_type;
61 :
62 : template<typename T, typename Alloc>
63 1 : void test_char_init(T& t, Alloc& allocator)
64 : {
65 : using namespace fas::testing;
66 : typedef typename Alloc::pointer pointer;
67 : typedef typename Alloc::value_type value_type;
68 :
69 778 : for (int i=0; i < MAX_TEST; ++i)
70 : {
71 777 : value_type* ch = allocator.allocate(1).get_address();
72 777 : t << is_true<assert>( ch != fas_nullptr ) << "i=" << i << " " << FAS_TESTING_FILE_LINE;
73 777 : t << stop;
74 777 : *ch = static_cast<char>('0' + i%10);
75 : }
76 :
77 1 : pointer beg = allocator.begin();
78 1 : pointer end = allocator.end();
79 :
80 1 : int i=0;
81 778 : for ( ; beg != end; ++i)
82 : {
83 777 : t << equal< assert, value_type > ( *beg, '0' + i%10 ) << char(*beg) << "!=" << char('0' + i%10) << " " << FAS_TESTING_FILE_LINE;
84 777 : size_t off1 = beg.get_offset();
85 777 : value_type* add1 = beg.get_address();
86 777 : t << equal< assert, value_type > ( *add1, '0' + i%10 ) << FAS_FL;
87 777 : t << equal< assert, value_type* > ( &(*beg), &(*add1) ) << FAS_FL;
88 777 : beg.set_offset(off1);
89 777 : t << equal< assert, size_t > ( off1, beg.get_offset() ) << FAS_FL;
90 777 : t << equal< assert, value_type* > ( add1, beg.get_address() ) << FAS_FL;
91 777 : size_t off2 = beg.set_address(add1);
92 777 : t << equal< assert, size_t > ( off1, off2 ) << FAS_FL;
93 777 : t << equal< assert, value_type* > ( add1, beg.get_address() ) << "[" << (add1 - beg.get_address()) << "]"<< FAS_FL;
94 777 : t << equal< assert, size_t > ( off1, beg.get_offset() ) << FAS_FL;
95 777 : t << stop;
96 : //std::cout << "next" << std::endl;
97 777 : ++beg;
98 : }
99 1 : }
100 :
101 : template<typename T, typename Alloc>
102 3 : void test_char_test(T& t, const Alloc& allocator)
103 : {
104 : using namespace fas::testing;
105 : typedef typename Alloc::const_pointer const_pointer;
106 : typedef typename Alloc::value_type value_type;
107 :
108 3 : const_pointer beg = allocator.begin();
109 3 : const_pointer end = allocator.end();
110 3 : int i=0;
111 2334 : for ( ; beg != end; ++beg, ++i)
112 : {
113 2331 : t << equal< assert, value_type > ( *beg, '0' + i%10 ) << char(*beg) << "!=" << char('0' + i%10) << " " << FAS_TESTING_FILE_LINE;
114 : }
115 3 : t << equal< assert, int > ( i, MAX_TEST) << i << "!=" << MAX_TEST << " " << FAS_TESTING_FILE_LINE;
116 :
117 3 : t << equal< assert, int > ( i, allocator.count() ) << FAS_TESTING_FILE_LINE;
118 3 : t << equal< assert, int > ( CAPACITY, allocator.capacity() ) << FAS_TESTING_FILE_LINE;
119 :
120 3 : beg = allocator.begin();
121 3 : const_pointer beg1 = beg;
122 3 : ++beg1;
123 3 : --beg1;
124 :
125 3 : t << equal< assert > ( *beg, *beg1 ) << FAS_TESTING_FILE_LINE;
126 3 : t << equal< assert > ( beg, beg1 ) << FAS_TESTING_FILE_LINE;
127 :
128 :
129 3 : i = MAX_TEST - 1;
130 3 : beg = allocator.begin();
131 3 : end = allocator.end();
132 :
133 2331 : for ( --end; beg!=end;--end, --i)
134 : {
135 2328 : if ( i < 0 )
136 : {
137 0 : t << is_true< assert>(false, "Iterator is out of range!!!");
138 0 : break;
139 : }
140 :
141 2328 : t << equal< assert, value_type > ( *end, '0' + i%10 ) << char(*end) << "!=" << char('0' + i%10) << " " << FAS_TESTING_FILE_LINE;
142 : }
143 3 : }
144 :
145 3 : UNIT(test_unit, "")
146 : {
147 : using namespace fas::testing;
148 : typedef vset::memory::manager< vset::memory::strategy::fsb_mmap<char, ::vset::memory::fsb::aspect_offset > > allocator_type;
149 :
150 1 : allocator_type allocator;
151 1 : allocator.buffer().open("allocator.bin");
152 1 : allocator.buffer().truncate(0);
153 :
154 1 : test_char_init(t, allocator);
155 1 : test_char_test(t, allocator);
156 :
157 1 : allocator.buffer().sync();
158 1 : allocator.buffer().close();
159 1 : allocator.buffer().open("allocator.bin");
160 :
161 1 : test_char_test(t, allocator);
162 :
163 2 : allocator_type allocator2;
164 1 : allocator2.buffer().open("allocator.bin");
165 :
166 1 : test_char_test(t, allocator);
167 :
168 1 : allocator2.buffer().close();
169 1 : allocator.buffer().close();
170 :
171 2 : t << nothing;
172 :
173 1 : }
174 :
175 3 : UNIT(test_allocator, "")
176 : {
177 : using namespace fas::testing;
178 : using namespace vset;
179 :
180 : typedef memory::manager< vset::memory::strategy::fsb_mmap<char, ::vset::memory::fsb::aspect_offset > > manager;
181 : typedef memory::provider< manager > provider;
182 : typedef memory::allocator< provider > allocator;
183 :
184 1 : manager m;
185 1 : provider p(&m);
186 1 : allocator a( p );
187 1 : a.memory().buffer().open("allocator2.bin");
188 1 : a.memory().buffer().clear();
189 1 : *(a.allocate(1))='X';
190 1 : a.memory().buffer().sync();
191 1 : a.memory().buffer().close();
192 1 : t << nothing;
193 1 : }
194 :
195 1 : BEGIN_SUITE(basic_suite, "")
196 : ADD_UNIT(test_offset)
197 : ADD_UNIT(test_unit)
198 : ADD_UNIT(test_allocator)
199 7 : END_SUITE(basic_suite)
|