+test.ha (1327B)
1 use allocate; 2 3 @test 4 fn SimpleAlloc() void = { 5 let al = &new(100)!; 6 defer allocate::finish(al); 7 8 let ptr = allocate::create(al, &42: *int, size(int))!: *int; 9 10 assert(*ptr == 42); 11 }; 12 13 @test 14 fn TooSmallAlloc() void = { 15 let al = &new(5)!; 16 defer allocate::finish(al); 17 18 let ptr = allocate::create(al, &[42, 43, 44]: *[]int, size(int) * 3); 19 20 assert(ptr is nomem); 21 }; 22 23 @test 24 fn ExactAlloc() void = { 25 let al = &new(size(int) * 3)!; 26 defer allocate::finish(al); 27 28 let ptr = allocate::create(al, &[42, 43, 44]: *[]int, size(int) * 3): *[*]int; 29 30 assert(ptr[0] == 42); 31 assert(ptr[1] == 43); 32 assert(ptr[2] == 44); 33 }; 34 35 @test 36 fn OneSmallAlloc() void = { 37 let al = &new((size(int) * 3) - 1)!; 38 defer allocate::finish(al); 39 40 let ptr = allocate::create(al, &[42, 43, 44]: *[]int, size(int) * 3); 41 42 assert(ptr is nomem); 43 }; 44 45 @test 46 fn OneBigAlloc() void = { 47 let al = &new((size(int) * 3) + 1)!; 48 defer allocate::finish(al); 49 50 let ptr = allocate::create(al, &[42, 43, 44]: *[]int, size(int) * 3): *[*]int; 51 52 assert(ptr[0] == 42); 53 assert(ptr[1] == 43); 54 assert(ptr[2] == 44); 55 }; 56 57 @test 58 fn MultiAlloc() void = { 59 let al = &new(100)!; 60 defer allocate::finish(al); 61 62 let ptr = allocate::create(al, &42: *int, size(int)): *int; 63 let ptr2 = allocate::create(al, &123: *int, size(int)): *int; 64 65 assert(*ptr == 42); 66 assert(*ptr2 == 123); 67 };