string_view for cpp-tests

This commit is contained in:
halx99 2021-12-28 17:20:17 +08:00
parent 7ece7b2cea
commit 1e9ad9d240
18 changed files with 80 additions and 74 deletions

View File

@ -43,9 +43,9 @@ USING_NS_CC;
class DurationRecorder
{
public:
void startTick(const std::string& key) { _durations[key] = -now(); }
void startTick(std::string_view key) { _durations[key] = -now(); }
int endTick(const std::string& key)
int endTick(std::string_view key)
{
auto n = now();
auto itr = _durations.find(key);
@ -69,7 +69,7 @@ public:
void reset() { _durations.clear(); }
private:
std::map<std::string, int64_t> _durations;
hlookup::string_map<int64_t> _durations;
};
NewRendererTests::NewRendererTests()

View File

@ -936,7 +936,7 @@ std::string NodeGlobalZValueTest::subtitle() const
class MySprite : public Sprite
{
public:
static MySprite* create(const std::string& spritefilename)
static MySprite* create(std::string_view spritefilename)
{
auto sprite = new MySprite;
sprite->initWithFile(spritefilename);

View File

@ -86,7 +86,7 @@ ShaderNode* ShaderNode::shaderNodeWithVertex(std::string_view vert, std::string_
return node;
}
bool ShaderNode::initWithVertex(std::string_view vert, const std::string& frag)
bool ShaderNode::initWithVertex(std::string_view vert, std::string_view frag)
{
_vertFileName = vert;
_fragFileName = frag;
@ -124,7 +124,7 @@ bool ShaderNode::initWithVertex(std::string_view vert, const std::string& frag)
return true;
}
void ShaderNode::loadShaderVertex(const std::string& vert, const std::string& frag)
void ShaderNode::loadShaderVertex(std::string_view vert, std::string_view frag)
{
auto fileUtiles = FileUtils::getInstance();

View File

@ -140,7 +140,7 @@ class ShaderNode : public cocos2d::Node
{
public:
CREATE_FUNC(ShaderNode);
static ShaderNode* shaderNodeWithVertex(const std::string& vert, const std::string& frag);
static ShaderNode* shaderNodeWithVertex(std::string_view vert, std::string_view frag);
virtual void update(float dt) override;
virtual void setPosition(const cocos2d::Vec2& newPosition) override;
@ -150,8 +150,8 @@ protected:
ShaderNode();
~ShaderNode();
bool initWithVertex(const std::string& vert, const std::string& frag);
void loadShaderVertex(const std::string& vert, const std::string& frag);
bool initWithVertex(std::string_view vert, std::string_view frag);
void loadShaderVertex(std::string_view vert, std::string_view frag);
virtual bool setProgramState(cocos2d::backend::ProgramState* programState, bool needsRetain = true) override
{

View File

@ -181,7 +181,7 @@ protected:
// Effect
//
bool Effect::initProgramState(const std::string& fragmentFilename)
bool Effect::initProgramState(std::string_view fragmentFilename)
{
auto fileUtiles = FileUtils::getInstance();
auto fragmentFullPath = fileUtiles->fullPathForFilename(fragmentFilename);
@ -407,7 +407,7 @@ class EffectNormalMapped : public Effect
{
public:
CREATE_FUNC(EffectNormalMapped);
static EffectNormalMapped* create(std::string_viewnormalMapFileName)
static EffectNormalMapped* create(std::string_view normalMapFileName)
{
EffectNormalMapped* normalMappedSprite = new EffectNormalMapped();
if (normalMappedSprite->init() && normalMappedSprite->initNormalMap(normalMapFileName))
@ -426,7 +426,7 @@ public:
protected:
bool init();
bool initNormalMap(std::string_viewnormalMapFileName);
bool initNormalMap(std::string_view normalMapFileName);
virtual void setTarget(EffectSprite* sprite) override;
EffectSprite* _sprite;
Vec3 _lightPos;

View File

@ -48,7 +48,7 @@ public:
virtual void setTarget(EffectSprite* sprite) {}
protected:
bool initProgramState(const std::string& fragmentFilename);
bool initProgramState(std::string_view fragmentFilename);
Effect();
virtual ~Effect();
cocos2d::backend::ProgramState* _programState = nullptr;

View File

@ -2591,7 +2591,7 @@ void Sprite3DPropertyTest::printMeshName(cocos2d::Ref* sender)
Vector<Mesh*> meshes = _sprite->getMeshes();
for (Mesh* mesh : meshes)
{
log("MeshName: %s ", mesh->getName().c_str());
log("MeshName: %s ", mesh->getName().data());
}
CCLOG("MeshName End");
}

View File

@ -74,7 +74,7 @@ SpriteFrameCachePixelFormatTest::SpriteFrameCachePixelFormatTest()
Texture2D::setDefaultAlphaPixelFormat(backend::PixelFormat::RGBA8);
}
void SpriteFrameCachePixelFormatTest::loadSpriteFrames(const std::string& file,
void SpriteFrameCachePixelFormatTest::loadSpriteFrames(std::string_view file,
cocos2d::backend::PixelFormat expectedFormat)
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(file);
@ -86,8 +86,9 @@ void SpriteFrameCachePixelFormatTest::loadSpriteFrames(const std::string& file,
#ifndef CC_USE_METAL
CC_ASSERT(texture->getPixelFormat() == expectedFormat);
#endif
const std::string textureInfo = StringUtils::format("%s: %.2f KB\r\n", texture->getStringForFormat(), memorySize);
infoLabel->setString(infoLabel->getString() + textureInfo);
const std::string textureInfo =
StringUtils::format("%s%s: %.2f KB\r\n", infoLabel->getString().data(), texture->getStringForFormat(), memorySize);
infoLabel->setString(textureInfo);
SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(file);
Director::getInstance()->getTextureCache()->removeTexture(texture);
@ -104,7 +105,7 @@ SpriteFrameCacheLoadMultipleTimes::SpriteFrameCacheLoadMultipleTimes()
loadSpriteFrames("Images/sprite_frames_test/test_RGBA8888.plist", backend::PixelFormat::RGBA8);
}
void SpriteFrameCacheLoadMultipleTimes::loadSpriteFrames(const std::string& file,
void SpriteFrameCacheLoadMultipleTimes::loadSpriteFrames(std::string_view file,
cocos2d::backend::PixelFormat expectedFormat)
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(file);
@ -123,7 +124,7 @@ SpriteFrameCacheFullCheck::SpriteFrameCacheFullCheck()
loadSpriteFrames("Images/test_polygon.plist", backend::PixelFormat::RGBA8);
}
void SpriteFrameCacheFullCheck::loadSpriteFrames(const std::string& file, cocos2d::backend::PixelFormat expectedFormat)
void SpriteFrameCacheFullCheck::loadSpriteFrames(std::string_view file, cocos2d::backend::PixelFormat expectedFormat)
{
auto cache = SpriteFrameCache::getInstance();
@ -157,7 +158,7 @@ public:
if (fullPath.empty())
{
// return if plist file doesn't exist
CCLOG("GenericJsonArraySpriteSheetLoader: can not find %s", filePath.c_str());
CCLOG("GenericJsonArraySpriteSheetLoader: can not find %s", filePath.data());
return;
}
@ -481,7 +482,7 @@ SpriteFrameCacheJsonAtlasTest::SpriteFrameCacheJsonAtlasTest()
infoLabel->setPosition(screenSize.width * 0.5f, screenSize.height * 0.7f);
addChild(infoLabel);
loadSpriteFrames("Images/sprite_frames_test/test_RGB8888_generic.json", backend::PixelFormat::RGBA8);
loadSpriteFrames("Images/sprite_frames_test/test_RGB8888_generic.json"sv, backend::PixelFormat::RGBA8);
}
SpriteFrameCacheJsonAtlasTest::~SpriteFrameCacheJsonAtlasTest()
@ -494,7 +495,7 @@ void SpriteFrameCacheJsonAtlasTest::loadSpriteFrames(std::string_view file,
cocos2d::backend::PixelFormat expectedFormat)
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile(file, GenericJsonArraySpriteSheetLoader::FORMAT);
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName("sprite_frames_test/grossini.png");
SpriteFrame* spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName("sprite_frames_test/grossini.png"sv);
Texture2D* texture = spriteFrame->getTexture();
const ssize_t bitsPerKB = 8 * 1024;
const double memorySize = 1.0 * texture->getBitsPerPixelForFormat() * texture->getContentSizeInPixels().width *
@ -502,8 +503,9 @@ void SpriteFrameCacheJsonAtlasTest::loadSpriteFrames(std::string_view file,
#ifndef CC_USE_METAL
CC_ASSERT(texture->getPixelFormat() == expectedFormat);
#endif
const std::string textureInfo = StringUtils::format("%s: %.2f KB\r\n", texture->getStringForFormat(), memorySize);
infoLabel->setString(infoLabel->getString() + textureInfo);
const std::string textureInfo =
StringUtils::format("%s%s: %.2f KB\r\n", infoLabel->getString().data(), texture->getStringForFormat(), memorySize);
infoLabel->setString(textureInfo);
SpriteFrameCache::getInstance()->removeSpriteFramesFromFile(file);
Director::getInstance()->getTextureCache()->removeTexture(texture);

View File

@ -41,7 +41,7 @@ public:
SpriteFrameCachePixelFormatTest();
private:
void loadSpriteFrames(const std::string& file, cocos2d::backend::PixelFormat expectedFormat);
void loadSpriteFrames(std::string_view file, cocos2d::backend::PixelFormat expectedFormat);
private:
cocos2d::Label* infoLabel;
@ -58,7 +58,7 @@ public:
SpriteFrameCacheLoadMultipleTimes();
private:
void loadSpriteFrames(const std::string& file, cocos2d::backend::PixelFormat expectedFormat);
void loadSpriteFrames(std::string_view file, cocos2d::backend::PixelFormat expectedFormat);
};
class SpriteFrameCacheFullCheck : public TestCase
@ -72,7 +72,7 @@ public:
SpriteFrameCacheFullCheck();
private:
void loadSpriteFrames(const std::string& file, cocos2d::backend::PixelFormat expectedFormat);
void loadSpriteFrames(std::string_view file, cocos2d::backend::PixelFormat expectedFormat);
};
class SpriteFrameCacheJsonAtlasTest : public TestCase

View File

@ -348,11 +348,11 @@ void SpritePolygonTestSlider::updateLabel(const cocos2d::Sprite* sp, const Polyg
Label* label = (Label*)(sp->getChildByName(sp->getName()));
auto filename = sp->getName();
auto size = pinfo.getRect().size / Director::getInstance()->getContentScaleFactor();
label->setString(filename + "\nVerts: " + Value((int)pinfo.getVertCount()).asString() +
label->setString((std::string)filename + "\nVerts: " + Value((int)pinfo.getVertCount()).asString() +
"\nPixels: " + Value((int)(pinfo.getArea() / (size.width * size.height) * 100)).asString() + "%");
}
Sprite* SpritePolygonTestSlider::makeSprite(const std::string& filename, const Vec2& pos)
Sprite* SpritePolygonTestSlider::makeSprite(std::string_view filename, const Vec2& pos)
{
// Sprite
auto quadSize = Sprite::create(filename)->getContentSize();
@ -374,7 +374,7 @@ Sprite* SpritePolygonTestSlider::makeSprite(const std::string& filename, const V
// Label
auto ttfConfig = TTFConfig("fonts/arial.ttf", 8);
auto spArea = Label::createWithTTF(
ttfConfig, filename + "\nVerts: " + Value((int)pinfo.getVertCount()).asString() +
ttfConfig, (std::string)filename + "\nVerts: " + Value((int)pinfo.getVertCount()).asString() +
"\nPixels: " + Value((int)(pinfo.getArea() / originalSize * 100)).asString() + "%");
ret->addChild(spArea);
spArea->setAnchorPoint(Vec2(0.0f, 1.0f));

View File

@ -219,7 +219,7 @@ void UIPageViewButtonTest::onButtonClicked(Ref* sender, Widget::TouchEventType t
{
if (type == Widget::TouchEventType::ENDED)
{
log("button %s clicked", static_cast<Button*>(sender)->getName().c_str());
log("button %s clicked", static_cast<Button*>(sender)->getName().data());
}
}

View File

@ -291,12 +291,12 @@ void UIRadioButtonTwoGroupsTest::clearRadioButtonGroup(Ref* sender)
void UIRadioButtonTwoGroupsTest::addLog(std::string_view log)
{
std::string existingLog = _logConsole->getString();
std::string existingLog{_logConsole->getString()};
if (!existingLog.empty())
{
existingLog = existingLog + "\n";
}
existingLog = existingLog + log;
existingLog += log;
++_numberOfLogLines;
if (_numberOfLogLines > 10)

View File

@ -74,7 +74,7 @@ bool WebViewTest::init()
resetBtn->addClickEventListener([=](Ref*) {
if (urlTextField->getString().size() != 0)
{
_webView->loadURL(std::string("https://") + urlTextField->getString());
_webView->loadURL(std::string("https://").append(urlTextField->getString()));
}
});
this->addChild(resetBtn);
@ -172,21 +172,21 @@ bool WebViewTest::init()
return false;
}
bool WebViewTest::onWebViewShouldStartLoading(ui::WebView* sender, const std::string& url)
bool WebViewTest::onWebViewShouldStartLoading(ui::WebView* sender, std::string_view url)
{
CCLOG("onWebViewShouldStartLoading, url is %s", url.c_str());
CCLOG("onWebViewShouldStartLoading, url is %s", url.data());
// don't do any OpenGL operation here!! It's forbidden!
return true;
}
void WebViewTest::onWebViewDidFinishLoading(ui::WebView* sender, const std::string& url)
void WebViewTest::onWebViewDidFinishLoading(ui::WebView* sender, std::string_view url)
{
auto node = (ui::Button*)this->getChildByName("evalJs");
node->setTitleText("start loading...");
CCLOG("onWebViewDidFinishLoading, url is %s", url.c_str());
CCLOG("onWebViewDidFinishLoading, url is %s", url.data());
}
void WebViewTest::onWebViewDidFailLoading(ui::WebView* sender, const std::string& url)
void WebViewTest::onWebViewDidFailLoading(ui::WebView* sender, std::string_view url)
{
CCLOG("onWebViewDidFailLoading, url is %s", url.c_str());
CCLOG("onWebViewDidFailLoading, url is %s", url.data());
}

View File

@ -37,9 +37,9 @@ public:
CREATE_FUNC(WebViewTest);
virtual bool init() override;
bool onWebViewShouldStartLoading(cocos2d::ui::WebView* sender, const std::string& url);
void onWebViewDidFinishLoading(cocos2d::ui::WebView* sender, const std::string& url);
void onWebViewDidFailLoading(cocos2d::ui::WebView* sender, const std::string& url);
bool onWebViewShouldStartLoading(cocos2d::ui::WebView* sender, std::string_view url);
void onWebViewDidFinishLoading(cocos2d::ui::WebView* sender, std::string_view url);
void onWebViewDidFailLoading(cocos2d::ui::WebView* sender, std::string_view url);
private:
cocos2d::ui::WebView* _webView;

View File

@ -373,7 +373,7 @@ void TemplateMapTest::onEnter()
UnitTestDemo::onEnter();
auto createMap = []() {
Map<std::string, Node*> ret;
StringMap<Node*> ret;
for (int i = 0; i < 20; ++i)
{
auto node = Node::create();
@ -392,7 +392,7 @@ void TemplateMapTest::onEnter()
CCASSERT(map1.keys(Node::create()).empty(), "map1's keys don't contain a empty Node.");
// Move constructor
Map<std::string, Node*> map2 = createMap();
auto map2 = createMap();
for (const auto& e : map2)
{
CC_UNUSED_PARAM(e);
@ -400,7 +400,7 @@ void TemplateMapTest::onEnter()
}
// Copy constructor
Map<std::string, Node*> map3(map2);
auto map3(map2);
for (const auto& e : map3)
{
CC_UNUSED_PARAM(e);
@ -408,7 +408,7 @@ void TemplateMapTest::onEnter()
}
// Move assignment operator
Map<std::string, Node*> map4;
StringMap<Node*> map4;
auto unusedNode = Node::create();
map4.insert("unused", unusedNode);
map4 = createMap();
@ -420,7 +420,7 @@ void TemplateMapTest::onEnter()
}
// Copy assignment operator
Map<std::string, Node*> map5;
StringMap<Node*> map5;
map5 = map4;
for (const auto& e : map5)
{
@ -500,7 +500,7 @@ void TemplateMapTest::onEnter()
CCASSERT(map6.at("insert03") == node3, "The element at insert03 is equal to node3.");
// erase
Map<std::string, Node*> mapForErase = createMap();
StringMap<Node*> mapForErase = createMap();
mapForErase.erase(mapForErase.find("9"));
CCASSERT(mapForErase.find("9") == mapForErase.end(), "9 is already removed.");
CCASSERT(mapForErase.size() == 19, "mapForErase's size is 19.");
@ -517,7 +517,7 @@ void TemplateMapTest::onEnter()
CCASSERT(mapForErase.size() == 15, "mapForErase's size is 15.");
// clear
Map<std::string, Node*> mapForClear = createMap();
StringMap<Node*> mapForClear = createMap();
auto mapForClearCopy = mapForClear;
mapForClear.clear();
@ -530,7 +530,7 @@ void TemplateMapTest::onEnter()
// get random object
// Set the seed by time
std::srand((unsigned)time(nullptr));
Map<std::string, Node*> mapForRandom = createMap();
StringMap<Node*> mapForRandom = createMap();
log("<--- begin ---->");
for (int i = 0; i < mapForRandom.size(); ++i)
{
@ -539,7 +539,7 @@ void TemplateMapTest::onEnter()
log("<---- end ---->");
// Self assignment
Map<std::string, Node*> mapForSelfAssign = createMap();
StringMap<Node*> mapForSelfAssign = createMap();
mapForSelfAssign = mapForSelfAssign;
CCASSERT(mapForSelfAssign.size() == 20, "mapForSelfAssign's size is 20.");
@ -559,7 +559,7 @@ void TemplateMapTest::onEnter()
}
}
void TemplateMapTest::constFunc(const Map<std::string, Node*>& map) const
void TemplateMapTest::constFunc(const StringMap<Node*>& map) const
{
log("[%s]=(tag)%d", "0", map.at("0")->getTag());
log("[%s]=(tag)%d", "1", map.find("1")->second->getTag());

View File

@ -52,7 +52,7 @@ public:
CREATE_FUNC(TemplateMapTest);
virtual void onEnter() override;
virtual std::string subtitle() const override;
void constFunc(const cocos2d::Map<std::string, cocos2d::Node*>& map) const;
void constFunc(const cocos2d::StringMap<cocos2d::Node*>& map) const;
};
class ValueTest : public UnitTestDemo

View File

@ -102,8 +102,9 @@ UserDefaultTest::UserDefaultTest()
void UserDefaultTest::doTest()
{
this->_label->setString(this->_label->getString() + "\n" +
"********************** init value ***********************");
std::string strVal{this->_label->getString()};
this->_label->setString(strVal.append("\n"
"********************** init value ***********************"));
// set default value
@ -126,8 +127,9 @@ void UserDefaultTest::doTest()
// CCUserDefault::getInstance()->flush();
this->_label->setString(this->_label->getString() + "\n" +
"********************** after change value ***********************");
strVal = this->_label->getString();
this->_label->setString(strVal.append("\n"
"********************** after change value ***********************"));
// change the value
@ -150,8 +152,9 @@ void UserDefaultTest::doTest()
// logData<float>("float_data");
// logData<double>("double_data");
this->_label->setString(this->_label->getString() + "\n" +
"********************** after delete value ***********************");
strVal = this->_label->getString();
this->_label->setString(strVal.append("\n"
"********************** after delete value ***********************"));
UserDefault::getInstance()->deleteValueForKey("string");
UserDefault::getInstance()->deleteValueForKey("integer");
@ -167,32 +170,32 @@ 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);
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);
double d = UserDefault::getInstance()->getDoubleForKey("double");
sprintf(strTemp, "double is %f", d);
this->_label->setString(this->_label->getString() + "\n" + strTemp);
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
int i = UserDefault::getInstance()->getIntegerForKey("integer");
sprintf(strTemp, "integer is %d", i);
this->_label->setString(this->_label->getString() + "\n" + strTemp);
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
float f = UserDefault::getInstance()->getFloatForKey("float");
sprintf(strTemp, "float is %f", f);
this->_label->setString(this->_label->getString() + "\n" + strTemp);
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
bool b = UserDefault::getInstance()->getBoolForKey("bool");
if (b)
{
sprintf(strTemp, "bool is true");
this->_label->setString(this->_label->getString() + "\n" + strTemp);
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
}
else
{
sprintf(strTemp, "bool is false");
this->_label->setString(this->_label->getString() + "\n" + strTemp);
this->_label->setString((std::string)this->_label->getString() + "\n" + strTemp);
}
}

View File

@ -43,9 +43,9 @@ std::string ZipTest::title() const
}
static void unzipTest(Label* label,
const std::string& originFile,
const std::string& tmpName,
const std::string& zipFile,
std::string_view originFile,
std::string_view tmpName,
std::string_view zipFile,
std::string_view password = "")
{
@ -58,7 +58,8 @@ static void unzipTest(Label* label,
unz_file_info fileInfo = {0};
char fileName[40] = {0};
auto newLocal = fu->getWritablePath() + tmpName;
std::string newLocal{fu->getWritablePath()};
newLocal += tmpName;
// copy file to support android
if (fu->isFileExist(newLocal))
@ -67,7 +68,7 @@ static void unzipTest(Label* label,
fu->removeFile(newLocal);
}
CCLOG("Copy %s to %s", zipFile.c_str(), newLocal.c_str());
CCLOG("Copy %s to %s", zipFile.data(), newLocal.c_str());
auto writeSuccess = fu->writeDataToFile(fu->getDataFromFile(zipFile), newLocal);
if (!writeSuccess)
{
@ -100,7 +101,7 @@ static void unzipTest(Label* label,
}
else
{
err = unzOpenCurrentFilePassword(fp, password.c_str());
err = unzOpenCurrentFilePassword(fp, password.data());
}
if (err != UNZ_OK)