DataExpression.cpp 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019-2023 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../../../Source/Core/DataExpression.cpp"
  29. #include <RmlUi/Core/DataModelHandle.h>
  30. #include <doctest.h>
  31. #include <nanobench.h>
  32. using namespace Rml;
  33. using namespace ankerl;
  34. static DataTypeRegister type_register;
  35. static DataModel model(&type_register);
  36. static DataExpressionInterface interface(&model, nullptr);
  37. TEST_CASE("data_expressions")
  38. {
  39. float radius = 6.0f;
  40. String color_name = "color";
  41. Colourb color_value = Colourb(180, 100, 255);
  42. DataModelConstructor constructor(&model);
  43. constructor.Bind("radius", &radius);
  44. constructor.Bind("color_name", &color_name);
  45. constructor.BindFunc("color_value", [&](Variant& variant) { variant = ToString(color_value); });
  46. nanobench::Bench bench;
  47. bench.title("Data expression");
  48. bench.relative(true);
  49. auto bench_expression = [&](const String& expression, const char* parse_name, const char* execute_name) {
  50. DataParser parser(expression, interface);
  51. bool result = true;
  52. bench.run(parse_name, [&] { result &= parser.Parse(false); });
  53. REQUIRE(result);
  54. Program program = parser.ReleaseProgram();
  55. AddressList addresses = parser.ReleaseAddresses();
  56. DataInterpreter interpreter(program, addresses, interface);
  57. bench.run(execute_name, [&] { result &= interpreter.Run(); });
  58. REQUIRE(result);
  59. };
  60. bench_expression("2 * 2", "Simple (parse)", "Simple (execute)");
  61. bench_expression("true || false ? true && radius==1+2 ? 'Absolutely!' : color_value : 'no'", "Complex (parse)", "Complex (execute)");
  62. auto bench_assignment = [&](const String& expression, const char* parse_name, const char* execute_name) {
  63. DataParser parser(expression, interface);
  64. bool result = true;
  65. bench.run(parse_name, [&] { result &= parser.Parse(true); });
  66. REQUIRE(result);
  67. Program program = parser.ReleaseProgram();
  68. AddressList addresses = parser.ReleaseAddresses();
  69. DataInterpreter interpreter(program, addresses, interface);
  70. bench.run(execute_name, [&] { result &= interpreter.Run(); });
  71. REQUIRE(result);
  72. };
  73. bench_assignment("radius = 15", "Simple assign (parse)", "Simple assign (execute)");
  74. bench_assignment("radius = radius*radius*3.14; color_name = 'image-color'", "Complex assign (parse)", "Complex assign (execute)");
  75. }