#!/usr/bin/ruby

# Tests for sets

var engineers = Set('John', 'Jane', 'Jack', 'Janice')   #=> Set("Janice", "Jack", "John", "Jane")
var programmers = Set('Jack', 'Sam', 'Susan', 'Janice') #=> Set("Janice", "Jack", "Susan", "Sam")
var managers = Set('Jane', 'Jack', 'Susan', 'Zack')     #=> Set("Jack", "Zack", "Susan", "Jane")

var employees = (engineers | programmers | managers)    #=> Set("Jane", "Jack", "Zack", "Susan", "John", "Sam", "Janice")
assert_eq(employees.sort, ["Jack", "Jane", "Janice", "John", "Sam", "Susan", "Zack"])

var engineering_management = (engineers & managers)     #=> Set("Jane", "Jack")
assert_eq(engineering_management.sort, ["Jack", "Jane"])

var fulltime_management = (managers - engineers - programmers)  #=> Set("Zack")

assert_eq(fulltime_management.sort, ["Zack"])
assert_eq(fulltime_management, Set("Zack"))
assert_ne(fulltime_management, Set("Foo"))

engineers.add('Marvin')    # add "Marvin" to the engineers set
assert_eq(engineers.len, 5)
assert_eq(engineers.sort, ["Jack", "Jane", "Janice", "John", "Marvin"])

assert(!employees.is_superset(engineers), "employees is not a superset of engineers")
employees |= engineers

assert(employees.is_superset(engineers), "employees is not a superset of engineers")
assert_eq(employees, Set("Jack", "Jane", "Janice", "John", "Marvin", "Sam", "Susan", "Zack"))

var tests = [
    ["Jack", "Jane", "Janice", "John", "Marvin"],
    ["Jack", "Janice", "Sam"],
    ["Jack", "Jane", "Zack"],
    ["Jack", "Jane", "Janice", "John", "Marvin", "Sam", "Zack"]
]

for group in [engineers, programmers, managers, employees] {
   group.discard("Susan")
   say group
   assert_eq(group.sort, tests.shift)
}

var a = Set()
for x in (Set("a", "a", "b", "c", 42)) {
    a.add(x)
}

assert_eq(a, Set(42, "a", "b", "c"))
assert_eq(a, Set("a", "b", "c", 42))

assert_eq(a.clone, a)
assert_eq(a.dclone, a)

assert_eq(Set(42, 99, 12, 17).map {|n| n+1 }, Set(43, 100, 13, 18))
assert_eq(Set(42, 99, 12, 17).map {|n| n+1 }.sort, [13, 18, 43, 100])

assert_eq(Set(42, 99, 13, 20).grep{.is_even}, Set(42, 20))
assert_eq(Set(42, 99, 13, 20).grep{.is_even}.sort, [20, 42])
assert_eq(Set(12, 3, 4, 2, 4).to_bag, Bag(2, 3, 4, 12))
assert_eq(Set(12, 3, 4, 2, 4).to_bag.to_set, Set(2, 3, 4, 12))
assert_eq(Set(12, 3, 4, 2, 4).to_bag.to_set.sort, [2, 3, 4, 12])

assert_eq(Set(1, 2, 3, 4).map { (_, _**2) }, Set(1, 2, 3, 4, 9, 16))

do {
    var a = Set("John", "Serena", "Bob", "Mary", "Serena")
    var b = Set("Jim", "Mary", "John", "Jim", "Bob")
    assert_eq(a ^ b, Set("Serena", "Jim"))
    assert_eq(a ^ b, Set("Jim", "Serena"))
    assert_eq((a ^ b).sort, ["Jim", "Serena"])
}

assert(Set(3,4)   ≡ Set(3,4))
assert(Set(3,3,4) ≡ Set(4,3))
assert(Set(3,3,4) ≡ Set(4,3,4,4,3,3,3))

assert(!(Set(3,4) ≡ Set(4,3,5)))
assert(!(Set(3,4) ≡ Set(4,3,5)))
assert(!(Set(3,4) ≡ Set(2,3)))
