two-bit/ram.test.ts

78 lines
2.2 KiB
TypeScript

import { ram } from "./ram"
import { Word, emptyWord, wordFromString } from "./word"
describe("Ram", () => {
test("begins life with the specified number of empty words of the specified width", () =>{
const subject = ram({
width: 6,
size: 4
})
expect(subject.values().every(word => word.equals(wordFromString("000000")))).toBe(true)
})
test("can be created from an array of strings", () => {
const subject = ram([
"0000",
"1111"
])
expect(subject.values().map(word => word.toString())).toEqual([
"0000",
"1111"
])
})
test("can be created from an array of words", () => {
const subject = ram([
wordFromString("0000"),
wordFromString("1000"),
wordFromString("1011"),
wordFromString("0101"),
wordFromString("1111"),
wordFromString("1111")
])
expect(subject.values().map(word => word.toString())).toEqual([
"0000",
"1000",
"1011",
"0101",
"1111",
"1111"
])
})
test("reports its word size", () => {
const subject = ram([wordFromString("001")])
expect(subject.wordSize).toEqual(3)
})
test("reports its address length", () => {
expect(ram({ size: 48, width: 8 }).addressLength).toEqual(6)
expect(ram({ size: 4, width: 8 }).addressLength).toEqual(2)
expect(ram({ size: 5, width: 8 }).addressLength).toEqual(3)
expect(ram({ size: 8, width: 8 }).addressLength).toEqual(3)
})
test("returns the memory value for a given index", () => {
const subject = ram({
width: 4,
size: 4
})
subject.write(wordFromString("1010") as Word<4>, wordFromString("10"))
expect(subject.read(wordFromString("10")).equals(wordFromString("1010"))).toBe(true)
})
test("returns the memory for a given index", () => {
const subject = ram([
"0000",
"0001"
])
expect(subject.get(wordFromString("1")).value().toString()).toEqual("0001")
})
})