GaitGeneration by Graph Search
読み取り中…
検索中…
一致する文字列を見つけられません
string_util_test.h
[詳解]
1
3
4// Copyright(c) 2023-2025 Design Engineering Laboratory, Saitama University
5// Released under the MIT license
6// https://opensource.org/licenses/mit-license.php
7
8#ifndef DESIGNLAB_STRING_UTIL_TEST_H_
9#define DESIGNLAB_STRING_UTIL_TEST_H_
10
11#include <doctest.h>
12
13#include <string>
14#include <vector>
15
16#include "string_util.h"
17
18TEST_SUITE("string_util::Split") {
19 std::string StringVectorToString(const std::vector<std::string>& str_vec) {
20 std::string str = "[";
21
22 for (const auto& s : str_vec) {
23 str += s + "]・[";
24 }
25
26 str += "]";
27
28 return str;
29 }
30
31 TEST_CASE(
32 "カンマで区切った時,カンマが取り除かれ区切られた文字列に分けられるべ"
33 "き") {
34 SUBCASE(
35 "文字列「a,b,"
36 "c」をカンマで区切った時,「a」「b」「c」に分けられるべき") {
37 const std::string str = "a,b,c";
38 const std::string delim = ",";
39
40 const std::vector<std::string> ans = {"a", "b", "c"};
41 const std::vector<std::string> result =
43
44 INFO("分割された文字列は次の通りです." << StringVectorToString(result));
45
46 CHECK(result == ans);
47 }
48
49 SUBCASE(
50 "文字列「a,b,c,"
51 "」をカンマで区切った時,「a」「b」「c」に分けられるべき") {
52 const std::string str = "a,b,c,";
53 const std::string delim = ",";
54
55 const std::vector<std::string> ans = {"a", "b", "c"};
56 const std::vector<std::string> result =
58
59 INFO("分割された文字列は次の通りです." << StringVectorToString(result));
60
61 CHECK(result == ans);
62 }
63
64 SUBCASE(
65 "文字列「,a,b,"
66 "c」をカンマで区切った時,「」「a」「b」「c」に分けられるべき") {
67 const std::string str = ",a,b,c";
68 const std::string delim = ",";
69
70 const std::vector<std::string> ans = {"", "a", "b", "c"};
71 const std::vector<std::string> result =
73
74 INFO("分割された文字列は次の通りです." << StringVectorToString(result));
75
76 CHECK(result == ans);
77 }
78 }
79}
80
81TEST_SUITE("string_util::EnumToStringRemoveTopK") {
82 enum class SampleEnum : int { kOne, knight, three, FOUR, Five };
83
84 TEST_CASE(
85 "先頭にkがついた列挙体の要素を渡した時,先頭のkを取り除いた要素名が返るべ"
86 "き") {
87 SUBCASE("「kOne」を渡した時,文字列「One」が返るべき") {
88 const SampleEnum sample_enum = SampleEnum::kOne;
89 const std::string expect = "One";
90 const std::string actual =
92
93 INFO("変換された文字列は次の通りです." << actual);
94
95 CHECK(actual == expect);
96 }
97
98 SUBCASE("「knight」を渡した時,文字列「night」が返るべき") {
99 const SampleEnum sample_enum = SampleEnum::knight;
100 const std::string expect = "night";
101 const std::string actual =
103
104 INFO("変換された文字列は次の通りです." << actual);
105
106 CHECK(actual == expect);
107 }
108 }
109
110 TEST_CASE(
111 "先頭にkがついてない列挙体の要素を渡した時,そのままの要素名が返るべき") {
112 SUBCASE("「three」を渡した時,そのままの要素名が返るべき") {
113 const SampleEnum sample_enum = SampleEnum::three;
114 const std::string expect = "three";
115 const std::string actual =
117
118 INFO("変換された文字列は次の通りです." << actual);
119
120 CHECK(actual == expect);
121 }
122
123 SUBCASE("「FOUR」を渡した時,そのままの要素名が返るべき") {
124 const SampleEnum sample_enum = SampleEnum::FOUR;
125 const std::string expect = "FOUR";
126 const std::string actual =
128
129 INFO("変換された文字列は次の通りです." << actual);
130
131 CHECK(actual == expect);
132 }
133
134 SUBCASE("「Five」を渡した時,そのままの要素名が返るべき") {
135 const SampleEnum sample_enum = SampleEnum::Five;
136 const std::string expect = "Five";
137 const std::string actual =
139
140 INFO("変換された文字列は次の通りです." << actual);
141
142 CHECK(actual == expect);
143 }
144 }
145}
146
147TEST_SUITE("string_util::EnumValuesToString") {
148 enum class SampleEnum : int {
149 kApple,
150 kBanana,
151 kTomato,
152 };
153
154 TEST_CASE(
155 "区切り文字をカンマにした時,文字列「kApple,kBanana,"
156 "kTomato」が返るべき") {
157 const std::string expect = "kApple,kBanana,kTomato";
158 const std::string sep = ",";
159 const std::string actual =
161
162 INFO("変換された文字列は次の通りです." << actual);
163
164 CHECK(actual == expect);
165 }
166}
167
168TEST_SUITE("string_util::EnumEntriesToString") {
169 enum class SampleEnum : int {
170 kApple = 0,
171 kBanana = 1,
172 kTomato = 5,
173 };
174
175 TEST_CASE(
176 "区切り文字をカンマにした時,文字列「kApple = 0,kBanana = 1,kTomato = "
177 "5」が返るべき") {
178 const std::string expect = "kApple = 0,kBanana = 1,kTomato = 5";
179 const std::string sep = ",";
180 const std::string actual =
182
183 INFO("変換された文字列は次の通りです." << actual);
184
185 CHECK(actual == expect);
186 }
187}
188
189#endif // DESIGNLAB_STRING_UTIL_TEST_H_
std::vector< std::string > Split(const std::string &str, const std::string &separator)
文字列を分割する関数.指定した文字で文字列を分割する. 分割した結果,空白が含まれる場合や文字列がない場合は,そのまま返す. 最後が区切り文字で終わる場合は,...
std::string EnumToStringRemoveTopK(const T &enum_value)
enumを文字列に変換する関数. Google C++ coding style だと enumの要素は 先頭にkをつけてキャメルケースで書くことが推奨されている....
Definition string_util.h:52
std::string EnumValuesToString(const std::string separator)
enum型を渡すと,その要素を列挙した文字列を返す関数.
Definition string_util.h:72
TEST_SUITE("string_util::Split")