Browse Source

Add basic unit tests

master
Forest Belton 3 years ago
parent
commit
b7b5514247
5 changed files with 2537 additions and 24 deletions
  1. +0
    -0
      lib/graph.ts
  2. +2230
    -24
      package-lock.json
  3. +8
    -0
      package.json
  4. +40
    -0
      test/graph.spec.ts
  5. +259
    -0
      test/ir.spec.ts

lib/graph/graph.ts → lib/graph.ts View File


+ 2230
- 24
package-lock.json
File diff suppressed because it is too large
View File


+ 8
- 0
package.json View File

@ -1,12 +1,20 @@
{ {
"devDependencies": { "devDependencies": {
"@types/chai": "^4.2.21",
"@types/mocha": "^9.0.0",
"@types/node": "^16.9.1", "@types/node": "^16.9.1",
"chai": "^4.3.4",
"mocha": "^8.4.0",
"peggy": "^1.2.0", "peggy": "^1.2.0",
"ts-mocha": "^8.0.0",
"typescript": "^4.4.3" "typescript": "^4.4.3"
}, },
"name": "gbuoy", "name": "gbuoy",
"version": "1.0.0", "version": "1.0.0",
"main": "gbuoy.js", "main": "gbuoy.js",
"mocha": {
"spec": "test/**/*.spec.ts"
},
"directories": { "directories": {
"lib": "lib" "lib": "lib"
}, },

+ 40
- 0
test/graph.spec.ts View File

@ -0,0 +1,40 @@
import { expect } from "chai"
import { createGraph, neighbors, vertices } from "../lib/graph"
const g = createGraph((v, e) => {
v("a", true)
v("b", true)
v("c", true)
v("d", true)
e("a", "b")
e("a", "c")
})
const empty = createGraph((v, e) => { })
describe("graph", () => {
describe("neighbors", () => {
it("returns neighbors of vertex", () => {
const ns = neighbors(g, "a")
expect(ns).to.deep.equal(new Set(["b", "c"]))
})
it("returns empty set when vertex has no neighbors", () => {
const ns = neighbors(g, "d")
expect(ns).to.deep.equal(new Set())
})
it("throw on invalid vertex", () => {
expect(() => neighbors(empty, "x")).to.throw()
})
})
describe("vertices", () => {
it("return all vertices for a graph", () => {
const vs = vertices(g)
expect(vs).to.deep.equal(new Set(["a", "b", "c", "d"]))
})
})
})

+ 259
- 0
test/ir.spec.ts View File

@ -0,0 +1,259 @@
import { expect } from "chai"
import { convertAssignStmt, prettyPrintIR, SSA } from "../lib/ir"
describe("ir", () => {
describe("convertExpr", () => {
it("load with immediate", () => {
const state = { nextID: 0, ssa_stmts: [] }
const ir = convertAssignStmt(state, {
type: "stmt",
stmt_type: "assign",
args: {
name: "x",
expr: 42,
},
})
expect(ir).to.deep.equal([
{
dest: {
type: "variable",
name: "x",
},
source: 42,
},
])
})
it("load with variable", () => {
const state = { nextID: 0, ssa_stmts: [] }
const ir = convertAssignStmt(state, {
type: "stmt",
stmt_type: "assign",
args: {
name: "x",
expr: "y",
},
})
expect(ir).to.deep.equal([
{
dest: {
type: "variable",
name: "x",
},
source: {
type: "variable",
name: "y",
},
},
])
})
it("unary op", () => {
const state = { nextID: 0, ssa_stmts: [] }
const ir = convertAssignStmt(state, {
type: "stmt",
stmt_type: "assign",
args: {
name: "x",
expr: {
type: "unary",
op: "arith_negate",
arg: "y"
},
},
})
expect(ir).to.deep.equal([
{
dest: {
type: "variable",
name: "x",
},
source: {
type: "variable",
name: "y",
},
op: "arith_negate",
},
])
})
it("binary op", () => {
const state = { nextID: 0, ssa_stmts: [] }
const ir = convertAssignStmt(state, {
type: "stmt",
stmt_type: "assign",
args: {
name: "x",
expr: {
type: "binary",
op: "add",
left: "y",
right: 42,
},
},
})
expect(ir).to.deep.equal([
{
dest: {
type: "variable",
name: "x",
},
source: {
type: "variable",
name: "y",
},
source1: 42,
op: "add",
},
])
})
it("nested binary op", () => {
const state = { nextID: 0, ssa_stmts: [] }
const ir = convertAssignStmt(state, {
type: "stmt",
stmt_type: "assign",
args: {
name: "x",
expr: {
type: "binary",
op: "add",
left: "y",
right: {
type: "binary",
op: "subtract",
left: "z",
right: 42,
},
},
},
})
expect(ir).to.deep.equal([
{
dest: {
type: "temporary",
name: "t0",
},
source: {
type: "variable",
name: "z",
},
op: "subtract",
source1: 42,
},
{
dest: {
type: "variable",
name: "x",
},
source: {
type: "variable",
name: "y",
},
op: "add",
source1: {
type: "temporary",
name: "t0",
},
},
])
})
})
describe("prettyPrintIR", () => {
it("load with immediate", () => {
const ssa: SSA = {
dest: {
type: "variable",
name: "x",
},
source: 42,
}
expect(prettyPrintIR(ssa)).to.equal("x = 42")
})
it("load with register", () => {
const ssa: SSA = {
dest: {
type: "variable",
name: "x",
},
source: {
type: "register",
name: "A"
},
}
expect(prettyPrintIR(ssa)).to.equal("x = %A")
})
it("load with temporary", () => {
const ssa: SSA = {
dest: {
type: "variable",
name: "x",
},
source: {
type: "temporary",
name: "t0"
},
}
expect(prettyPrintIR(ssa)).to.equal("x = #t0")
})
it("load with variable", () => {
const ssa: SSA = {
dest: {
type: "variable",
name: "x",
},
source: {
type: "variable",
name: "y"
},
}
expect(prettyPrintIR(ssa)).to.equal("x = y")
})
it("unary op", () => {
const ssa: SSA = {
dest: {
type: "variable",
name: "x",
},
source: {
type: "variable",
name: "y"
},
op: "arith_negate",
}
expect(prettyPrintIR(ssa)).to.equal("x = arith_negate(y)")
})
it("binary op", () => {
const ssa: SSA = {
dest: {
type: "variable",
name: "x",
},
source: {
type: "variable",
name: "y"
},
source1: 42,
op: "add",
}
expect(prettyPrintIR(ssa)).to.equal("x = add(y, 42)")
})
})
})

Loading…
Cancel
Save