53 lines
1.3 KiB
Nim
53 lines
1.3 KiB
Nim
import unittest, sequtils
|
|
|
|
proc sameContents*[T](a1, a2: openArray[T]): bool =
|
|
# Answers the question: do these two arrays contain the same contents,
|
|
# regardless of their order?
|
|
if a1.len != a2.len: return false
|
|
for a in a1:
|
|
if not a2.anyIt(a == it): return false
|
|
return true
|
|
|
|
type
|
|
SimpleObj* = object
|
|
strVal*: string
|
|
|
|
TestObj = object
|
|
strVal*: string
|
|
intVal*: int
|
|
objVal*: SimpleObj
|
|
|
|
suite "testutil":
|
|
|
|
test "sameContents":
|
|
let a1 = [1, 2, 3, 4]
|
|
let a2 = [3, 2, 4, 1]
|
|
let a3 = [1, 2, 3, 5]
|
|
let b1 = ["this", "is", "a", "test"]
|
|
let b2 = ["a", "test", "this", "is"]
|
|
let b3 = ["a", "negative", "test", "this", "is"]
|
|
let c1 = [TestObj(strVal: "a", intVal: 1, objVal: SimpleObj(strVal: "innerA")),
|
|
TestObj(strVal: "b", intVal: 2, objVal: SimpleObj(strVal: "innerB"))]
|
|
let c2 = [c1[1], c1[0]]
|
|
|
|
check:
|
|
sameContents(a1, a2)
|
|
sameContents(b2, b1)
|
|
sameContents(c1, c2)
|
|
sameContents(a1, a3) == false
|
|
sameContents(b1, b3) == false
|
|
|
|
test "sameContents (seq)":
|
|
let a1 = @[1, 2, 3, 4]
|
|
let a2 = @[3, 2, 4, 1]
|
|
|
|
check:
|
|
sameContents(a1, a2)
|
|
|
|
test "sameContents (cross-type)":
|
|
let a1 = @[1, 2, 3, 4]
|
|
let a2 = [3, 2, 4, 1]
|
|
|
|
check:
|
|
sameContents(a1, a2)
|