axmol/tests/cpp-tests/Source/UserDefaultTest/UserDefaultTest.cpp

206 lines
6.7 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2022-04-25 19:15:46 +08:00
Copyright (c) 2020 C4games Ltd.
2021-12-28 16:06:23 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2021-12-28 16:06:23 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2021-12-28 16:06:23 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-28 16:06:23 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "UserDefaultTest.h"
#include "stdio.h"
#include "stdlib.h"
#include <vector>
2015-05-22 03:20:43 +08:00
#include <sstream>
2015-05-22 03:39:00 +08:00
#include <iomanip>
using namespace std;
USING_NS_AX;
UserDefaultTests::UserDefaultTests()
{
ADD_TEST_CASE(UserDefaultTest);
}
UserDefaultTest::UserDefaultTest()
{
2021-12-28 16:06:23 +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);
addChild(label, 0);
2021-12-28 16:06:23 +08:00
label->setPosition(Vec2(s.width / 2, s.height - 50));
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));
doTest();
}
2021-12-28 16:06:23 +08:00
// template<typename T>
// void logData(const char* key)
//{
2021-12-28 16:06:23 +08:00
// Data data = UserDefault::getInstance()->getDataForKey(key);
// T* buffer = (T*) data.getBytes();
// ssize_t length = data.getSize() / sizeof(T);
//
2021-12-28 16:06:23 +08:00
// std::ostringstream ss;
// ss << setprecision(2) << std::fixed;
// for (int i = 0; i < length; i++)
// {
// ss << buffer[i] << " ";
// }
//
2022-07-16 10:43:05 +08:00
// AXLOG("%s is %s", key, ss.str().c_str());
2021-12-28 16:06:23 +08:00
// }
2021-12-28 16:06:23 +08:00
// template<typename T>
// void setData(const char* key)
//{
2021-12-28 16:06:23 +08:00
// Data data;
// vector<T> v;
//
2021-12-28 16:06:23 +08:00
// for (int i = 0; i <= 5; i++)
// {
// v.emplace_back(static_cast<T>(i));
2021-12-28 16:06:23 +08:00
// }
// data.copy((unsigned char*) v.data(), v.size() * sizeof(T));
// UserDefault::getInstance()->setDataForKey(key, data);
// }
//
2021-12-28 16:06:23 +08:00
// template<typename T>
// void setData2(const char* key)
//{
2021-12-28 16:06:23 +08:00
// Data data;
// vector<T> v;
//
2021-12-28 16:06:23 +08:00
// for (int i = 5; i >= 0; i--)
// {
// v.emplace_back(static_cast<T>(i));
2021-12-28 16:06:23 +08:00
// }
// data.copy((unsigned char*) v.data(), v.size() * sizeof(T));
// UserDefault::getInstance()->setDataForKey(key, data);
// }
void UserDefaultTest::doTest()
{
2021-12-28 17:20:17 +08:00
std::string strVal{this->_label->getString()};
2021-12-29 19:31:28 +08:00
this->_label->setString(
strVal.append("\n"
"********************** init value ***********************"));
// set default value
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);
// test saving of Data buffers
2021-12-28 16:06:23 +08:00
// setData<int>("int_data");
// setData<float>("float_data");
// setData<double>("double_data");
2015-07-06 17:01:17 +08:00
printValue();
2021-12-28 16:06:23 +08:00
// logData<int>("int_data");
// logData<float>("float_data");
// logData<double>("double_data");
2021-12-28 16:06:23 +08:00
// CCUserDefault::getInstance()->flush();
2021-12-29 19:31:28 +08:00
strVal = this->_label->getString();
this->_label->setString(
strVal.append("\n"
"********************** after change value ***********************"));
// change the value
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);
2021-12-28 16:06:23 +08:00
// setData2<int>("int_data");
// setData2<float>("float_data");
// setData2<double>("double_data");
UserDefault::getInstance()->flush();
// print value
2015-07-06 17:01:17 +08:00
printValue();
2021-12-28 16:06:23 +08:00
// logData<int>("int_data");
// logData<float>("float_data");
// logData<double>("double_data");
2015-07-06 17:01:17 +08:00
2021-12-29 19:31:28 +08:00
strVal = this->_label->getString();
this->_label->setString(
strVal.append("\n"
"********************** after delete value ***********************"));
2015-07-06 17:01:17 +08:00
UserDefault::getInstance()->deleteValueForKey("string");
UserDefault::getInstance()->deleteValueForKey("integer");
UserDefault::getInstance()->deleteValueForKey("float");
UserDefault::getInstance()->deleteValueForKey("double");
UserDefault::getInstance()->deleteValueForKey("bool");
// print value
printValue();
}
2015-07-06 17:01:17 +08:00
void UserDefaultTest::printValue()
{
char strTemp[256] = "";
// print value
2021-12-28 17:20:17 +08:00
std::string_view ret = UserDefault::getInstance()->getStringForKey("string");
sprintf(strTemp, "string is %s", ret.data());
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
2015-07-06 17:01:17 +08:00
double d = UserDefault::getInstance()->getDoubleForKey("double");
sprintf(strTemp, "double is %f", d);
2021-12-28 17:20:17 +08:00
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
2015-07-06 17:01:17 +08:00
int i = UserDefault::getInstance()->getIntegerForKey("integer");
sprintf(strTemp, "integer is %d", i);
2021-12-28 17:20:17 +08:00
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
2015-07-06 17:01:17 +08:00
float f = UserDefault::getInstance()->getFloatForKey("float");
sprintf(strTemp, "float is %f", f);
2021-12-28 17:20:17 +08:00
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
2015-07-06 17:01:17 +08:00
bool b = UserDefault::getInstance()->getBoolForKey("bool");
if (b)
{
2015-07-06 17:01:17 +08:00
sprintf(strTemp, "bool is true");
2021-12-28 17:20:17 +08:00
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
}
else
{
2015-07-06 17:01:17 +08:00
sprintf(strTemp, "bool is false");
2021-12-28 17:20:17 +08:00
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
}
}
2021-12-28 16:06:23 +08:00
UserDefaultTest::~UserDefaultTest() {}