User Guide
codegen_utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <catch2/catch_test_macros.hpp>
9 
12 
13 using namespace nmodl;
14 using namespace visitor;
15 using namespace codegen;
16 
17 using input_result_map = std::unordered_map<std::string, std::string>;
18 
19 SCENARIO("C codegen utility functions", "[codegen][util][c]") {
20  GIVEN("Double constant as string") {
21  std::string double_constant = "0.012345678901234567";
22 
23  THEN("Codegen C Visitor prints double with same precision") {
24  auto nmodl_constant_result = codegen::utils::format_double_string(double_constant);
25  REQUIRE(nmodl_constant_result == double_constant);
26  }
27  }
28 
29  GIVEN("Integer constant as string") {
30  std::string double_constant = "1";
31 
32  std::string codegen_output = "1.0";
33 
34  THEN("Codegen C Visitor prints integer as double number") {
35  auto nmodl_constant_result = codegen::utils::format_double_string(double_constant);
36  REQUIRE(nmodl_constant_result == codegen_output);
37  }
38  }
39 
40  GIVEN("Double constants in scientific notation as strings") {
41  input_result_map tests({{"1e+18", "1e+18"}, {"1e-18", "1e-18"}, {"1E18", "1E18"}});
42 
43  THEN("Codegen C Visitor prints doubles with scientific notation") {
44  for (const auto& test: tests) {
45  REQUIRE(codegen::utils::format_double_string(test.first) == test.second);
46  }
47  }
48  }
49 
50  GIVEN("Float constant as string") {
51  std::string float_constant = "0.01234567";
52 
53  THEN("Codegen C Visitor prints float with same precision") {
54  auto nmodl_constant_result = codegen::utils::format_float_string(float_constant);
55  REQUIRE(nmodl_constant_result == float_constant);
56  }
57  }
58 
59  GIVEN("Float constant as string") {
60  std::string float_constant = "1";
61 
62  std::string codegen_output = "1.0";
63 
64  THEN("Codegen C Visitor prints integer as double number") {
65  auto nmodl_constant_result = codegen::utils::format_float_string(float_constant);
66  REQUIRE(nmodl_constant_result == codegen_output);
67  }
68  }
69 
70  GIVEN("Float constants in scientific notation as strings") {
71  input_result_map tests({{"1e+18", "1e+18"}, {"1e-18", "1e-18"}, {"1E18", "1E18"}});
72 
73  THEN("Codegen C Visitor prints doubles with scientific notation") {
74  for (const auto& test: tests) {
75  REQUIRE(codegen::utils::format_float_string(test.first) == test.second);
76  }
77  }
78  }
79 }
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::codegen::utils::format_float_string
std::string format_float_string(const std::string &s_value)
Handles the float constants format being printed in the generated code.
Definition: codegen_utils.cpp:32
codegen_cpp_visitor.hpp
Visitor for printing C++ code compatible with legacy api of CoreNEURON
SCENARIO
SCENARIO("C codegen utility functions", "[codegen][util][c]")
Definition: codegen_utils.cpp:19
input_result_map
std::unordered_map< std::string, std::string > input_result_map
Definition: codegen_utils.cpp:17
codegen_utils.hpp
Implement utility functions for codegen visitors.
nmodl::codegen::utils::format_double_string
std::string format_double_string(const std::string &s_value)
Handles the double constants format being printed in the generated code.
Definition: codegen_utils.cpp:23