1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include "globals.h"
5 #include "bytes.h"
6 #include "ldbs.h"
7 
8 static Bytes testdata
9 {
10     'L',  'B',  'S',  0x01,  'D',  'S',  'K',  0x02,
11     0x29, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
12     0x34, 0x12, 0x00, 0x00,  'L',  'D',  'B',  0x01,
13     0x00, 0x00, 0x00, 0x01,  0x01, 0x00, 0x00, 0x00,
14     0x01, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
15     0x01, 'L',  'D',  'B',   0x01, 0x00, 0x00, 0x00,
16     0x02, 0x01, 0x00, 0x00,  0x00, 0x01, 0x00, 0x00,
17     0x00, 0x14, 0x00, 0x00,  0x00, 0x02
18 };
19 
assertBytes(const Bytes & want,const Bytes & got)20 static void assertBytes(const Bytes& want, const Bytes& got)
21 {
22     if (want != got)
23     {
24         std::cout << "Wanted bytes:" << std::endl;
25         hexdump(std::cout, want);
26         std::cout << std::endl << "Produced bytes:" << std::endl;
27         hexdump(std::cout, got);
28         abort();
29     }
30 }
test_getset()31 static void test_getset()
32 {
33     LDBS ldbs;
34 
35     uint32_t block1 = ldbs.put(Bytes { 1 }, 1);
36     uint32_t block2 = ldbs.put(Bytes { 2 }, 2);
37     assert(block1 != block2);
38 
39     assert(ldbs.get(block1) == Bytes { 1 });
40     assert(ldbs.get(block2) == Bytes { 2 });
41 }
42 
test_write()43 static void test_write()
44 {
45     LDBS ldbs;
46 
47     uint32_t block1 = ldbs.put(Bytes { 1 }, 1);
48     uint32_t block2 = ldbs.put(Bytes { 2 }, 2);
49     Bytes data = ldbs.write(0x1234);
50 
51     assertBytes(testdata, data);
52 }
53 
test_read()54 static void test_read()
55 {
56     LDBS ldbs;
57     uint32_t trackDirectory = ldbs.read(testdata);
58 
59     assert(trackDirectory == 0x1234);
60     assert(ldbs.get(0x14) == Bytes { 1 });
61     assert(ldbs.get(0x29) == Bytes { 2 });
62 }
63 
main(int argc,const char * argv[])64 int main(int argc, const char* argv[])
65 {
66     test_getset();
67     test_write();
68     test_read();
69     return 0;
70 }
71