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();
|
2015-07-06 17:01:17 +08:00
|
|
|
auto label = Label::createWithTTF("CCUserDefault test Log data see console", "fonts/arial.ttf", 22);
|
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
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
this->_label = Label::createWithTTF("result", "fonts/arial.ttf", 12);
|
|
|
|
addChild(this->_label, 0);
|
|
|
|
|
|
|
|
label->setPosition(Vec2(s.width / 2, s.height - 50));
|
|
|
|
this->_label->setPosition(Vec2(s.width / 2, s.height / 2));
|
|
|
|
|
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();
|
2015-05-27 13:45:07 +08:00
|
|
|
ssize_t length = data.getSize() / sizeof(T);
|
2015-05-22 02:22:37 +08:00
|
|
|
|
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()
|
|
|
|
{
|
2015-07-06 17:01:17 +08:00
|
|
|
this->_label->setString(this->_label->getString() + "\n" + "********************** init value ***********************");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// 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");
|
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
printValue();
|
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
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
this->_label->setString(this->_label->getString() + "\n" + "********************** after change value ***********************");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// 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
|
2015-07-06 17:01:17 +08:00
|
|
|
printValue();
|
|
|
|
|
|
|
|
logData<int>("int_data");
|
|
|
|
logData<float>("float_data");
|
|
|
|
logData<double>("double_data");
|
|
|
|
|
|
|
|
this->_label->setString(this->_label->getString() + "\n" + "********************** after delete value ***********************");
|
|
|
|
|
|
|
|
UserDefault::getInstance()->deleteValueForKey("string");
|
|
|
|
UserDefault::getInstance()->deleteValueForKey("integer");
|
|
|
|
UserDefault::getInstance()->deleteValueForKey("float");
|
|
|
|
UserDefault::getInstance()->deleteValueForKey("double");
|
|
|
|
UserDefault::getInstance()->deleteValueForKey("bool");
|
|
|
|
|
|
|
|
// print value
|
|
|
|
printValue();
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
void UserDefaultTest::printValue()
|
|
|
|
{
|
|
|
|
char strTemp[256] = "";
|
|
|
|
// print value
|
|
|
|
std::string ret = UserDefault::getInstance()->getStringForKey("string");
|
|
|
|
sprintf(strTemp, "string is %s", ret.c_str());
|
|
|
|
this->_label->setString(this->_label->getString() + "\n" + strTemp);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
double d = UserDefault::getInstance()->getDoubleForKey("double");
|
|
|
|
sprintf(strTemp, "double is %f", d);
|
|
|
|
this->_label->setString(this->_label->getString() + "\n" + strTemp);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
int i = UserDefault::getInstance()->getIntegerForKey("integer");
|
|
|
|
sprintf(strTemp, "integer is %d", i);
|
|
|
|
this->_label->setString(this->_label->getString() + "\n" + strTemp);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
float f = UserDefault::getInstance()->getFloatForKey("float");
|
|
|
|
sprintf(strTemp, "float is %f", f);
|
|
|
|
this->_label->setString(this->_label->getString() + "\n" + strTemp);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-07-06 17:01:17 +08:00
|
|
|
bool b = UserDefault::getInstance()->getBoolForKey("bool");
|
2012-04-19 14:35:52 +08:00
|
|
|
if (b)
|
|
|
|
{
|
2015-07-06 17:01:17 +08:00
|
|
|
sprintf(strTemp, "bool is true");
|
|
|
|
this->_label->setString(this->_label->getString() + "\n" + strTemp);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-07-06 17:01:17 +08:00
|
|
|
sprintf(strTemp, "bool is false");
|
|
|
|
this->_label->setString(this->_label->getString() + "\n" + strTemp);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UserDefaultTest::~UserDefaultTest()
|
|
|
|
{
|
|
|
|
}
|
2015-07-06 17:01:17 +08:00
|
|
|
|
|
|
|
|