Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / common / test_sloppy_crc_map.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <iostream>
5
6 #include "common/SloppyCRCMap.h"
7 #include "common/Formatter.h"
8 #include <gtest/gtest.h>
9
10 using namespace std;
11
12 void dump(const SloppyCRCMap& scm)
13 {
14   Formatter *f = Formatter::create("json-pretty");
15   f->open_object_section("map");
16   scm.dump(f);
17   f->close_section();
18   f->flush(cout);
19   delete f;
20 }
21
22 TEST(SloppyCRCMap, basic) {
23   SloppyCRCMap scm(4);
24
25   bufferlist a, b;
26   a.append("The quick brown fox jumped over a fence whose color I forget.");
27   b.append("asdf");
28
29   scm.write(0, a.length(), a);
30   if (0)
31     dump(scm);
32   ASSERT_EQ(0, scm.read(0, a.length(), a, &cout));
33
34   scm.write(12, b.length(), b);
35   if (0)
36     dump(scm);
37
38   ASSERT_EQ(0, scm.read(12, b.length(), b, &cout));
39   ASSERT_EQ(1, scm.read(0, a.length(), a, &cout));
40 }
41
42 TEST(SloppyCRCMap, truncate) {
43   SloppyCRCMap scm(4);
44
45   bufferlist a, b;
46   a.append("asdf");
47   b.append("qwer");
48
49   scm.write(0, a.length(), a);
50   scm.write(4, a.length(), a);
51   ASSERT_EQ(0, scm.read(4, 4, a, &cout));
52   ASSERT_EQ(1, scm.read(4, 4, b, &cout));
53   scm.truncate(4);
54   ASSERT_EQ(0, scm.read(4, 4, b, &cout));
55 }
56
57 TEST(SloppyCRCMap, zero) {
58   SloppyCRCMap scm(4);
59
60   bufferlist a, b;
61   a.append("asdf");
62   b.append("qwer");
63
64   scm.write(0, a.length(), a);
65   scm.write(4, a.length(), a);
66   ASSERT_EQ(0, scm.read(4, 4, a, &cout));
67   ASSERT_EQ(1, scm.read(4, 4, b, &cout));
68   scm.zero(4, 4);
69   ASSERT_EQ(1, scm.read(4, 4, a, &cout));
70   ASSERT_EQ(1, scm.read(4, 4, b, &cout));
71
72   bufferptr bp(4);
73   bp.zero();
74   bufferlist c;
75   c.append(bp);
76   ASSERT_EQ(0, scm.read(0, 4, a, &cout));
77   ASSERT_EQ(0, scm.read(4, 4, c, &cout));
78   scm.zero(0, 15);
79   ASSERT_EQ(1, scm.read(0, 4, a, &cout));
80   ASSERT_EQ(0, scm.read(0, 4, c, &cout));
81 }
82
83 TEST(SloppyCRCMap, clone_range) {
84   SloppyCRCMap src(4);
85   SloppyCRCMap dst(4);
86
87   bufferlist a, b;
88   a.append("asdfghjkl");
89   b.append("qwertyui");
90
91   src.write(0, a.length(), a);
92   src.write(8, a.length(), a);
93   src.write(16, a.length(), a);
94
95   dst.write(0, b.length(), b);
96   dst.clone_range(0, 8, 0, src);
97   ASSERT_EQ(2, dst.read(0, 8, b, &cout));
98   ASSERT_EQ(0, dst.read(8, 8, b, &cout));
99
100   dst.write(16, b.length(), b);
101   ASSERT_EQ(2, dst.read(16, 8, a, &cout));
102   dst.clone_range(16, 8, 16, src);
103   ASSERT_EQ(0, dst.read(16, 8, a, &cout));
104
105   dst.write(16, b.length(), b);
106   ASSERT_EQ(1, dst.read(16, 4, a, &cout));
107   dst.clone_range(16, 8, 2, src);
108   ASSERT_EQ(0, dst.read(16, 4, a, &cout));
109
110   dst.write(0, b.length(), b);
111   dst.write(8, b.length(), b);
112   ASSERT_EQ(2, dst.read(0, 8, a, &cout));
113   ASSERT_EQ(2, dst.read(8, 8, a, &cout));
114   dst.clone_range(2, 8, 0, src);
115   ASSERT_EQ(0, dst.read(0, 8, a, &cout));
116   ASSERT_EQ(0, dst.read(8, 4, a, &cout));
117 }