tests: Execute and test format functions

This commit is contained in:
WerWolv
2025-05-25 12:43:15 +02:00
parent 2070c95f58
commit d96bfbb942
3 changed files with 41 additions and 3 deletions

View File

@@ -19,7 +19,7 @@ namespace old_binary {
}; };
fn format_time(u32 value) { fn format_time(u32 value) {
return std::time::format(std::time::to_utc(swap_32bit(value))); return std::time::format(std::time::to_utc(old_binary::swap_32bit(value)));
}; };
using SwappedU32 = u32 [[transform("old_binary::swap_32bit"), format("old_binary::swap_32bit")]]; using SwappedU32 = u32 [[transform("old_binary::swap_32bit"), format("old_binary::swap_32bit")]];

View File

@@ -117,7 +117,10 @@ namespace fmt {
}; };
fn const_ref(u2 index) { fn const_ref(u2 index) {
cp_info info = file.constant_pool[index-1]; return fmt::format_info_const_ref(index, file.constant_pool[index-1]);
};
fn format_info_const_ref(u2 index, ref auto info) {
match(info.tag) { match(info.tag) {
(1): return info.bytes; (1): return info.bytes;
(3): return std::format("{:d} [{:d}]", index, info.bytes); (3): return std::format("{:d} [{:d}]", index, info.bytes);
@@ -140,7 +143,10 @@ namespace fmt {
}; };
fn const_ref_top(u2 index) { fn const_ref_top(u2 index) {
cp_info info = file.constant_pool[index-1]; return fmt::format_info_const_ref_top(index, file.constant_pool[index-1]);
};
fn format_info_const_ref_top(u2 index, ref auto info) {
match(info.tag) { match(info.tag) {
(1): return std::format("{:d} [{:s}]", index, info.bytes); (1): return std::format("{:d} [{:s}]", index, info.bytes);
(_): return fmt::const_ref(index); (_): return fmt::const_ref(index);

View File

@@ -6,8 +6,29 @@
#include <wolv/utils/string.hpp> #include <wolv/utils/string.hpp>
#include <cstdlib> #include <cstdlib>
#include <pl/patterns/pattern.hpp>
#define EXIT_SKIP 77 #define EXIT_SKIP 77
bool validatePatternValues(pl::ptrn::Pattern *pattern) {
auto value = pattern->getFormattedValue();
if (!pattern->hasValidFormattedValue()) {
fmt::print("Invalid formatted value \"{}\" of pattern \"{}\"\n", value, pattern->getDisplayName());
return false;
}
for (const auto &[address, child] : pattern->getChildren()) {
if (pattern == child)
continue;
child->setOffset(address);
if (!validatePatternValues(child))
return false;
}
return true;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
// Any number of arguments other than 5 are invalid // Any number of arguments other than 5 are invalid
if (argc != 4 && argc != 5) if (argc != 4 && argc != 5)
@@ -87,6 +108,17 @@ int main(int argc, char **argv) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
for (const auto &pattern : runtime.getPatterns()) {
if (!validatePatternValues(pattern.get()))
return EXIT_FAILURE;
}
for (const auto &[id, section] : runtime.getSections()) {
for (const auto &pattern : runtime.getPatterns(id)) {
if (!validatePatternValues(pattern.get()))
return EXIT_FAILURE;
}
}
} else { } else {
// Parse the file // Parse the file
fmt::println("Parsing pattern {} without executing it", patternName); fmt::println("Parsing pattern {} without executing it", patternName);