-
-
Notifications
You must be signed in to change notification settings - Fork 338
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add benchmark and test for color parser
- Loading branch information
Showing
5 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include <benchmark/benchmark.h> | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <mbgl/util/color.hpp> | ||
|
||
static const std::vector<std::string> testStrings = {"#000000", | ||
"#FFFFFF", | ||
"#FF00FFAA", | ||
"rgba(255, 0, 0, 1.0)", | ||
"rgb(0, 255, 0)", | ||
"blue", | ||
"red", | ||
"invalid-color", | ||
"rgba(255, 255, 255, 0.5)", | ||
"#123"}; | ||
|
||
namespace { | ||
|
||
void ColorParse(benchmark::State& state) { | ||
for (auto _ : state) { | ||
for (const auto& str : testStrings) { | ||
auto result = mbgl::Color::parse(str); | ||
benchmark::DoNotOptimize(result); | ||
} | ||
} | ||
} | ||
|
||
}; // namespace | ||
|
||
BENCHMARK(ColorParse); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <gtest/gtest.h> | ||
#include <optional> | ||
#include <string> | ||
|
||
#include <mbgl/util/color.hpp> | ||
|
||
using namespace mbgl; | ||
|
||
void logUnexpectedValidResult(const std::string& input, const Color& color) { | ||
std::cerr << "Unexpected valid result for input: " << input << "\n"; | ||
std::cerr << "Parsed Color: r = " << color.r << ", g = " << color.g << ", b = " << color.b << ", a = " << color.a | ||
<< "\n"; | ||
} | ||
|
||
const std::map<std::string, std::optional<Color>> testCases = { | ||
// Valid inputs | ||
{"#000000", Color(0.0f, 0.0f, 0.0f, 1.0f)}, | ||
{"#FFFFFF", Color(1.0f, 1.0f, 1.0f, 1.0f)}, | ||
{"#FF0000", Color(1.0f, 0.0f, 0.0f, 1.0f)}, | ||
{"rgba(255, 0, 0, 1.0)", Color(1.0f, 0.0f, 0.0f, 1.0f)}, | ||
{"rgb(0, 255, 0)", Color(0.0f, 1.0f, 0.0f, 1.0f)}, | ||
{"blue", Color::blue()}, | ||
{"red", Color::red()}, | ||
{"rgba(0, 0, 0, 0)", Color(0.0f, 0.0f, 0.0f, 0.0f)}, | ||
{"#123", Color(0.067f, 0.133f, 0.2f, 1.0f)}, // Short hex format | ||
{"rgb(-10, 0, 0)", Color(0.0f, 0.0f, 0.0f, 1.0f)}, // Clamped to 0 | ||
{"rgba(300, 0, 0, 1.0)", Color(1.0f, 0.0f, 0.0f, 1.0f)}, // Clamped to 1 | ||
// {"#GGGGGG", Color(0.0f, 0.0f, 0.0f, 1.0f)}, // Treated as fallback black | ||
// not supported right now | ||
// {"#0F0F", Color(0.0f, 1.0f, 0.0f, 1.0f)}, | ||
// {"#123F", Color( | ||
// static_cast<float>(0x1) / (0xF + 1), | ||
// static_cast<float>(0x2) / (0xF + 1), | ||
// static_cast<float>(0x3) / (0xF + 1), | ||
// static_cast<float>(0x4) / (0xF + 1) | ||
// )}, | ||
|
||
// Invalid inputs | ||
{"not-a-color", std::nullopt}, | ||
{"", std::nullopt}, | ||
}; | ||
|
||
TEST(ColorParse, AllCases) { | ||
constexpr float absError = 0.02f; | ||
for (const auto& [input, expectedResult] : testCases) { | ||
auto result = Color::parse(input); | ||
|
||
if (expectedResult.has_value()) { | ||
// Valid case: Check the values | ||
ASSERT_TRUE(result.has_value()); | ||
EXPECT_NEAR(result->r, expectedResult->r, absError); | ||
EXPECT_NEAR(result->g, expectedResult->g, absError); | ||
EXPECT_NEAR(result->b, expectedResult->b, absError); | ||
EXPECT_NEAR(result->a, expectedResult->a, absError); | ||
} else { | ||
// Invalid case: Ensure no value is returned | ||
if (result.has_value()) { | ||
logUnexpectedValidResult(input, *result); | ||
} | ||
EXPECT_FALSE(result.has_value()); | ||
} | ||
} | ||
} |