You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
1.5 KiB
Zig

const expect = @import("std").testing.expect;
test "if statement" {
const a = true;
var x: u16 = 0;
if (a) {
x += 1;
} else {
x += 2;
}
expect(x == 1);
}
test "if statement expression" {
const a = true;
var x: u16 = 0;
x += if (a) 1 else 2;
expect(x == 1);
}
test "while" {
var i: u8 = 2;
while (i < 100) {
i *= 2;
}
expect(i == 128);
}
test "while with continue expression" {
var sum: u8 = 0;
var i: u8 = 1;
while (i <= 10) : (i += 1) {
sum += i;
}
expect(sum == 55);
}
test "while with continue" {
var sum: u8 = 0;
var i: u8 = 0;
while (i <= 3) : (i += 1) {
if (i == 2) continue;
sum += i;
}
expect(sum == 4);
}
test "while with break" {
var sum: u8 = 0;
var i: u8 = 0;
while (i <= 3) : (i += 1) {
if (i == 2) break;
sum += i;
}
expect(sum == 1);
}
test "for" {
const string = [_]u8{ 'a', 'b', 'c' };
for (string) |character, index| {}
for (string) |character| {}
for (string) |_, index| {}
for (string) |_| {}
}
fn addFive(x: u32) u32 {
return x + 5;
}
test "function" {
const y = addFive(0);
expect(@TypeOf(y) == u32);
expect(y == 5);
}
fn fibonacci(n: u16) u16 {
if (n == 0 or n == 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
test "function recursion" {
const x = fibonacci(10);
expect(x == 55);
}
test "defer" {
var x: i16 = 5;
{
defer x += 2;
expect(x == 5);
}
expect(x == 7);
}