Are these considered equal?
const a = [_:0]u8{ '4', '2'};
const b = [_]u8{ '4', '2'};
I am not sure how I could even check the sentinels match, or if it even
matters :) but since I still have some custom string utils (some with
sentinels, and some without (wasm not C)), I want to also have some
tests to make sure they are adding/not adding sentinels in the correct
place. But then I get index of of bounds... Any tips on how to test
such functions please? Cheers Peter
On Mon, 01 Aug 2022 15:55 +0200, Peter Bridge wrote:
>Are these considered equal?>>const a = [_:0]u8{ '4', '2'};>const b = [_]u8{ '4', '2'};>>I am not sure how I could even check the sentinels match, or if it >even matters :) but since I still have some custom string utils (some >with sentinels, and some without (wasm not C)), I want to also have >some tests to make sure they are adding/not adding sentinels in the >correct place. But then I get index of of bounds... Any tips on how >to test such functions please? Cheers Peter
"Equal" in what sense? They have different types, but they would compare
equal element-wise.
Sentinels are encoded in the type system. To check if a given variable
has a sentinel you can check the type info using @typeInfo, e.g.
@typeInfo(@TypeOf(a)).Array.sentinel
There are also some handy functions in std.meta for this kind of
thing, e.g. std.meta.sentinel:
const std = @import("std");
test {
const a = [_:0]u8{ '4', '2' };
const b = [_]u8{ '4', '2' };
try std.testing.expectEqual(std.meta.sentinel(@TypeOf(a)), 0);
try std.testing.expectEqual(std.meta.sentinel(@TypeOf(b)), null);
}
Greg
Hi Greg, Perfect! just what I was looking for, and makes total sense.
Many thanks, Peter
On 02/08/2022 03:32, Gregory Anders wrote:
> On Mon, 01 Aug 2022 15:55 +0200, Peter Bridge wrote:>> Are these considered equal?>>>> const a = [_:0]u8{ '4', '2'};>> const b = [_]u8{ '4', '2'};>>>> I am not sure how I could even check the sentinels match, or if it >> even matters :) but since I still have some custom string utils (some >> with sentinels, and some without (wasm not C)), I want to also have >> some tests to make sure they are adding/not adding sentinels in the >> correct place. But then I get index of of bounds... Any tips on how >> to test such functions please? Cheers Peter>> "Equal" in what sense? They have different types, but they would compare> equal element-wise.>> Sentinels are encoded in the type system. To check if a given variable> has a sentinel you can check the type info using @typeInfo, e.g.>> @typeInfo(@TypeOf(a)).Array.sentinel>> There are also some handy functions in std.meta for this kind of> thing, e.g. std.meta.sentinel:>> const std = @import("std");> test {> const a = [_:0]u8{ '4', '2' };> const b = [_]u8{ '4', '2' };>> try std.testing.expectEqual(std.meta.sentinel(@TypeOf(a)), 0);> try std.testing.expectEqual(std.meta.sentinel(@TypeOf(b)), null);> }>> Greg> .>