tsetutils.nim 1.2 KB

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