Skip to content

Commit

Permalink
[format] improve string literal readability
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancoisCarouge committed Dec 26, 2024
1 parent 3dc50d4 commit 56a4873
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
10 changes: 8 additions & 2 deletions test/kalman_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,14 @@ namespace {
[[maybe_unused]] auto test{[] {
kalman filter;

assert(std::format("{}", filter) ==
R"({"f": 1, "k": 1, "p": 1, "r": 0, "s": 1, "x": 0, "y": 0, "z": 0})");
assert(std::format("{}", filter) == R"({"f": 1,)"
R"( "k": 1,)"
R"( "p": 1,)"
R"( "r": 0,)"
R"( "s": 1,)"
R"( "x": 0,)"
R"( "y": 0,)"
R"( "z": 0})");

return 0;
}()};
Expand Down
16 changes: 13 additions & 3 deletions test/kalman_format_1x4x3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,19 @@ template <auto Size> using vector = column_vector<double, Size>;
[[maybe_unused]] auto test{[] {
kalman filter{state{vector<1>{0.}}, output<vector<4>>, input<vector<3>>};

assert(
std::format("{}", filter) ==
R"({"f": 1, "g": [1, 0, 0], "h": [[1], [0], [0], [0]], "k": [1, 0, 0, 0], "p": 1, "q": 0, "r": [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], "s": [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], "u": [[0], [0], [0]], "x": 0, "y": [[0], [0], [0], [0]], "z": [[0], [0], [0], [0]]})");
assert(std::format("{}", filter) ==
R"({"f": 1,)"
R"( "g": [1, 0, 0],)"
R"( "h": [[1], [0], [0], [0]],)"
R"( "k": [1, 0, 0, 0],)"
R"( "p": 1,)"
R"( "q": 0,)"
R"( "r": [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],)"
R"( "s": [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]],)"
R"( "u": [[0], [0], [0]],)"
R"( "x": 0,)"
R"( "y": [[0], [0], [0], [0]],)"
R"( "z": [[0], [0], [0], [0]]})");

return 0;
}()};
Expand Down
13 changes: 12 additions & 1 deletion test/kalman_format_5x4x3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,18 @@ template <auto Size> using vector = column_vector<double, Size>;

assert(
std::format("{}", filter) ==
R"({"f": [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]], "g": [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0], [0, 0, 0]], "h": [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0]], "k": [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]], "p": [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]], "q": [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]], "r": [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], "s": [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]], "u": [[0], [0], [0]], "x": [[0], [0], [0], [0], [0]], "y": [[0], [0], [0], [0]], "z": [[0], [0], [0], [0]]})");
R"({"f": [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]],)"
R"( "g": [[1, 0, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0], [0, 0, 0]],)"
R"( "h": [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0]],)"
R"( "k": [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [0, 0, 0, 0]],)"
R"( "p": [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 1]],)"
R"( "q": [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]],)"
R"( "r": [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]],)"
R"( "s": [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]],)"
R"( "u": [[0], [0], [0]],)"
R"( "x": [[0], [0], [0], [0], [0]],)"
R"( "y": [[0], [0], [0], [0]],)"
R"( "z": [[0], [0], [0], [0]]})");

return 0;
}()};
Expand Down
20 changes: 17 additions & 3 deletions test/kalman_format_arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,23 @@ namespace {
update_types<double, double>,
prediction_types<double, double, double>};

assert(
std::format("{}", filter) ==
R"({"f": 1, "g": 1, "h": 1, "k": 1, "p": 1, "prediction_0": 0, "prediction_1": 0, "prediction_2": 0, "q": 0, "r": 0, "s": 1, "u": 0, "update_0": 0, "update_1": 0, "x": 0, "y": 0, "z": 0})");
assert(std::format("{}", filter) == R"({"f": 1,)"
R"( "g": 1,)"
R"( "h": 1,)"
R"( "k": 1,)"
R"( "p": 1,)"
R"( "prediction_0": 0,)"
R"( "prediction_1": 0,)"
R"( "prediction_2": 0,)"
R"( "q": 0,)"
R"( "r": 0,)"
R"( "s": 1,)"
R"( "u": 0,)"
R"( "update_0": 0,)"
R"( "update_1": 0,)"
R"( "x": 0,)"
R"( "y": 0,)"
R"( "z": 0})");

return 0;
}()};
Expand Down
15 changes: 12 additions & 3 deletions test/kalman_format_float_1x1x1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,18 @@ namespace {
[[maybe_unused]] auto test{[] {
kalman filter{state{0.F}, output<float>, input<float>};

assert(
std::format("{}", filter) ==
R"({"f": 1, "g": 1, "h": 1, "k": 1, "p": 1, "q": 0, "r": 0, "s": 1, "u": 0, "x": 0, "y": 0, "z": 0})");
assert(std::format("{}", filter) == R"({"f": 1,)"
R"( "g": 1,)"
R"( "h": 1,)"
R"( "k": 1,)"
R"( "p": 1,)"
R"( "q": 0,)"
R"( "r": 0,)"
R"( "s": 1,)"
R"( "u": 0,)"
R"( "x": 0,)"
R"( "y": 0,)"
R"( "z": 0})");

return 0;
}()};
Expand Down

0 comments on commit 56a4873

Please sign in to comment.