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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
use rt::{compile, status, slice};
type my_struct = struct {
x: int,
y: int,
};
type my_struct_ptr = *my_struct;
type nomem_alias = nomem;
type my_int = int;
type my_u32 = u32;
fn allocation() void = {
let x = alloc(1234)!;
assert(*x == 1234);
free(x);
let y: (*int | nomem) = alloc(1234);
if (!(y is nomem)) {
assert(*(y as *int) == 1234);
};
free(y as *int);
let z: my_struct_ptr = alloc(my_struct {
x = 42,
y = 69,
})!;
assert(z.x == 42 && z.y == 69);
free(z);
let w: *my_int = alloc(1234)!;
assert(*w == 1234);
free(w);
let a: *const my_u32 = alloc(1234u32)!;
assert(*a == 1234);
free(a);
};
fn failure() (void | nomem_alias) = {
let x = alloc([1u8...], 500000000000000); // 500 TB
assert(x is nomem);
let sl = *(&slice {
data = 0xdeaddead: uintptr: *opaque,
length = 500000000000000,
capacity = 500000000000000,
}: *[]u8);
let x = alloc(sl...);
assert(x is nomem);
let x: (*[500000000000000]u8 | nomem) = alloc([1...]);
assert(x is nomem);
let x: (*[500000000000000]u8 | nomem) = alloc([1...])?;
abort(); // unreachable
};
fn assignment() void = {
let x = alloc(1234)!;
*x = 4321;
assert(*x == 4321);
free(x);
};
fn double_pointer() void = {
let x = alloc(1234)!;
let y = alloc(x)!;
*x = 4321;
assert(**y == 4321);
**y = 1337;
assert(*x == 1337);
free(y);
free(x);
};
fn double_alloc() void = {
let x = alloc(1234)!;
let y = alloc(4321)!;
assert(x != y && *x != *y);
free(x);
free(y);
};
type aslice = []int;
type aarray = [3]int;
fn array() void = {
let aa: *aarray = alloc([0...])!;
free(aa);
let aa: *aarray = alloc([0...])!;
free(aa);
let aa: *aarray = alloc([0...]: aarray)!;
free(aa);
let aa: *aarray = alloc([0...]: aarray)!;
free(aa);
let x: *[24]int = alloc([1, 2...])!;
assert(len(x) == 24);
assert(x[0] == 1);
for (let i = 1z; i < len(x); i += 1) {
assert(x[i] == 2);
};
free(x);
};
fn slice_allocation() void = {
let x: aslice = alloc([1, 2, 3]: aarray, 10)!;
assert(len(x) == 3);
for (let i = 0z; i < len(x); i += 1) {
assert(x[i] == (i + 1): int);
};
free(x);
let y: []int = alloc([1, 2, 3])!;
assert(len(x) == 3);
for (let i = 0z; i < len(y); i += 1) {
assert(y[i] == (i + 1): int);
};
free(y);
let z: []int = [];
let p = &z: *struct {
data: nullable *[*]int,
length: size,
capacity: size,
};
assert(p.data == null && p.length == 0 && p.capacity == 0);
let x: []int = alloc([1, 2, 3...], 42)!;
defer free(x);
assert(x[0] == 1);
assert(x[1] == 2);
assert(x[2] == 3);
for (let i = 2z; i < len(x); i += 1) {
assert(x[i] == 3);
};
// ensure capacity is cast to size correctly
let a: u32 = 4;
let y: []u64 = alloc([1...], a)!;
defer free(y);
assert(len(y) == 4 && y[0] == 1 && y[3] == 1);
};
fn slice_copy() void = {
let x: []int = [1, 2, 3];
let p: []int = alloc(x...)!;
defer free(p);
assert(p: *[*]int != x: *[*]int);
assert(len(p) == len(x));
for (let i = 0z; i < len(p); i += 1) {
assert(p[i] == x[i]);
};
let q: *[]int = alloc(x)!;
defer free(q);
assert((*q): *[*]int == x: *[*]int);
let r: []int = alloc([1, 2, 3]...)!;
defer free(r);
assert(len(x) == len(r));
for (let i = 0z; i < len(x); i += 1) {
assert(x[i] == r[i]);
};
};
fn string() void = {
let x = struct {
data: *[3]int = alloc([1, 2, 3])!,
length: size = 3,
capacity: size = 3,
};
let y = *(&x: *str);
assert(len(y) == 3);
free(y);
};
fn _null() void = {
let x: nullable *int = null;
free(x);
free(null);
};
fn aliases() void = {
compile(status::CHECK,
"fn test() void = { let x: (*u32 | *u64 | nomem) = alloc(123); };"
)!;
};
export fn main() void = {
assert(size(nomem) == 0);
assignment();
assert(failure() is nomem_alias);
allocation();
double_pointer();
double_alloc();
array();
slice_allocation();
slice_copy();
string();
_null();
aliases();
};
|