A "high-level" language for the Gameboy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.6 KiB

  1. import { expect } from "chai"
  2. import { colorGreedy, createGraph, neighbors, vertices } from "../../lib/data/graph"
  3. const g = createGraph((v, e) => {
  4. v("a", true)
  5. v("b", true)
  6. v("c", true)
  7. v("d", true)
  8. e("a", "b")
  9. e("a", "c")
  10. })
  11. const empty = createGraph((v, e) => { })
  12. describe("graph", () => {
  13. describe("neighbors", () => {
  14. it("returns neighbors of vertex", () => {
  15. const ns = neighbors(g, "a")
  16. expect(ns).to.deep.equal(new Set(["b", "c"]))
  17. })
  18. it("returns empty set when vertex has no neighbors", () => {
  19. const ns = neighbors(g, "d")
  20. expect(ns).to.deep.equal(new Set())
  21. })
  22. it("throw on invalid vertex", () => {
  23. expect(() => neighbors(empty, "x")).to.throw()
  24. })
  25. })
  26. describe("vertices", () => {
  27. it("return all vertices for a graph", () => {
  28. const vs = vertices(g)
  29. expect(vs).to.deep.equal(new Set(["a", "b", "c", "d"]))
  30. })
  31. })
  32. describe("colorGreedy", () => {
  33. it("colors graph", () => {
  34. const g = createGraph((v, e) => {
  35. [0, 1, 2, 3, 4].forEach(i => v("x" + i, true))
  36. e("x0", "x1")
  37. e("x1", "x2")
  38. e("x2", "x3")
  39. })
  40. const coloring1 = colorGreedy(g, ["x0", "x1", "x2", "x3", "x4"])
  41. expect(coloring1.numColors).to.equal(2)
  42. const coloring2 = colorGreedy(g, ["x0", "x3", "x1", "x2", "x4"])
  43. expect(coloring2.numColors).to.equal(3)
  44. })
  45. })
  46. })