2012-04-19 14:35:52 +08:00
|
|
|
#include "UserDefaultTest.h"
|
|
|
|
#include "stdio.h"
|
|
|
|
#include "stdlib.h"
|
2015-05-22 02:22:37 +08:00
|
|
|
#include <vector>
|
2015-05-22 03:20:43 +08:00
|
|
|
#include <sstream>
|
2015-05-22 03:39:00 +08:00
|
|
|
#include <iomanip>
|
2015-05-22 02:22:37 +08:00
|
|
|
|
|
|
|
using namespace std;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-04-09 08:37:30 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
|
2014-01-26 18:35:35 +08:00
|
|
|
// enable log
|
|
|
|
#define COCOS2D_DEBUG 1
|
|
|
|
|
2015-05-22 02:22:37 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
UserDefaultTests::UserDefaultTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(UserDefaultTest);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
UserDefaultTest::UserDefaultTest()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2014-04-09 23:31:05 +08:00
|
|
|
auto label = Label::createWithTTF("CCUserDefault test see log", "fonts/arial.ttf", 28);
|
2012-04-19 14:35:52 +08:00
|
|
|
addChild(label, 0);
|
2014-05-15 01:07:09 +08:00
|
|
|
label->setPosition( Vec2(s.width/2, s.height-50) );
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
doTest();
|
|
|
|
}
|
|
|
|
|
2015-05-22 02:22:37 +08:00
|
|
|
template<typename T>
|
|
|
|
void logData(const char* key)
|
|
|
|
{
|
|
|
|
Data data = UserDefault::getInstance()->getDataForKey(key);
|
|
|
|
T* buffer = (T*) data.getBytes();
|
|
|
|
int length = data.getSize() / sizeof(T);
|
|
|
|
|
2015-05-22 03:20:43 +08:00
|
|
|
std::ostringstream ss;
|
2015-05-22 03:39:00 +08:00
|
|
|
ss << setprecision(2) << std::fixed;
|
2015-05-22 02:22:37 +08:00
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
{
|
2015-05-22 03:39:00 +08:00
|
|
|
ss << buffer[i] << " ";
|
2015-05-22 02:22:37 +08:00
|
|
|
}
|
2015-05-22 03:39:00 +08:00
|
|
|
|
|
|
|
CCLOG("%s is %s", key, ss.str().c_str());
|
2015-05-22 02:22:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void setData(const char* key)
|
|
|
|
{
|
|
|
|
Data data;
|
|
|
|
vector<T> v;
|
|
|
|
|
|
|
|
for (int i = 0; i <= 5; i++)
|
|
|
|
{
|
|
|
|
v.push_back(static_cast<T>(i));
|
|
|
|
}
|
|
|
|
data.copy((unsigned char*) v.data(), v.size() * sizeof(T));
|
|
|
|
UserDefault::getInstance()->setDataForKey(key, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void setData2(const char* key)
|
|
|
|
{
|
|
|
|
Data data;
|
|
|
|
vector<T> v;
|
|
|
|
|
|
|
|
for (int i = 5; i >= 0; i--)
|
|
|
|
{
|
|
|
|
v.push_back(static_cast<T>(i));
|
|
|
|
}
|
|
|
|
data.copy((unsigned char*) v.data(), v.size() * sizeof(T));
|
|
|
|
UserDefault::getInstance()->setDataForKey(key, data);
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
void UserDefaultTest::doTest()
|
|
|
|
{
|
|
|
|
CCLOG("********************** init value ***********************");
|
|
|
|
|
|
|
|
// set default value
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
UserDefault::getInstance()->setStringForKey("string", "value1");
|
|
|
|
UserDefault::getInstance()->setIntegerForKey("integer", 10);
|
|
|
|
UserDefault::getInstance()->setFloatForKey("float", 2.3f);
|
|
|
|
UserDefault::getInstance()->setDoubleForKey("double", 2.4);
|
|
|
|
UserDefault::getInstance()->setBoolForKey("bool", true);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-05-22 02:22:37 +08:00
|
|
|
// test saving of Data buffers
|
|
|
|
setData<int>("int_data");
|
|
|
|
setData<float>("float_data");
|
|
|
|
setData<double>("double_data");
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// print value
|
|
|
|
|
2013-12-19 03:45:30 +08:00
|
|
|
std::string ret = UserDefault::getInstance()->getStringForKey("string");
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("string is %s", ret.c_str());
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
double d = UserDefault::getInstance()->getDoubleForKey("double");
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("double is %f", d);
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
int i = UserDefault::getInstance()->getIntegerForKey("integer");
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("integer is %d", i);
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
float f = UserDefault::getInstance()->getFloatForKey("float");
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("float is %f", f);
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
bool b = UserDefault::getInstance()->getBoolForKey("bool");
|
2012-04-19 14:35:52 +08:00
|
|
|
if (b)
|
|
|
|
{
|
|
|
|
CCLOG("bool is true");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("bool is false");
|
|
|
|
}
|
2015-05-22 02:22:37 +08:00
|
|
|
|
|
|
|
logData<int>("int_data");
|
|
|
|
logData<float>("float_data");
|
|
|
|
logData<double>("double_data");
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
//CCUserDefault::getInstance()->flush();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
CCLOG("********************** after change value ***********************");
|
|
|
|
|
|
|
|
// change the value
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
UserDefault::getInstance()->setStringForKey("string", "value2");
|
|
|
|
UserDefault::getInstance()->setIntegerForKey("integer", 11);
|
|
|
|
UserDefault::getInstance()->setFloatForKey("float", 2.5f);
|
|
|
|
UserDefault::getInstance()->setDoubleForKey("double", 2.6);
|
|
|
|
UserDefault::getInstance()->setBoolForKey("bool", false);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-05-22 02:22:37 +08:00
|
|
|
setData2<int>("int_data");
|
|
|
|
setData2<float>("float_data");
|
|
|
|
setData2<double>("double_data");
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
UserDefault::getInstance()->flush();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// print value
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
ret = UserDefault::getInstance()->getStringForKey("string");
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("string is %s", ret.c_str());
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
d = UserDefault::getInstance()->getDoubleForKey("double");
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("double is %f", d);
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
i = UserDefault::getInstance()->getIntegerForKey("integer");
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("integer is %d", i);
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
f = UserDefault::getInstance()->getFloatForKey("float");
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLOG("float is %f", f);
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
b = UserDefault::getInstance()->getBoolForKey("bool");
|
2012-04-19 14:35:52 +08:00
|
|
|
if (b)
|
|
|
|
{
|
|
|
|
CCLOG("bool is true");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CCLOG("bool is false");
|
|
|
|
}
|
2015-05-22 02:22:37 +08:00
|
|
|
|
|
|
|
logData<int>("int_data");
|
|
|
|
logData<float>("float_data");
|
|
|
|
logData<double>("double_data");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UserDefaultTest::~UserDefaultTest()
|
|
|
|
{
|
|
|
|
}
|