;; set.sx — Phase 12 Data.Set unit tests.

;; ── SX-level (direct hk-set-*) ───────────────────────────────
(hk-test
  "hk-set-empty: size 0 + null"
  (list (hk-set-size hk-set-empty) (hk-set-null hk-set-empty))
  (list 0 true))

(hk-test
  "hk-set-singleton: member yes"
  (let
    ((s (hk-set-singleton 5)))
    (list (hk-set-size s) (hk-set-member 5 s) (hk-set-member 99 s)))
  (list 1 true false))

(hk-test
  "hk-set-insert: idempotent"
  (let
    ((s (hk-set-insert 1 (hk-set-insert 1 hk-set-empty))))
    (hk-set-size s))
  1)

(hk-test
  "hk-set-from-list: dedupes"
  (hk-set-to-asc-list (hk-set-from-list (list 3 1 4 1 5 9 2 6)))
  (list 1 2 3 4 5 6 9))

(hk-test
  "hk-set-delete: removes"
  (let
    ((s (hk-set-from-list (list 1 2 3))))
    (hk-set-to-asc-list (hk-set-delete 2 s)))
  (list 1 3))

(hk-test
  "hk-set-union"
  (hk-set-to-asc-list
    (hk-set-union
      (hk-set-from-list (list 1 2 3))
      (hk-set-from-list (list 3 4 5))))
  (list 1 2 3 4 5))

(hk-test
  "hk-set-intersection"
  (hk-set-to-asc-list
    (hk-set-intersection
      (hk-set-from-list (list 1 2 3 4))
      (hk-set-from-list (list 3 4 5 6))))
  (list 3 4))

(hk-test
  "hk-set-difference"
  (hk-set-to-asc-list
    (hk-set-difference
      (hk-set-from-list (list 1 2 3 4))
      (hk-set-from-list (list 3 4 5))))
  (list 1 2))

(hk-test
  "hk-set-is-subset-of: yes"
  (hk-set-is-subset-of
    (hk-set-from-list (list 2 3))
    (hk-set-from-list (list 1 2 3 4)))
  true)

(hk-test
  "hk-set-is-subset-of: no"
  (hk-set-is-subset-of
    (hk-set-from-list (list 5 6))
    (hk-set-from-list (list 1 2 3 4)))
  false)

(hk-test
  "hk-set-filter"
  (hk-set-to-asc-list
    (hk-set-filter (fn (k) (> k 2)) (hk-set-from-list (list 1 2 3 4 5))))
  (list 3 4 5))

(hk-test
  "hk-set-map"
  (hk-set-to-asc-list
    (hk-set-map (fn (k) (* k 10)) (hk-set-from-list (list 1 2 3))))
  (list 10 20 30))

(hk-test
  "hk-set-foldr: sum"
  (hk-set-foldr + 0 (hk-set-from-list (list 1 2 3 4 5)))
  15)

;; ── Haskell-level (Set.* via import wiring) ──────────────────
(hk-test
  "Set.size after Set.insert chain"
  (hk-deep-force
    (hk-run
      "import qualified Data.Set as Set\nmain = Set.size (Set.insert 3 (Set.insert 1 (Set.insert 2 Set.empty)))"))
  3)

(hk-test
  "Set.member true"
  (hk-deep-force
    (hk-run
      "import qualified Data.Set as Set\nmain = Set.member 5 (Set.insert 5 Set.empty)"))
  (list "True"))

(hk-test
  "Set.union via Haskell"
  (hk-deep-force
    (hk-run
      "import Data.Set\nmain = Set.size (Set.union (Set.insert 1 Set.empty) (Set.insert 2 Set.empty))"))
  2)

(hk-test
  "Set.isSubsetOf via Haskell"
  (hk-deep-force
    (hk-run
      "import qualified Data.Set as S\nmain = S.isSubsetOf (S.insert 1 S.empty) (S.insert 2 (S.insert 1 S.empty))"))
  (list "True"))

{:fails hk-test-fails :pass hk-test-pass :fail hk-test-fail}
