DataExpression.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 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.GetTransformFuncRegister());
  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, &type_register);
  43. constructor.Bind("radius", &radius);
  44. constructor.Bind("color_name", &color_name);
  45. constructor.BindFunc("color_value", [&](Variant& variant) {
  46. variant = ToString(color_value);
  47. });
  48. nanobench::Bench bench;
  49. bench.title("Data expression");
  50. bench.relative(true);
  51. auto bench_expression = [&](const String& expression, const char* parse_name, const char* execute_name) {
  52. DataParser parser(expression, interface);
  53. bool result = true;
  54. bench.run(parse_name, [&] {
  55. result &= parser.Parse(false);
  56. });
  57. REQUIRE(result);
  58. Program program = parser.ReleaseProgram();
  59. AddressList addresses = parser.ReleaseAddresses();
  60. DataInterpreter interpreter(program, addresses, interface);
  61. bench.run(execute_name, [&] {
  62. result &= interpreter.Run();
  63. });
  64. REQUIRE(result);
  65. };
  66. bench_expression(
  67. "2 * 2",
  68. "Simple (parse)",
  69. "Simple (execute)"
  70. );
  71. bench_expression(
  72. "true || false ? true && radius==1+2 ? 'Absolutely!' : color_value : 'no'",
  73. "Complex (parse)",
  74. "Complex (execute)"
  75. );
  76. auto bench_assignment = [&](const String& expression, const char* parse_name, const char* execute_name) {
  77. DataParser parser(expression, interface);
  78. bool result = true;
  79. bench.run(parse_name, [&] {
  80. result &= parser.Parse(true);
  81. });
  82. REQUIRE(result);
  83. Program program = parser.ReleaseProgram();
  84. AddressList addresses = parser.ReleaseAddresses();
  85. DataInterpreter interpreter(program, addresses, interface);
  86. bench.run(execute_name, [&] {
  87. result &= interpreter.Run();
  88. });
  89. REQUIRE(result);
  90. };
  91. bench_assignment(
  92. "radius = 15",
  93. "Simple assign (parse)",
  94. "Simple assign (execute)"
  95. );
  96. bench_assignment(
  97. "radius = radius*radius*3.14; color_name = 'image-color'",
  98. "Complex assign (parse)",
  99. "Complex assign (execute)"
  100. );
  101. }