From 9fef0e70e1dc7cd56c2e4f655fe7dc938f1a5e6f Mon Sep 17 00:00:00 2001 From: Ernie Pasveer Date: Sun, 13 Jul 2025 14:39:50 -0500 Subject: [PATCH] Add more Zig test programs. --- tests/hellozig/.gitignore | 17 +++++++++++--- tests/hellozig/Makefile | 31 +++++++++++++++++++++++-- tests/hellozig/arrays.zig | 26 +++++++++++++++++++++ tests/hellozig/floats.zig | 14 ++++++++++++ tests/hellozig/for.zig | 41 ++++++++++++++++++++++++++++++++++ tests/hellozig/hello-world.zig | 21 +++++++++++++++++ tests/hellozig/ifelse.zig | 19 ++++++++++++++++ tests/hellozig/index.zig | 24 ++++++++++++++++++++ tests/hellozig/pointers.zig | 26 +++++++++++++++++++++ tests/hellozig/slices.zig | 25 +++++++++++++++++++++ tests/hellozig/switch.zig | 39 ++++++++++++++++++++++++++++++++ tests/hellozig/while.zig | 38 +++++++++++++++++++++++++++++++ 12 files changed, 316 insertions(+), 5 deletions(-) create mode 100644 tests/hellozig/arrays.zig create mode 100644 tests/hellozig/floats.zig create mode 100644 tests/hellozig/for.zig create mode 100644 tests/hellozig/ifelse.zig create mode 100644 tests/hellozig/index.zig create mode 100644 tests/hellozig/pointers.zig create mode 100644 tests/hellozig/slices.zig create mode 100644 tests/hellozig/switch.zig create mode 100644 tests/hellozig/while.zig diff --git a/tests/hellozig/.gitignore b/tests/hellozig/.gitignore index fd43876..8fdcbe8 100644 --- a/tests/hellozig/.gitignore +++ b/tests/hellozig/.gitignore @@ -1,4 +1,15 @@ -hello-zig -assignment -integers + +hello-world +arrays +assignment +floats +for +ifelse +index +integers +pointers +slices +switch +while +*.o diff --git a/tests/hellozig/Makefile b/tests/hellozig/Makefile index d244f3a..318a191 100644 --- a/tests/hellozig/Makefile +++ b/tests/hellozig/Makefile @@ -1,16 +1,43 @@ .PHONY: all -all: hello-world assignment integers +all: hello-world arrays assignment floats for ifelse integers pointers slices switch while hello-world: hello-world.zig zig build-exe -O Debug hello-world.zig +arrays: arrays.zig + zig build-exe -O Debug arrays.zig + assignment: assignment.zig zig build-exe -O Debug assignment.zig +floats: floats.zig + zig build-exe -O Debug floats.zig + +for: for.zig + zig build-exe -O Debug for.zig + +ifelse: ifelse.zig + zig build-exe -O Debug ifelse.zig + +index: index.zig + zig build-exe -O Debug index.zig + integers: integers.zig zig build-exe -O Debug integers.zig +pointers: pointers.zig + zig build-exe -O Debug pointers.zig + +slices: slices.zig + zig build-exe -O Debug slices.zig + +switch: switch.zig + zig build-exe -O Debug switch.zig + +while: while.zig + zig build-exe -O Debug while.zig + .PHONY: clean clean: - rm -f *.o hello-world assignment integers + rm -f *.o hello-world arrays assignment floats for ifelse index integers pointers slices switch while diff --git a/tests/hellozig/arrays.zig b/tests/hellozig/arrays.zig new file mode 100644 index 0000000..520e682 --- /dev/null +++ b/tests/hellozig/arrays.zig @@ -0,0 +1,26 @@ +const std = @import("std"); +const print = std.debug.print; + +pub fn main() !void { + + const a = [3]i32{ 1, 2, 3 }; + + const b = [_]i32{ 4, 5, 6 }; + + const c: [3]i32 = .{ 7, 8, 9 }; + + var d: [3]i32 = undefined; + d[0] = 10; + d[1] = 11; + d[2] = 12; + + print("len: {}\n", .{c.len}); + + print("repeat: {any}\n", .{a ** 2}); + + print("concat: {any}\n", .{a ++ b}); + + for (d) |elem| { + print("elem: {}\n", .{elem}); + } +} diff --git a/tests/hellozig/floats.zig b/tests/hellozig/floats.zig new file mode 100644 index 0000000..1318bb5 --- /dev/null +++ b/tests/hellozig/floats.zig @@ -0,0 +1,14 @@ +const std = @import("std"); +const print = std.debug.print; + +const a: f16 = 1.0; +const b: f32 = 100.0; +const c: f64 = 1_000.0; +const d: f128 = 10_000.0; + +const e: comptime_float = 100_000.0; +const f = 1_000_000.0; + +pub fn main() !void { + print("float: {}\n", .{f}); +} diff --git a/tests/hellozig/for.zig b/tests/hellozig/for.zig new file mode 100644 index 0000000..6b18aec --- /dev/null +++ b/tests/hellozig/for.zig @@ -0,0 +1,41 @@ +// _For loops_ can be used to iterate over sequences. + +const std = @import("std"); +const print = std.debug.print; + +pub fn main() !void { + var array = [_]u32{ 1, 2, 3 }; + + // Here, we iterate over `array` by _value_, storing a copy of each element + // in `elem`. Note that since `elem` is just a copy, we cannot use it to + // modify `array`'s contents. + for (array) |elem| { + print("by val: {}\n", .{elem}); + } + + // To iterate by _reference_, we can loop over a slice of `array` and + // prefix `elem` with a `*`. Here, `elem` is a pointer to an element in + // `array`, which we can use to modify `array`'s contents. + for (&array) |*elem| { + elem.* += 100; + print("by ref: {}\n", .{elem.*}); + } + + // Here, we iterate over multiple sequences. Note that both sequences + // _must_ have the same length. + for (array, &array) |val, *ref| { + _ = val; + _ = ref; + } + + // You may also specify a _range_ with the `start..end` syntax. Note that + // `end` may be omitted if another sequence is being iterated over as well; + // the compiler will infer the range's size. + for (0.., array) |i, elem| { + print("{}: {}\n", .{ i, elem }); + } + + // To ignore the elements of a sequence, use `_`. + for (array) |_| {} +} + diff --git a/tests/hellozig/hello-world.zig b/tests/hellozig/hello-world.zig index 933ff9b..14041ae 100644 --- a/tests/hellozig/hello-world.zig +++ b/tests/hellozig/hello-world.zig @@ -1,5 +1,26 @@ const std = @import("std"); pub fn main() !void { + std.debug.print("Hello, World!\n", .{}); + + const a = [3]i32{ 1, 2, 3 }; + const b = [_]i32{ 4, 5, 6 }; + const c: [3]i32 = .{ 7, 8, 9 }; + + var d: [3]i32 = undefined; + d[0] = 10; + d[1] = 11; + d[2] = 12; + + std.debug.print("len: {}\n", .{c.len}); + + std.debug.print("repeat: {any}\n", .{a ** 2}); + + std.debug.print("concat: {any}\n", .{a ++ b}); + + for (d) |elem| { + std.debug.print("elem: {}\n", .{elem}); + } } + diff --git a/tests/hellozig/ifelse.zig b/tests/hellozig/ifelse.zig new file mode 100644 index 0000000..b39cb78 --- /dev/null +++ b/tests/hellozig/ifelse.zig @@ -0,0 +1,19 @@ +const std = @import("std"); +const print = std.debug.print; + +pub fn main() !void { + + const a: bool = true; + var x: u16 = 0; + + if (a) { + x += 1; + } else { + x += 2; + } + + print("x = 1? {}\n", .{x == 1}); + + x += if (x == 1) 1 else 2; + print("x now is: {d}\n", .{x}); +} diff --git a/tests/hellozig/index.zig b/tests/hellozig/index.zig new file mode 100644 index 0000000..2db1e70 --- /dev/null +++ b/tests/hellozig/index.zig @@ -0,0 +1,24 @@ +const std = @import("std"); +const parseInt = std.fmt.parseInt; + +test "parse integers" { + const input = "123 67 89,99"; + const ally = std.testing.allocator; + + var list = std.ArrayList(u32).init(ally); + // Ensure the list is freed at scope exit. + // Try commenting out this line! + defer list.deinit(); + + var it = std.mem.tokenizeAny(u8, input, " ,"); + while (it.next()) |num| { + const n = try parseInt(u32, num, 10); + try list.append(n); + } + + const expected = [_]u32{ 123, 67, 89, 99 }; + + for (expected, list.items) |exp, actual| { + try std.testing.expectEqual(exp, actual); + } +} diff --git a/tests/hellozig/pointers.zig b/tests/hellozig/pointers.zig new file mode 100644 index 0000000..f727dc3 --- /dev/null +++ b/tests/hellozig/pointers.zig @@ -0,0 +1,26 @@ +const std = @import("std"); +const print = std.debug.print; + +const Single = *bool; + +const Many = [*]bool; + +const Null = ?*bool; + +pub fn main() !void { + + var v = false; + const ptr: *bool = &v; + print("pointer: {}\n", .{ptr}); + + ptr.* = true; + print("value: {}\n", .{ptr.*}); + + const const_ptr: *bool = &v; + const_ptr.* = false; + + const cf = false; + const ct = true; + var ptr_to_const: *const bool = &cf; + ptr_to_const = &ct; +} diff --git a/tests/hellozig/slices.zig b/tests/hellozig/slices.zig new file mode 100644 index 0000000..269c001 --- /dev/null +++ b/tests/hellozig/slices.zig @@ -0,0 +1,25 @@ +const std = @import("std"); +const print = std.debug.print; + +const Slice = []bool; + +pub fn main() !void { + + var array = [5]i32{ 1, 2, 3, 4, 5 }; + const end: usize = 4; + const slice = array[1..end]; + + print("len: {}\n", .{slice.len}); + print("first: {}\n", .{slice[0]}); + for (slice) |elem| { + print("elem: {}\n", .{elem}); + } + + const ptr: *[3]i32 = array[1..4]; + + print("len: {}\n", .{ptr.len}); + print("first: {}\n", .{ptr[0]}); + for (ptr) |elem| { + print("elem: {}\n", .{elem}); + } +} diff --git a/tests/hellozig/switch.zig b/tests/hellozig/switch.zig new file mode 100644 index 0000000..fbfa84e --- /dev/null +++ b/tests/hellozig/switch.zig @@ -0,0 +1,39 @@ +const std = @import("std"); +const print = std.debug.print; + +pub fn main() !void { + var x: i8 = 10; + + switch (x) { + -1...1 => { + x = -x; + }, + 10, 100 => print("x: {d}\n", .{x}), + else => {}, + } + + var count: u8 = 1; + while (count <= 15) : (count += 1) { + + const div3: u2 = @intFromBool(count % 3 == 0); + + const div5 = @intFromBool(count % 5 == 0); + + switch (div3 * 2 + div5) { + 0b10 => print("Fizz\n", .{}), + 0b01 => print("Buzz\n", .{}), + 0b11 => { + print("Fizz", .{}); + print("Buzz\n", .{}); + }, + else => print("{d}\n", .{count}), + } + } + + x = switch (x) { + -1...1 => -x, + 10, 100 => @divExact(x, 10), + else => x, + }; + print("x: {d}\n", .{x}); +} diff --git a/tests/hellozig/while.zig b/tests/hellozig/while.zig new file mode 100644 index 0000000..4ac02af --- /dev/null +++ b/tests/hellozig/while.zig @@ -0,0 +1,38 @@ +const std = @import("std"); +const print = std.debug.print; + +pub fn main() !void { + var a: usize = 0; + var b: usize = 0; + var c: usize = 0; + var d: usize = 0; + + while (a < 2) { + print("a: {}\n", .{a}); + a += 1; + } + + while (b < 2) : (b += 1) { + print("b: {}\n", .{b}); + } + + while (c < 4) : ({ + c += 1; + c += 1; + }) { + print("c: {}\n", .{c}); + } + + while (true) { + break; + } + + while (true) : (d += 1) { + if (d < 2) { + print("d: {}\n", .{d}); + continue; + } + + break; + } +}