Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3d67220

Browse files
authored
Path Schema Ordered updates (#230)
1 parent3f37a04 commit3d67220

File tree

5 files changed

+193
-35
lines changed

5 files changed

+193
-35
lines changed

‎buildcc/lib/target/include/target/custom_generator/custom_generator_context.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ namespace buildcc {
2626
classCustomGeneratorContext {
2727
public:
2828
CustomGeneratorContext(const env::Command &c,
29-
const std::unordered_set<std::string> &i,
30-
const std::unordered_set<std::string> &o,
29+
const std::vector<std::string> &i,
30+
const std::vector<std::string> &o,
3131
const std::vector<uint8_t> &ub)
3232
: command(c), inputs(i), outputs(o), userblob(ub) {}
3333

3434
const env::Command &command;
35-
const std::unordered_set<std::string> &inputs;
36-
const std::unordered_set<std::string> &outputs;
35+
const std::vector<std::string> &inputs;
36+
const std::vector<std::string> &outputs;
3737
const std::vector<uint8_t> &userblob;
3838
};
3939

‎buildcc/lib/target/test/target/test_custom_generator.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ TEST(CustomGeneratorTestGroup, RealGenerate_Update_Success) {
375375
CHECK_EQUAL(imap.at("id2").outputs.GetPaths().size(),1);
376376

377377
STRCMP_EQUAL(std::to_string(last_write_timestamp).c_str(),
378-
imap.at("id2").inputs.GetPathInfos().begin()->second.c_str());
378+
imap.at("id2").inputs.GetPathInfos()[0].hash.c_str());
379379

380380
CHECK(buildcc::env::get_task_state() == buildcc::env::TaskState::SUCCESS);
381381
}

‎buildcc/schema/cmake/schema.cmake‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ if (${TESTING})
3131
target_link_options(mock_schemaPUBLIC${TEST_LINK_FLAGS}${BUILD_LINK_FLAGS})
3232

3333
# Tests
34+
add_executable(test_path_schema
35+
test/test_path_schema.cpp
36+
)
37+
target_link_libraries(test_path_schemaPRIVATE mock_schema)
38+
3439
add_executable(test_custom_generator_serialization
3540
test/test_custom_generator_serialization.cpp
3641
)
@@ -41,6 +46,9 @@ if (${TESTING})
4146
)
4247
target_link_libraries(test_target_serializationPRIVATE mock_schema)
4348

49+
add_test(NAME test_path_schemaCOMMAND test_path_schema
50+
WORKING_DIRECTORY${CMAKE_CURRENT_SOURCE_DIR}/test
51+
)
4452
add_test(NAME test_custom_generator_serializationCOMMAND test_custom_generator_serialization
4553
WORKING_DIRECTORY${CMAKE_CURRENT_SOURCE_DIR}/test
4654
)

‎buildcc/schema/include/schema/path.h‎

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,7 @@ struct Path {
103103
*/
104104
static std::stringToPathString(const std::string &str) {
105105
auto path_str = str;
106-
std::replace(path_str.begin(), path_str.end(),'\\','/');
107-
path_str =fs::path(path_str).lexically_normal().string();
106+
path_str =fs::path(path_str).make_preferred().lexically_normal().string();
108107
return path_str;
109108
}
110109

@@ -189,11 +188,11 @@ class PathList {
189188

190189
voidEmplace(const std::string &pstr) {
191190
auto path_str =Path::ToPathString(pstr);
192-
paths_.emplace(std::move(path_str));
191+
paths_.emplace_back(std::move(path_str));
193192
}
194193

195194
boolIsEqual(const PathList &other)const {return paths_ == other.paths_; }
196-
const std::unordered_set<std::string> &GetPaths()const {return paths_; }
195+
const std::vector<std::string> &GetPaths()const {return paths_; }
197196

198197
friendvoidto_json(json &j,const PathList &plist) { j = plist.paths_; }
199198

@@ -202,7 +201,7 @@ class PathList {
202201
}
203202

204203
private:
205-
std::unordered_set<std::string> paths_;
204+
std::vector<std::string> paths_;
206205
};
207206

208207
/**
@@ -211,10 +210,34 @@ class PathList {
211210
*/
212211
classPathInfoList {
213212
private:
214-
staticconstexprconstchar *constkPath ="path";
215-
staticconstexprconstchar *constkHash ="hash";
216-
217213
public:
214+
structPathInfo {
215+
private:
216+
staticconstexprconstchar *constkPath ="path";
217+
staticconstexprconstchar *constkHash ="hash";
218+
219+
public:
220+
PathInfo() =default;
221+
PathInfo(const std::string &p,const std::string &h) : path(p), hash(h) {}
222+
223+
booloperator==(const PathInfo &other)const {
224+
return ((path == other.path) && (hash == other.hash));
225+
}
226+
227+
friendvoidto_json(json &j,const PathInfo &info) {
228+
j[kPath] = info.path;
229+
j[kHash] = info.hash;
230+
}
231+
232+
friendvoidfrom_json(const json &j, PathInfo &info) {
233+
j.at(kPath).get_to(info.path);
234+
j.at(kHash).get_to(info.hash);
235+
}
236+
237+
std::string path;
238+
std::string hash;
239+
};
240+
218241
PathInfoList() =default;
219242
explicitPathInfoList(
220243
std::initializer_list<std::pair<const std::string, std::string>>
@@ -226,34 +249,31 @@ class PathInfoList {
226249

227250
voidEmplace(const std::string &pstr,const std::string &hash) {
228251
auto path_str =Path::ToPathString(pstr);
229-
path_infos_.emplace(std::move(path_str), hash);
252+
infos_.emplace_back(PathInfo(path_str, hash));
230253
}
231254

232-
voidComputeHashForAll() {
233-
for (auto &[path_str, hash] : path_infos_) {
234-
hash =ComputeHash(path_str);
255+
voidInsert(const PathInfoList &other) {
256+
for (constauto &info : other.infos_) {
257+
Emplace(info.path, info.hash);
235258
}
236259
}
237260

238-
const std::string &GetHash(const std::string &str)const {
239-
auto path_str =Path::ToPathString(str);
240-
constbool found = path_infos_.find(path_str) != path_infos_.end();
241-
env::assert_fatal(found,"");
242-
return path_infos_.at(path_str);
261+
voidComputeHashForAll() {
262+
for (auto &info : infos_) {
263+
info.hash =ComputeHash(info.path);
264+
}
243265
}
244266

245267
boolIsEqual(const PathInfoList &other)const {
246-
returnpath_infos_ == other.path_infos_;
268+
returninfos_ == other.infos_;
247269
}
248270

249-
const std::unordered_map<std::string, std::string> &GetPathInfos()const {
250-
return path_infos_;
251-
}
271+
const std::vector<PathInfo> &GetPathInfos()const {return infos_; }
252272

253-
std::unordered_set<std::string>GetPaths()const {
254-
std::unordered_set<std::string> paths;
255-
for (constauto &[path_str, hash] : path_infos_) {
256-
paths.emplace(path_str);
273+
std::vector<std::string>GetPaths()const {
274+
std::vector<std::string> paths;
275+
for (constauto &info : infos_) {
276+
paths.emplace_back(info.path);
257277
}
258278
return paths;
259279
}
@@ -274,16 +294,14 @@ class PathInfoList {
274294
returnstd::to_string(last_write_timestamp);
275295
}
276296

277-
friendvoidto_json(json &j,const PathInfoList &plist) {
278-
j = plist.path_infos_;
279-
}
297+
friendvoidto_json(json &j,const PathInfoList &plist) { j = plist.infos_; }
280298

281299
friendvoidfrom_json(const json &j, PathInfoList &plist) {
282-
j.get_to(plist.path_infos_);
300+
j.get_to(plist.infos_);
283301
}
284302

285303
private:
286-
std::unordered_map<std::string, std::string> path_infos_;
304+
std::vector<PathInfo> infos_;
287305
};
288306

289307
// TODO, Remove this
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Internal
2+
#include"schema/path.h"
3+
4+
#include"env/host_os.h"
5+
6+
// NOTE, Make sure all these includes are AFTER the system and header includes
7+
#include"CppUTest/CommandLineTestRunner.h"
8+
#include"CppUTest/MemoryLeakDetectorNewMacros.h"
9+
#include"CppUTest/TestHarness.h"
10+
#include"CppUTest/Utest.h"
11+
12+
// clang-format off
13+
TEST_GROUP(PathSchemaTestGroup)
14+
{
15+
};
16+
// clang-format on
17+
18+
TEST(PathSchemaTestGroup, PathList) { buildcc::internal::PathList paths; }
19+
20+
TEST(PathSchemaTestGroup, Path_ToPathString) {
21+
auto path_str =buildcc::internal::Path::ToPathString("hello/\\first.txt");
22+
23+
ifconstexpr (buildcc::env::is_win()) {
24+
STRCMP_EQUAL("hello\\first.txt", path_str.c_str());
25+
}elseifconstexpr (buildcc::env::is_linux() ||buildcc::env::is_mac() ||
26+
buildcc::env::is_unix()) {
27+
STRCMP_EQUAL("hello/\\first.txt", path_str.c_str());
28+
}else {
29+
FAIL("Operating system not supported");
30+
}
31+
}
32+
33+
TEST(PathSchemaTestGroup, PathInfoList_OrderedEmplace) {
34+
buildcc::internal::PathInfoList path_infos;
35+
36+
path_infos.Emplace("hello/world/first_file.txt","");
37+
path_infos.Emplace("hello/world/second_file.txt","");
38+
path_infos.Emplace("hello/world/third_file.txt","");
39+
path_infos.Emplace("hello/world/fourth_file.txt","");
40+
41+
std::vector<std::string> paths;
42+
ifconstexpr (buildcc::env::is_win()) {
43+
paths = {
44+
"hello\\world\\first_file.txt",
45+
"hello\\world\\second_file.txt",
46+
"hello\\world\\third_file.txt",
47+
"hello\\world\\fourth_file.txt",
48+
};
49+
}elseifconstexpr (buildcc::env::is_linux() ||buildcc::env::is_unix() ||
50+
buildcc::env::is_mac()) {
51+
paths = {
52+
"hello/world/first_file.txt",
53+
"hello/world/second_file.txt",
54+
"hello/world/third_file.txt",
55+
"hello/world/fourth_file.txt",
56+
};
57+
}else {
58+
FAIL("Operating system not supported");
59+
}
60+
61+
auto inserted_paths = path_infos.GetPaths();
62+
for (std::size_t i =0; i < inserted_paths.size(); i++) {
63+
STRCMP_EQUAL(paths[i].c_str(), inserted_paths[i].c_str());
64+
}
65+
66+
json j = path_infos;
67+
UT_PRINT(j.dump().c_str());
68+
}
69+
70+
TEST(PathSchemaTestGroup, PathInfoList_GetChanged) {
71+
{
72+
buildcc::internal::PathInfoList pinfolist1{
73+
{"first.txt","1"},
74+
{"second.txt","2"},
75+
{"third.txt","3"},
76+
};
77+
buildcc::internal::PathInfoList pinfolist2{
78+
{"first.txt","1"},
79+
{"second.txt","2"},
80+
{"third.txt","3"},
81+
};
82+
CHECK_TRUE(pinfolist1.IsEqual(pinfolist2));
83+
}
84+
85+
// Missing path info
86+
{
87+
buildcc::internal::PathInfoList pinfolist1{
88+
{"first.txt","1"},
89+
{"second.txt","2"},
90+
{"third.txt","3"},
91+
};
92+
buildcc::internal::PathInfoList pinfolist2{
93+
{"first.txt","1"}, {"second.txt","2"},
94+
// {"third.txt", "3"},
95+
};
96+
CHECK_FALSE(pinfolist1.IsEqual(pinfolist2));
97+
}
98+
99+
// Wrong hash
100+
{
101+
buildcc::internal::PathInfoList pinfolist1{
102+
{"first.txt","1"},
103+
{"second.txt","2"},
104+
{"third.txt","3"},
105+
};
106+
buildcc::internal::PathInfoList pinfolist2{
107+
{"first.txt","1"},
108+
{"second.txt","2"},
109+
{"third.txt","4"},
110+
};
111+
CHECK_FALSE(pinfolist1.IsEqual(pinfolist2));
112+
}
113+
114+
// Wrong order
115+
{
116+
buildcc::internal::PathInfoList pinfolist1{
117+
{"first.txt","1"},
118+
{"second.txt","2"},
119+
{"third.txt","3"},
120+
};
121+
buildcc::internal::PathInfoList pinfolist2{
122+
{"first.txt","1"},
123+
{"third.txt","3"},
124+
{"second.txt","2"},
125+
};
126+
CHECK_FALSE(pinfolist1.IsEqual(pinfolist2));
127+
}
128+
}
129+
130+
intmain(int ac,char **av) {
131+
returnCommandLineTestRunner::RunAllTests(ac, av);
132+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp