Commit f3039cc5 authored by Mol Thomas Folkert's avatar Mol Thomas Folkert
Browse files

added zig version of some tests from c

parent 2e2f8caa
null_and_bounds
null_and_bounds_c
*.gch
zig-cache/
CC = clang
CCFLAGS = -O3
TARGET = null_and_bounds
TARGET = null_and_bounds_c
DEP = null_and_bounds_externals.h
all: $(TARGET)
......
const std = @import("std");
const ctime = @cImport({
@cInclude("time.h");
});
fn nab_comptime() i32 {
const lo: i32 = 0;
const hi: i32 = 14000;
const arr = [_]i32{0} ** 14000;
var tmp: i32 = 0;
var i: usize = lo;
while (i < hi) : (i += 1) {
tmp += arr[i];
}
return tmp;
}
fn nab_volhiarr() i32 {
const lo: i32 = 0;
var hi_p: usize = 14000;
var hi: *volatile usize = &hi_p;
var arr_p = [_]i32{0} ** 14000;
var arr: []volatile i32 = &arr_p;
var tmp: i32 = 0;
var i: usize = lo;
while (i < hi.*) : (i += 1) {
tmp += arr[i];
}
return tmp;
}
fn nab_passed(lo: i32, hi: i32, arr: []i32) i32 {
var tmp: i32 = 0;
var i: usize = lo;
while (i < hi) : (i += 1) {
tmp += arr[i];
}
return tmp;
}
fn hi_fun() i32 {
return 14000;
}
var funarr: [14000]i32 = [_]i32{0} ** 14000;
fn arr_fun(i: usize) [14000]i32 {
funarr[i] *= -1;
return funarr;
}
fn nab_funhiarr(hifun: fn () i32, arrfun: fn (usize) [14000]i32) i32 {
const lo: i32 = 0;
var tmp: i32 = 0;
var i: usize = lo;
while (i < hifun()) : (i += 1) {
tmp += arrfun(i)[i];
}
return tmp;
}
fn run_test(function: fn () i32, name: *const [8:0]u8) !void {
const stdout = std.io.getStdOut().writer();
var start = ctime.clock();
var result = function();
var time = @intToFloat(f64, ctime.clock() - start) / @intToFloat(f64, ctime.CLOCKS_PER_SEC);
try stdout.print("{s} done! Time: {d} Result: {}\n", .{ name, time, result });
}
pub fn main() !void {
var t = arr_fun(0);
try run_test(nab_comptime, "Comptime");
try run_test(nab_volhiarr, "Volhiarr");
const stdout = std.io.getStdOut().writer();
var start = ctime.clock();
var result = nab_funhiarr(hi_fun, arr_fun);
var time = @intToFloat(f64, ctime.clock() - start) / @intToFloat(f64, ctime.CLOCKS_PER_SEC);
try stdout.print("Funhiarr done! Time: {d} Result: {}\n", .{ time, result });
}
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