Commit 5b258718 authored by Mol Thomas Folkert's avatar Mol Thomas Folkert
Browse files

code generator generates code, framework now in place for writing more tests

parent 1223ae43
......@@ -108,10 +108,11 @@ pub const ZIG_VOLATILE_LOOP_TEMPLATE =
/// 0 (string) = general type, 1 (string) = function name
/// 2 (int) = array size, 3 (string) = array
pub const C_PASSED_LOOP_TEMPLATE =
\\{0s} _internal_passed_p({0s} _lo, {0s} *_hi, {0s} []_arr) {{
\\{0s} _internal_passed_p({0s} _lo, {0s} *_hi, {0s} _arr[]) {{
\\ {0s} _acc = 0;
\\ for ({0s} i = _lo; i < *_hi, ++i) {{
\\ for ({0s} i = _lo; i < *_hi; ++i) {{
\\ _acc += _arr[i];
\\ }}
\\ return _acc;
\\}}
\\
......@@ -154,7 +155,7 @@ pub const C_FUNCTION_LOOP_TEMPLATE =
\\ return {2d};
\\}}
\\
\\{0s} _fn_arr[{0s}] = {3s};
\\{0s} _fn_arr[{2d}] = {3s};
\\
\\{0s} *_arr_fn() {{
\\ return _fn_arr;
......@@ -163,7 +164,7 @@ pub const C_FUNCTION_LOOP_TEMPLATE =
\\{0s} _internal_passed_fn({0s} (*_hi_fn_p)(), {0s} *(*_arr_fn_p)()) {{
\\ {0s} _lo = 0;
\\ {0s} _acc = 0;
\\ for ({0s} i = lo; i < (*_hi_fn_p)(); ++i)
\\ for ({0s} i = _lo; i < (*_hi_fn_p)(); ++i)
\\ _acc += (*_arr_fn_p)()[i];
\\ return _acc;
\\}}
......@@ -206,29 +207,29 @@ pub const ZIG_FUNCTION_LOOP_TEMPLATE =
/// General testing template for running a function and printing its duration.
/// Include inside a function body.
/// Argument index:
/// 0 (string) = function name (to test)
/// 0 (string) = general type, 1 (string) = function name (to test)
pub const C_TEST_FUNCTION_TEMPLATE =
\\ struct timespec {0s}_start;
\\ struct timespec {0s}_end;
\\ clock_gettime(CLOCK_MONOTONIC_RAW, &{0s}_start);
\\ {0s}();
\\ clock_gettime(CLOCK_MONOTONIC_RAW, &{0s}_end);
\\ double {0s}_duration = {0s}_end.tv_nsec - {0s}_start.tv_nsec;
\\ printf("{0s} done. Time: %d\n.", {0s}_duration);
\\ struct timespec {1s}_start = {{0, 0}};
\\ struct timespec {1s}_end = {{0, 0}};
\\ clock_gettime(CLOCK_MONOTONIC_RAW, &{1s}_start);
\\ {0s} {1s}_result = {1s}();
\\ clock_gettime(CLOCK_MONOTONIC_RAW, &{1s}_end);
\\ double {1s}_duration = {1s}_end.tv_nsec - {1s}_start.tv_nsec;
\\ printf("{1s} done. Result: %d. Duration: %d nanoseconds\n", {1s}_result, {1s}_duration);
\\
\\
;
pub const ZIG_TEST_FUNCTION_TEMPLATE =
\\ const {0s}_stdout = std.io.getStdOut().writer();
\\
\\ var {0s}_start: ctime.timespec = .{{.tv_sec = 0, .tv_nsec = 0}};
\\ var {0s}_end: ctime.timespec = .{{.tv_sec = 0, .tv_nsec = 0}};
\\ _ = ctime.clock_gettime(ctime.CLOCK_MONOTONIC_RAW, &{0s}_start);
\\ _ = {0s}();
\\ _ = ctime.clock_gettime(ctime.CLOCK_MONOTONIC_RAW, &{0s}_end);
\\ var {0s}_duration: c_long = {0s}_end.tv_nsec - {0s}_start.tv_nsec;
\\ try {0s}_stdout.print("{0s} done. {{d}} nanoseconds\n", .{{{0s}_duration}});
\\ const {1s}_stdout = std.io.getStdOut().writer();
\\
\\ var {1s}_start: ctime.timespec = .{{.tv_sec = 0, .tv_nsec = 0}};
\\ var {1s}_end: ctime.timespec = .{{.tv_sec = 0, .tv_nsec = 0}};
\\ _ = ctime.clock_gettime(ctime.CLOCK_MONOTONIC_RAW, &{1s}_start);
\\ {1s}_result: {0s} = {1s}();
\\ _ = ctime.clock_gettime(ctime.CLOCK_MONOTONIC_RAW, &{1s}_end);
\\ var {1s}_duration: c_long = {1s}_end.tv_nsec - {1s}_start.tv_nsec;
\\ try {1s}_stdout.print("{1s} done. Result: {{d}}. Duration: {{d}} nanoseconds\n", .{{{1s}_result, {1s}_duration}});
\\
\\
;
......@@ -3,41 +3,105 @@ const templates = @import("template_strings.zig");
const fs = std.fs;
const TestFunction = struct {
name: []const u8,
ctemplate: []const u8,
zigtemplate: []const u8,
};
const TestParameters = struct {
ctype: []const u8,
zigtype: []const u8,
arr_size: usize,
array: []const u8,
};
const c_output = "c_generated_tests.c";
const zig_output = "z_generated_tests.zig";
const c_i32 = "int32_t";
const zig_i32 = "i32";
const simple_array = "{1, 2, 3, 4, 5}";
fn create_array_string() *const [15:0]u8 {
return simple_array;
}
const comptime_args = .{ zig_i32, "comptime_loop", 5, simple_array };
const volatile_args = .{ zig_i32, "volatile_loop", 5, simple_array };
const passed_args = .{ zig_i32, "passed_loop", 5, simple_array };
const function_args = .{ zig_i32, "function_loop", 5, simple_array };
fn write_zig_tests() !void {
fn write_c_test(writer: anytype, comptime func: TestFunction, param: TestParameters) !void {
try writer.print(func.ctemplate, .{ param.ctype, func.name, param.arr_size, param.array });
}
fn write_zig_test(writer: anytype, comptime func: TestFunction, param: TestParameters) !void {
try writer.print(func.zigtemplate, .{ param.zigtype, func.name, param.arr_size, param.array });
}
const test_params = TestParameters{
.ctype = "int32_t",
.zigtype = "i32",
.arr_size = 5,
.array = simple_array,
};
fn write_c_tests(comptime tests: []const TestFunction) !void {
const file = try fs.cwd().createFile(c_output, .{});
defer file.close();
const writer = fs.File.writer(file);
try writer.print(templates.C_HEADER, .{});
inline for (tests) |val| {
try write_c_test(writer, val, test_params);
}
try writer.print("int main(void) {{\n", .{});
for (tests) |val| {
try writer.print(templates.C_TEST_FUNCTION_TEMPLATE, .{ test_params.ctype, val.name });
}
try writer.print("}}\n", .{});
}
fn write_zig_tests(comptime tests: []const TestFunction) !void {
const file = try fs.cwd().createFile(zig_output, .{});
defer file.close();
const writer = fs.File.writer(file);
try writer.print(templates.ZIG_HEADER, .{});
try writer.print(templates.ZIG_COMPTIME_LOOP_TEMPLATE, comptime_args);
try writer.print(templates.ZIG_VOLATILE_LOOP_TEMPLATE, volatile_args);
try writer.print(templates.ZIG_PASSED_LOOP_TEMPLATE, passed_args);
try writer.print(templates.ZIG_FUNCTION_LOOP_TEMPLATE, function_args);
inline for (tests) |val| {
try write_zig_test(writer, val, test_params);
}
try writer.print("pub fn main() !void {{\n", .{});
try writer.print(templates.ZIG_TEST_FUNCTION_TEMPLATE, .{"comptime_loop"});
try writer.print(templates.ZIG_TEST_FUNCTION_TEMPLATE, .{"volatile_loop"});
try writer.print(templates.ZIG_TEST_FUNCTION_TEMPLATE, .{"passed_loop"});
try writer.print(templates.ZIG_TEST_FUNCTION_TEMPLATE, .{"function_loop"});
for (tests) |val| {
try writer.print(templates.ZIG_TEST_FUNCTION_TEMPLATE, .{ test_params.zigtype, val.name });
}
try writer.print("}}\n", .{});
}
const comptime_func = TestFunction{
.name = "comptime_loop",
.ctemplate = templates.C_COMPTIME_LOOP_TEMPLATE,
.zigtemplate = templates.ZIG_COMPTIME_LOOP_TEMPLATE,
};
const volatile_func = TestFunction{
.name = "volatile_loop",
.ctemplate = templates.C_VOLATILE_LOOP_TEMPLATE,
.zigtemplate = templates.ZIG_VOLATILE_LOOP_TEMPLATE,
};
const passed_func = TestFunction{
.name = "passed_loop",
.ctemplate = templates.C_PASSED_LOOP_TEMPLATE,
.zigtemplate = templates.ZIG_PASSED_LOOP_TEMPLATE,
};
const function_func = TestFunction{
.name = "function_loop",
.ctemplate = templates.C_FUNCTION_LOOP_TEMPLATE,
.zigtemplate = templates.ZIG_FUNCTION_LOOP_TEMPLATE,
};
const run_tests: []const TestFunction = &[_]TestFunction{ comptime_func, volatile_func, passed_func, function_func };
pub fn main() !void {
try write_zig_tests();
try write_c_tests(run_tests);
try write_zig_tests(run_tests);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment