shithub: mc

ref: 50c1c9eeaadb2d41ce4c5feadfc211ec75e03c1d
dir: /libstd/test.myr/

View raw version
use std

const ntstr = {s
	var n

	n = 0
	while s[n] != 0 && n < s.len
		n++
	;;
	-> s[:n]
}

const main = {args : byte[:][:]
	var x : byte#[1024]
	var sz
	var i
        var ctx
        var o
	var a
	var buf

	std.put("The time is %l seconds past the epoch\n", std.now()/1_000_000);
	std.uname(&buf)
	std.put("And you are running on:\n")
	std.put("\tsystem:\t\"%s\"\n", ntstr(buf.system[:]))
	std.put("\tnode:\t\"%s\"\n", ntstr(buf.node[:]))
	std.put("\trelease:\t\"%s\"\n", ntstr(buf.release[:]))
	std.put("\tmachine:\t\"%s\"\n", ntstr(buf.machine[:]))
        ctx = std.optinit("asdf:g?h", args)
	std.put("arglen = %i\n", ctx.args.len)
        while !std.optdone(ctx)
		(o, a) = std.optnext(ctx)
		if o == 'h'
			usage()
		;;
		std.put("option %c, arg = %s\n", o, a)
        ;;

	std.put("env.len = %i\n", std._environment.len)
	for i = 0; i < std._environment.len; i++
		std.put("env[%i] = %s\n", i, std._environment[i])
	;;
	std.put("args.len = %i\n", args.len)
	for i = 0; i < args.len; i++
		std.put("args[%i] = %s\n", i, args[i])
	;;

	for i = 0; i < ctx.args.len; i++
		std.put("arg %s\n", ctx.args[i])
	;;
	printenv("SHELL")

        
	/* try the byte allocator for large variety of sizes. */
	for sz = 1; sz < 65536; sz *= 2
		for i = 0; i < 1024; i++
			x[i] = std.bytealloc(sz)
		;;
		for i = 0; i < 1024; i++
			std.bytefree(x[i], sz)
		;;
	;;
	
	/* make sure the generic allocator works */
	for i = 0; i < 1024; i++
		x[i] = std.alloc()
	;;
	for i = 0; i < 1024; i++
		std.free(x[i])
	;;
	std.write(1, "Hello, 世界\n")
	chartypes()
	testrng()
	std.put("format output %i %i %s %s\n", 123, 321, "asdf", "מִלָּה")
	std.put("format with no args\n")
}

const chartypes = {
	var s
	var c
	var buf : byte[32]

	s = " 1世界 äa\n"
	while s.len != 0
		(c, s) = std.striter(s)
		if std.isspace(c)
			std.write(1, "Space\n")
		elif std.isalpha(c)
			std.write(1, "Alpha\n")
		elif std.isnum(c)
			std.write(1, "Num\n")
		else
			std.write(1, "Dunno\n")
		;;
		if !std.encode(buf[:std.charlen(c)], c)
			std.write(1, "couldn't encode\n")
		;;
		std.write(1, buf[:std.charlen(c)])
		std.write(1, "\n")
	;;
	if !std.encode(buf[0:3], -1)
		std.write(1, "couldn't encode\n")
	;;
}

const testrng = {
	var r
	var i

	r = std.mksrng(10)
	for i = 0; i < 300; i++
		std.put("r[%i] = %l\n", i, std.rand(r, 5, 10) castto(int64))
	;;
	std.put("\n");
}

const usage = {
	std.put("Pokes a bit at the standard library.\n")
	std.put("Option string is asdf:g?h\n")
	std.exit(0)
}

const printenv = {name
	match std.getenv(name)
	| `std.Some env:
		std.put("Value of %s is %s\n", name, env)
	| `std.None:
		std.put("No env var %s is set\n", name)
	;;
}