2017-05-09 19:23:38 -07:00
|
|
|
const assert = @import("std").debug.assert;
|
2017-12-12 18:50:37 -08:00
|
|
|
const mem = @import("std").mem;
|
2017-05-09 19:23:38 -07:00
|
|
|
|
2017-05-19 07:39:59 -07:00
|
|
|
const x = @intToPtr(&i32, 0x1000)[0..0x500];
|
|
|
|
const y = x[0x100..];
|
2017-05-09 19:23:38 -07:00
|
|
|
test "compile time slice of pointer to hard coded address" {
|
2017-08-08 14:38:25 -07:00
|
|
|
assert(@ptrToInt(x.ptr) == 0x1000);
|
2017-05-09 19:23:38 -07:00
|
|
|
assert(x.len == 0x500);
|
|
|
|
|
2017-08-08 14:38:25 -07:00
|
|
|
assert(@ptrToInt(y.ptr) == 0x1100);
|
2017-05-09 19:23:38 -07:00
|
|
|
assert(y.len == 0x400);
|
|
|
|
}
|
2017-09-13 11:30:57 -07:00
|
|
|
|
|
|
|
test "slice child property" {
|
|
|
|
var array: [5]i32 = undefined;
|
|
|
|
var slice = array[0..];
|
|
|
|
assert(@typeOf(slice).Child == i32);
|
|
|
|
}
|
2017-12-12 18:50:37 -08:00
|
|
|
|
2018-01-24 22:46:12 -08:00
|
|
|
test "runtime safety lets us slice from len..len" {
|
2017-12-12 18:50:37 -08:00
|
|
|
var an_array = []u8{1, 2, 3};
|
|
|
|
assert(mem.eql(u8, sliceFromLenToLen(an_array[0..], 3, 3), ""));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) -> []u8 {
|
|
|
|
return a_slice[start..end];
|
|
|
|
}
|
2017-12-19 19:38:02 -08:00
|
|
|
|
|
|
|
test "implicitly cast array of size 0 to slice" {
|
|
|
|
var msg = []u8 {};
|
|
|
|
assertLenIsZero(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn assertLenIsZero(msg: []const u8) {
|
|
|
|
assert(msg.len == 0);
|
|
|
|
}
|