tsetutils.nim 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. discard """
  2. targets: "c js"
  3. """
  4. import std/setutils
  5. import std/assertions
  6. type
  7. Colors = enum
  8. red, green = 5, blue = 10
  9. Bar = enum
  10. bar0 = -1, bar1, bar2
  11. template main =
  12. block: # toSet
  13. doAssert "abcbb".toSet == {'a', 'b', 'c'}
  14. doAssert toSet([10u8, 12, 13]) == {10u8, 12, 13}
  15. doAssert toSet(0u16..30) == {0u16..30}
  16. type A = distinct char
  17. doAssert [A('x')].toSet == {A('x')}
  18. block: # fullSet
  19. doAssert fullSet(Colors) == {red, green, blue}
  20. doAssert fullSet(char) == {0.chr..255.chr}
  21. doAssert fullSet(Bar) == {bar0, bar1, bar2}
  22. doAssert fullSet(bool) == {true, false}
  23. block: # complement
  24. doAssert {red, blue}.complement == {green}
  25. doAssert (complement {red, green, blue}).card == 0
  26. doAssert (complement {false}) == {true}
  27. doAssert {bar0}.complement == {bar1, bar2}
  28. doAssert {range[0..10](0), 1, 2, 3}.complement == {range[0..10](4), 5, 6, 7, 8, 9, 10}
  29. doAssert {'0'..'9'}.complement == {0.char..255.char} - {'0'..'9'}
  30. block: # `[]=`
  31. type A = enum
  32. a0, a1, a2, a3
  33. var s = {a0, a3}
  34. s[a0] = false
  35. s[a1] = false
  36. doAssert s == {a3}
  37. s[a2] = true
  38. s[a3] = true
  39. doAssert s == {a2, a3}
  40. main()
  41. static: main()