zig/test/cases/math.zig

110 lines
2.2 KiB
Zig
Raw Normal View History

fn exactDivision() {
@setFnTest(this);
assert(divExact(55, 11) == 5);
}
fn divExact(a: u32, b: u32) -> u32 {
@divExact(a, b)
}
fn floatDivision() {
@setFnTest(this);
assert(fdiv32(12.0, 3.0) == 4.0);
}
fn fdiv32(a: f32, b: f32) -> f32 {
a / b
}
fn overflowIntrinsics() {
@setFnTest(this);
var result: u8 = undefined;
assert(@addWithOverflow(u8, 250, 100, &result));
assert(!@addWithOverflow(u8, 100, 150, &result));
assert(result == 250);
}
fn shlWithOverflow() {
@setFnTest(this);
var result: u16 = undefined;
assert(@shlWithOverflow(u16, 0b0010111111111111, 3, &result));
assert(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result));
assert(result == 0b1011111111111100);
}
fn countLeadingZeroes() {
@setFnTest(this);
assert(@clz(u8(0b00001010)) == 4);
assert(@clz(u8(0b10001010)) == 0);
assert(@clz(u8(0b00000000)) == 8);
}
fn countTrailingZeroes() {
@setFnTest(this);
assert(@ctz(u8(0b10100000)) == 5);
assert(@ctz(u8(0b10001010)) == 1);
assert(@ctz(u8(0b00000000)) == 8);
}
2016-12-21 21:55:21 -08:00
fn modifyOperators() {
@setFnTest(this);
var i : i32 = 0;
i += 5; assert(i == 5);
i -= 2; assert(i == 3);
i *= 20; assert(i == 60);
i /= 3; assert(i == 20);
i %= 11; assert(i == 9);
i <<= 1; assert(i == 18);
i >>= 2; assert(i == 4);
i = 6;
i &= 5; assert(i == 4);
i ^= 6; assert(i == 2);
i = 6;
i |= 3; assert(i == 7);
}
2016-12-21 22:20:08 -08:00
fn threeExprInARow() {
@setFnTest(this);
assertFalse(false || false || false);
assertFalse(true && true && false);
assertFalse(1 | 2 | 4 != 7);
assertFalse(3 ^ 6 ^ 8 != 13);
assertFalse(7 & 14 & 28 != 4);
assertFalse(9 << 1 << 2 != 9 << 3);
assertFalse(90 >> 1 >> 2 != 90 >> 3);
assertFalse(100 - 1 + 1000 != 1099);
assertFalse(5 * 4 / 2 % 3 != 1);
assertFalse(i32(i32(5)) != 5);
assertFalse(!!false);
assertFalse(i32(7) != --(i32(7)));
}
fn assertFalse(b: bool) {
assert(!b);
}
fn constNumberLiteral() {
@setFnTest(this);
const one = 1;
const eleven = ten + one;
assert(eleven == 11);
}
const ten = 10;
2016-12-21 21:55:21 -08:00
// TODO const assert = @import("std").debug.assert;
fn assert(ok: bool) {
if (!ok)
@unreachable();
}