mirror of
https://github.com/epasveer/seer.git
synced 2026-08-29 08:34:42 +08:00
Add more Zig test programs.
This commit is contained in:
@@ -1,4 +1,15 @@
|
||||
hello-zig
|
||||
assignment
|
||||
integers
|
||||
|
||||
hello-world
|
||||
arrays
|
||||
assignment
|
||||
floats
|
||||
for
|
||||
ifelse
|
||||
index
|
||||
integers
|
||||
pointers
|
||||
slices
|
||||
switch
|
||||
while
|
||||
*.o
|
||||
|
||||
|
||||
+29
-2
@@ -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
|
||||
|
||||
|
||||
@@ -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});
|
||||
}
|
||||
}
|
||||
@@ -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});
|
||||
}
|
||||
@@ -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) |_| {}
|
||||
}
|
||||
|
||||
@@ -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});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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});
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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});
|
||||
}
|
||||
}
|
||||
@@ -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});
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user