1 // Copyright 2010-2018, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include "storage/memory_storage.h"
31
32 #include <map>
33 #include <memory>
34 #include <string>
35
36 #include "base/port.h"
37 #include "testing/base/public/googletest.h"
38 #include "testing/base/public/gunit.h"
39
40 namespace mozc {
41 namespace storage {
42 namespace {
43
CreateKeyValue(std::map<string,string> * output,int size)44 void CreateKeyValue(std::map<string, string> *output, int size) {
45 output->clear();
46 for (int i = 0; i < size; ++i) {
47 char key[64];
48 char value[64];
49 snprintf(key, sizeof(key), "key%d", i);
50 snprintf(value, sizeof(value), "value%d", i);
51 output->insert(std::pair<string, string>(key, value));
52 }
53 }
54
55 } // namespace
56
TEST(MemoryStorageTest,SimpleTest)57 TEST(MemoryStorageTest, SimpleTest) {
58 static const int kSize[] = {10, 100, 1000};
59
60 for (int i = 0; i < arraysize(kSize); ++i) {
61 std::unique_ptr<StorageInterface> storage(MemoryStorage::New());
62
63 // Insert
64 std::map<string, string> target;
65 CreateKeyValue(&target, kSize[i]);
66 {
67 for (std::map<string, string>::const_iterator it = target.begin();
68 it != target.end(); ++it) {
69 EXPECT_TRUE(storage->Insert(it->first, it->second));
70 }
71 }
72
73 // Lookup
74 for (std::map<string, string>::const_iterator it = target.begin();
75 it != target.end(); ++it) {
76 string value;
77 EXPECT_TRUE(storage->Lookup(it->first, &value));
78 EXPECT_EQ(value, it->second);
79 }
80
81 for (std::map<string, string>::const_iterator it = target.begin();
82 it != target.end(); ++it) {
83 const string key = it->first + ".dummy";
84 string value;
85 EXPECT_FALSE(storage->Lookup(key, &value));
86 }
87
88 // Erase
89 int id = 0;
90 for (std::map<string, string>::const_iterator it = target.begin();
91 it != target.end(); ++it) {
92 if (id % 2 == 0) {
93 EXPECT_TRUE(storage->Erase(it->first));
94 const string key = it->first + ".dummy";
95 EXPECT_FALSE(storage->Erase(key));
96 }
97 }
98
99 for (std::map<string, string>::const_iterator it = target.begin();
100 it != target.end(); ++it) {
101 string value;
102 const string &key = it->first;
103 if (id % 2 == 0) {
104 EXPECT_FALSE(storage->Lookup(key, &value));
105 } else {
106 EXPECT_TRUE(storage->Lookup(key, &value));
107 }
108 }
109 }
110 }
111
112 } // namespace storage
113 } // namespace mozc
114