Add strSplitHelp test cases

Add a test for the case when the string starts with the delimiter, and
when the string and the delimiter are equal.
This commit is contained in:
raleng 2022-07-28 01:34:20 +02:00
parent 6b6f240acb
commit 261ff3224d
No known key found for this signature in database
GPG key ID: A7DBF700E0E77020

View file

@ -766,7 +766,6 @@ fn strFromFloatHelp(comptime T: type, float: T) RocStr {
}
// Str.split
pub fn strSplit(string: RocStr, delimiter: RocStr) callconv(.C) RocList {
const segment_count = countSegments(string, delimiter);
const list = RocList.allocate(@alignOf(RocStr), segment_count, @sizeOf(RocStr));
@ -892,6 +891,46 @@ test "strSplitHelp: no delimiter" {
try expect(array[0].eq(expected[0]));
}
test "strSplitHelp: empty start" {
const str_arr = "/a";
const str = RocStr.init(str_arr, str_arr.len);
const delimiter_arr = "/";
const delimiter = RocStr.init(delimiter_arr, delimiter_arr.len);
const array_len: usize = 2;
var array: [array_len]RocStr = [_]RocStr{
undefined,
undefined,
};
const array_ptr: [*]RocStr = &array;
strSplitHelp(array_ptr, str, delimiter);
const one = RocStr.init("a", 1);
var expected = [2]RocStr{
RocStr.empty(), one,
};
defer {
for (array) |rocStr| {
rocStr.deinit();
}
for (expected) |rocStr| {
rocStr.deinit();
}
str.deinit();
delimiter.deinit();
}
try expectEqual(array.len, expected.len);
try expect(array[0].eq(expected[0]));
try expect(array[1].eq(expected[1]));
}
test "strSplitHelp: empty end" {
const str_arr = "1---- ---- ---- ---- ----2---- ---- ---- ---- ----";
const str = RocStr.init(str_arr, str_arr.len);
@ -935,6 +974,40 @@ test "strSplitHelp: empty end" {
try expect(array[2].eq(expected[2]));
}
test "strSplitHelp: string equals delimiter" {
const str_delimiter_arr = "/";
const str_delimiter = RocStr.init(str_delimiter_arr, str_delimiter_arr.len);
const array_len: usize = 2;
var array: [array_len]RocStr = [_]RocStr{
undefined,
undefined,
};
const array_ptr: [*]RocStr = &array;
strSplitHelp(array_ptr, str_delimiter, str_delimiter);
var expected = [2]RocStr{
RocStr.empty(), RocStr.empty()
};
defer {
for (array) |rocStr| {
rocStr.deinit();
}
for (expected) |rocStr| {
rocStr.deinit();
}
str_delimiter.deinit();
}
try expectEqual(array.len, expected.len);
try expect(array[0].eq(expected[0]));
try expect(array[1].eq(expected[1]));
}
test "strSplitHelp: delimiter on sides" {
const str_arr = "tttghittt";
const str = RocStr.init(str_arr, str_arr.len);