From d9e93b33a07a15523fab1be60cffe4f46ea492ea Mon Sep 17 00:00:00 2001 From: andri lim Date: Wed, 6 Jun 2018 22:53:02 +0700 Subject: [PATCH] add equality operator --- ranges/typedranges.nim | 4 ++++ tests/ttypedranges.nim | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/ranges/typedranges.nim b/ranges/typedranges.nim index 9794ac2..0ff8651 100644 --- a/ranges/typedranges.nim +++ b/ranges/typedranges.nim @@ -66,6 +66,10 @@ proc `[]`*[T](r: Range[T], idx: int): T {.inline.} = assert(idx < r.len) r.start.shift(idx)[] +proc `==`*[T](a, b: Range[T]): bool = + if a.len != b.len: return false + equalMem(a.start, b.start, sizeof(T) * a.len) + iterator ptrs[T](r: Range[T]): (int, ptr T) = var p = r.start var i = 0 diff --git a/tests/ttypedranges.nim b/tests/ttypedranges.nim index b3b07b9..3a49424 100644 --- a/tests/ttypedranges.nim +++ b/tests/ttypedranges.nim @@ -19,3 +19,10 @@ suite "Typed ranges": let b = toRange(@[1, 2, 3]) a[1 .. 3] = b check a.toSeq == @[0, 1, 2, 3, 0] + + test "equality operator": + var x = toRange(@[0, 1, 2, 3, 4, 5]) + var y = x[1 .. ^2] + var z = toRange(@[1, 2, 3, 4]) + check y == z + check x != z