harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

17-alloc.ha (2820B)


      1 type my_struct = struct {
      2 	x: int,
      3 	y: int,
      4 };
      5 
      6 type my_struct_ptr = *my_struct;
      7 
      8 fn allocation() void = {
      9 	let x = alloc(1234);
     10 	assert(*x == 1234);
     11 	free(x);
     12 
     13 	let y: nullable *int = alloc(1234);
     14 	if (y != null) {
     15 		assert(*(y: *int) == 1234);
     16 	};
     17 	free(y);
     18 
     19 	let z: my_struct_ptr = alloc(struct {
     20 		x: int = 42,
     21 		y: int = 69,
     22 	});
     23 	assert(z.x == 42 && z.y == 69);
     24 	free(z);
     25 };
     26 
     27 fn assignment() void = {
     28 	let x = alloc(1234);
     29 	*x = 4321;
     30 	assert(*x == 4321);
     31 	free(x);
     32 };
     33 
     34 fn double_pointer() void = {
     35 	let x = alloc(1234);
     36 	let y = alloc(x);
     37 	*x = 4321;
     38 	assert(**y == 4321);
     39 	**y = 1337;
     40 	assert(*x == 1337);
     41 	free(y);
     42 	free(x);
     43 };
     44 
     45 fn double_alloc() void = {
     46 	let x = alloc(1234);
     47 	let y = alloc(4321);
     48 	assert(x != y && *x != *y);
     49 	free(x);
     50 	free(y);
     51 };
     52 
     53 type aslice = []int;
     54 type aarray = [3]int;
     55 
     56 fn array() void = {
     57 	let aa: *aarray = alloc([0...]);
     58 	free(aa);
     59 	let aa: nullable *aarray = alloc([0...]);
     60 	free(aa);
     61 	let aa: *aarray = alloc([0...]: aarray);
     62 	free(aa);
     63 	let aa: nullable *aarray = alloc([0...]: aarray);
     64 	free(aa);
     65 
     66 	let x: *[24]int = alloc([1, 2...]);
     67 
     68 	assert(len(x) == 24);
     69 	assert(x[0] == 1);
     70 	for (let i = 1z; i < len(x); i += 1) {
     71 		assert(x[i] == 2);
     72 	};
     73 	free(x);
     74 };
     75 
     76 fn slice() void = {
     77 	let x: aslice = alloc([1, 2, 3]: aarray, 10);
     78 	assert(len(x) == 3);
     79 	for (let i = 0z; i < len(x); i += 1) {
     80 		assert(x[i] == (i + 1): int);
     81 	};
     82 	free(x);
     83 
     84 	let y: []int = alloc([1, 2, 3]);
     85 	assert(len(x) == 3);
     86 	for (let i = 0z; i < len(y); i += 1) {
     87 		assert(y[i] == (i + 1): int);
     88 	};
     89 	free(y);
     90 
     91 	let z: []int = [];
     92 	let p = &z: *struct {
     93 		data: nullable *[*]int,
     94 		length: size,
     95 		capacity: size,
     96 	};
     97 	assert(p.data == null && p.length == 0 && p.capacity == 0);
     98 
     99 	let x: []int = alloc([1, 2, 3...], 42);
    100 	defer free(x);
    101 	assert(x[0] == 1);
    102 	assert(x[1] == 2);
    103 	assert(x[2] == 3);
    104 	for (let i = 2z; i < len(x); i += 1) {
    105 		assert(x[i] == 3);
    106 	};
    107 
    108 	// ensure capacity is cast to size correctly
    109 	let a: u32 = 4;
    110 	let y: []u64 = alloc([1...], a);
    111 	defer free(y);
    112 	assert(len(y) == 4 && y[0] == 1 && y[3] == 1);
    113 };
    114 
    115 fn slice_copy() void = {
    116 	let x: []int = [1, 2, 3];
    117 
    118 	let p: []int = alloc(x...);
    119 	defer free(p);
    120 	assert(p: *[*]int != x: *[*]int);
    121 	assert(len(p) == len(x));
    122 	for (let i = 0z; i < len(p); i += 1) {
    123 		assert(p[i] == x[i]);
    124 	};
    125 
    126 	let q: *[]int = alloc(x);
    127 	defer free(q);
    128 	assert((*q): *[*]int == x: *[*]int);
    129 
    130 	let r: []int = alloc([1, 2, 3]...);
    131 	defer free(r);
    132 	assert(len(x) == len(r));
    133 	for (let i = 0z; i < len(x); i += 1) {
    134 		assert(x[i] == r[i]);
    135 	};
    136 };
    137 
    138 fn string() void = {
    139 	let x = struct {
    140 		data: *[3]int = alloc([1, 2, 3]),
    141 		length: size = 3,
    142 		capacity: size = 3,
    143 	};
    144 	let y = *(&x: *str);
    145 	assert(len(y) == 3);
    146 	free(y);
    147 };
    148 
    149 export fn main() void = {
    150 	assignment();
    151 	allocation();
    152 	double_pointer();
    153 	double_alloc();
    154 	array();
    155 	slice();
    156 	slice_copy();
    157 	string();
    158 };