aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/sized.zc
blob: 1be0b315610aa107298a91b8f5d1a2c4715ff461 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
namespace;
include rt, run;

// sized integers: scalars wrap to their size, arrays and pointers of them are packed
struct pix { u8 r, g, b, a; i16 dx; u8[4] tag; };
u8 gbyte = 300;
i8 gneg = 200;
u16 gword[5];
u8[10] run.gbuf;

u8 add8(u8 a, u8 b) { return a + b; }

fn scalars() {
	u8 a = 250;
	i8 s = 100;
	i16 h = 32767;
	u32 w = 4294967295;
	a = a + 10; rt.put_n(a); rt.put_c(' ');			// 4
	s = s + 100; rt.put_n(s); rt.put_c(' ');			// -56
	h++; rt.put_n(h); rt.put_c(' ');				// -32768
	w++; rt.put_n(w); rt.put_c(' ');				// 0
	rt.put_n(gbyte); rt.put_c(' '); rt.put_n(gneg); rt.put_c(' ');	// 44 -56
	rt.put_n(add8(200, 100)); rt.put_c(' ');			// 44
	rt.put_n((i8)200); rt.put_c(' '); rt.put_n(300 as u8); rt.put_c(10);	// -56 44
	return 0;
}
fn arrays() {
	u8 buf[20];
	i16 sh[7];
	u32 ws[3];
	i8 sb[9];
	int i = 0, sum = 0;
	while (i < 20) { buf[i] = i * 20; i++; }	// wraps past 255
	i = 0; while (i < 20) { sum = sum + buf[i]; i++; }
	rt.put_n(sum); rt.put_c(' ');				// 1250
	sh[3] = 40000; rt.put_n(sh[3]); rt.put_c(' ');		// -25536
	sh[3] += 30000; rt.put_n(sh[3]); rt.put_c(' ');		// 4464
	ws[1] = 5000000000; rt.put_n(ws[1]); rt.put_c(' ');	// 705032704
	sb[8] = 127; sb[8]++; rt.put_n(sb[8]); rt.put_c(' ');	// -128
	sb[0] = 200; rt.put_n(sb[0] < 0); rt.put_c(' ');	// 1: -56 is negative
	ws[0] = 4000000000; rt.put_n(ws[0] + ws[0]); rt.put_c(' ');	// arithmetic is done in int: 8000000000
	rt.put_n(sizeof(buf)); rt.put_c(' ');			// 3 words
	rt.put_n(sizeof(sh)); rt.put_c(10);			// 2 words
	return 0;
}
fn heap() {
	u8* p = rt.new(u8, 1000);
	int i = 0, sum = 0;
	while (i < 1000) { p[i] = i; i++; }
	i = 0; while (i < 1000) { sum = sum + p[i]; i++; }
	rt.put_n(sum); rt.put_c(' ');				// 1000 bytes of 0..255: 3 * 32640 + 28 * ... = 127..
	rt.put_n(rt.osize(p) < 256); rt.put_c(32);			// 125 words plus the header, not 1000
	*p = 77; rt.put_n(p[0]); rt.put_c(' ');
	rt.free(d*p);
	run.gbuf[9] = 255; gword[4] = 65535; gword[4]++;
	rt.put_n(run.gbuf[9]); rt.put_c(' '); rt.put_n(gword[4]); rt.put_c(10);
	return 0;
}
fn fields() {
	pix* q = rt.new(pix);
	q.r = 255; q.g = 256; q.dx = 65535; q.tag[2] = 7; q.tag[3] = 300;
	rt.put_n(q.r); rt.put_c(' '); rt.put_n(q.g); rt.put_c(' '); rt.put_n(q.dx); rt.put_c(' ');
	rt.put_n(q.tag[2]); rt.put_c(' '); rt.put_n(q.tag[3]); rt.put_c(' '); rt.put_n(sizeof(pix)); rt.put_c(10);
	rt.free(d*q);
	return 0;
}
o3 fn checksum(u8* p, int n) {
	int i = 0, a = 1, b = 0;
	while (i < n) { a = (a + p[i]) % 251; b = (b + a) % 251; i++; }
	return b * 256 + a;
}
fn o3run() {
	u8* p = rt.new(u8, 5000);
	int i = 0;
	while (i < 5000) { p[i] = i * 7; i++; }
	rt.put_n(checksum(p, 5000)); rt.put_c(10);
	rt.free(d*p);
	return 0;
}
fn main() {
	scalars(); arrays(); heap(); fields(); o3run();
	return 0;
}