PuzzleSDK
UserDefaultTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
3
4 http://www.cocos2d-x.org
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 ****************************************************************************/
24
25#include "UserDefaultTest.h"
26#include "stdio.h"
27#include "stdlib.h"
28#include <vector>
29#include <sstream>
30#include <iomanip>
31
32using namespace std;
33
35
36// enable log
37#define COCOS2D_DEBUG 1
38
39
40UserDefaultTests::UserDefaultTests()
41{
43}
44
46{
47 auto s = Director::getInstance()->getWinSize();
48 auto label = Label::createWithTTF("CCUserDefault test Log data see console", "fonts/arial.ttf", 22);
49 addChild(label, 0);
50 label->setPosition( Vec2(s.width/2, s.height-50) );
51
52 this->_label = Label::createWithTTF("result", "fonts/arial.ttf", 12);
53 addChild(this->_label, 0);
54
55 label->setPosition(Vec2(s.width / 2, s.height - 50));
56 this->_label->setPosition(Vec2(s.width / 2, s.height / 2));
57
58 doTest();
59}
60
61template<typename T>
62void logData(const char* key)
63{
64 Data data = UserDefault::getInstance()->getDataForKey(key);
65 T* buffer = (T*) data.getBytes();
66 ssize_t length = data.getSize() / sizeof(T);
67
68 std::ostringstream ss;
69 ss << setprecision(2) << std::fixed;
70 for (int i = 0; i < length; i++)
71 {
72 ss << buffer[i] << " ";
73 }
74
75 CCLOG("%s is %s", key, ss.str().c_str());
76}
77
78template<typename T>
79void setData(const char* key)
80{
81 Data data;
82 vector<T> v;
83
84 for (int i = 0; i <= 5; i++)
85 {
86 v.push_back(static_cast<T>(i));
87 }
88 data.copy((unsigned char*) v.data(), v.size() * sizeof(T));
89 UserDefault::getInstance()->setDataForKey(key, data);
90}
91
92template<typename T>
93void setData2(const char* key)
94{
95 Data data;
96 vector<T> v;
97
98 for (int i = 5; i >= 0; i--)
99 {
100 v.push_back(static_cast<T>(i));
101 }
102 data.copy((unsigned char*) v.data(), v.size() * sizeof(T));
103 UserDefault::getInstance()->setDataForKey(key, data);
104}
105
107{
108 this->_label->setString(this->_label->getString() + "\n" + "********************** init value ***********************");
109
110 // set default value
111
112 UserDefault::getInstance()->setStringForKey("string", "value1");
113 UserDefault::getInstance()->setIntegerForKey("integer", 10);
114 UserDefault::getInstance()->setFloatForKey("float", 2.3f);
115 UserDefault::getInstance()->setDoubleForKey("double", 2.4);
116 UserDefault::getInstance()->setBoolForKey("bool", true);
117
118 // test saving of Data buffers
119 setData<int>("int_data");
120 setData<float>("float_data");
121 setData<double>("double_data");
122
123 printValue();
124
125 logData<int>("int_data");
126 logData<float>("float_data");
127 logData<double>("double_data");
128
129 //CCUserDefault::getInstance()->flush();
130
131 this->_label->setString(this->_label->getString() + "\n" + "********************** after change value ***********************");
132
133 // change the value
134
135 UserDefault::getInstance()->setStringForKey("string", "value2");
136 UserDefault::getInstance()->setIntegerForKey("integer", 11);
137 UserDefault::getInstance()->setFloatForKey("float", 2.5f);
138 UserDefault::getInstance()->setDoubleForKey("double", 2.6);
139 UserDefault::getInstance()->setBoolForKey("bool", false);
140
141 setData2<int>("int_data");
142 setData2<float>("float_data");
143 setData2<double>("double_data");
144
145 UserDefault::getInstance()->flush();
146
147 // print value
148 printValue();
149
150 logData<int>("int_data");
151 logData<float>("float_data");
152 logData<double>("double_data");
153
154 this->_label->setString(this->_label->getString() + "\n" + "********************** after delete value ***********************");
155
156 UserDefault::getInstance()->deleteValueForKey("string");
157 UserDefault::getInstance()->deleteValueForKey("integer");
158 UserDefault::getInstance()->deleteValueForKey("float");
159 UserDefault::getInstance()->deleteValueForKey("double");
160 UserDefault::getInstance()->deleteValueForKey("bool");
161
162 // print value
163 printValue();
164}
165
167{
168 char strTemp[256] = "";
169 // print value
170 std::string ret = UserDefault::getInstance()->getStringForKey("string");
171 sprintf(strTemp, "string is %s", ret.c_str());
172 this->_label->setString(this->_label->getString() + "\n" + strTemp);
173
174 double d = UserDefault::getInstance()->getDoubleForKey("double");
175 sprintf(strTemp, "double is %f", d);
176 this->_label->setString(this->_label->getString() + "\n" + strTemp);
177
178 int i = UserDefault::getInstance()->getIntegerForKey("integer");
179 sprintf(strTemp, "integer is %d", i);
180 this->_label->setString(this->_label->getString() + "\n" + strTemp);
181
182 float f = UserDefault::getInstance()->getFloatForKey("float");
183 sprintf(strTemp, "float is %f", f);
184 this->_label->setString(this->_label->getString() + "\n" + strTemp);
185
186 bool b = UserDefault::getInstance()->getBoolForKey("bool");
187 if (b)
188 {
189 sprintf(strTemp, "bool is true");
190 this->_label->setString(this->_label->getString() + "\n" + strTemp);
191 }
192 else
193 {
194 sprintf(strTemp, "bool is false");
195 this->_label->setString(this->_label->getString() + "\n" + strTemp);
196 }
197}
198
200{
201}
202
203
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
void setData2(const char *key)
void logData(const char *key)
void setData(const char *key)
USING_NS_CC
cocos2d::Label * _label