apple2js/test/js/util.test.ts
Ian Flanigan c4df78cf06
Typescript conversion of several files, including js/cpu6502 (#38)
* Convert `js/util.js` to Typescript and add tests

Besides converting `js/util.js` to Typescript, this change also adds
`js/types.ts` that defines common types used in apple2js. Some of
these types, like `byte` and `word` are for information only.

* Convert `js/base64.js` to Typescript

This also adds a new type, `memory`, that is either an array of
numbers, or a Uint8Array.

* Convert `js/ram.js` to Typescript

This change does not convert `RAM` to a class; it just introduces types.

* Basic typing of cpu6502

This is a really rough first pass. There are some problems that can't
be fixed until this is turned into a real class, but at least all of
the function arguments are now typed. This caught a few cases where
extra arguments were being passed in.

* Convert `js/cpu6502` to a class

In theory, idiomatic classes should be better than the previous
closure-based classes. However, this conversion shows that the
instruction table does not fit well with idiomatic classes as method
referenced in the table need to be called with the correct `this`
everywhere.

This should, at best, be considered a first attempt.
2020-11-01 08:43:48 -08:00

99 lines
2.7 KiB
TypeScript

/** @fileoverview Test for utils.ts. */
import { allocMem, allocMemPages, numToString, testables, toBinary, toHex } from "../../js/util";
describe('garbage', () => {
it('returns 0 <= x <= 255', () => {
for (let i = 0; i < 1024; i++) {
expect(testables.garbage()).toBeGreaterThanOrEqual(0);
}
});
});
describe('allocMem', () => {
it('returns an array of the correct size', () => {
expect(allocMem(2048).length).toBe(2048);
});
it('has 0xff and 0x00 patterns', () => {
let memory = allocMem(2048);
expect(memory[0]).toBe(0xff);
expect(memory[1]).toBe(0xff);
expect(memory[2]).toBe(0x00);
expect(memory[3]).toBe(0x00);
expect(memory[4]).toBe(0xff);
});
it('has garbage in the right places', () => {
let memory = allocMem(0x800);
for (let i = 0; i < 0x800; i += 0x200) {
let passed = memory[i + 0x28] != 0xff
&& memory[i + 0x29] != 0xff
&& memory[i + 0x68] != 0xff
&& memory[i + 0x69] != 0xff;
if (passed) {
return;
}
}
fail('garbage not found');
});
});
describe('allocMemPages', () => {
it('allocates 256 * the size', () => {
expect(allocMemPages(5).length).toBe(5 * 256);
});
});
describe('toHex', () => {
it('converts an odd number of characters', () => {
expect(toHex(0xfedcb, 5)).toEqual("FEDCB");
});
it('correctly guesses byte values', () => {
expect(toHex(0xa5)).toEqual("A5");
});
it('correctly guesses word values', () => {
expect(toHex(0x1abc)).toEqual("1ABC");
});
it('only uses the bottom work of larger values', () => {
expect(toHex(0xabcdef)).toEqual("CDEF");
});
it('correctly prepends zeros', () => {
expect(toHex(0xa5, 4)).toEqual("00A5");
});
});
describe('toBinary', () => {
it('has 8 digits for zero', () => {
expect(toBinary(0x00)).toEqual("00000000");
});
it('correctly sets bits', () => {
expect(toBinary(0xa5)).toEqual("10100101");
});
});
describe('gup', () => {
// untestable due to direct reference to window.location
});
describe('hup', () => {
// untestable due to direct reference to window.location
});
describe('numToString', () => {
it('packs a zero byte into a string of all zeros', () => {
expect(numToString(0x00)).toEqual("\0\0\0\0");
});
it('packs a byte in the printable ASCII range into a zero-padded string',
() => {
expect(numToString(0x41)).toEqual("A\0\0\0");
});
it('packs a word into a string', () => {
expect(numToString(0x4142)).toEqual("BA\0\0");
});
it('packs a 32-bit value into a string', () => {
expect(numToString(0x41424344)).toEqual("DCBA");
});
it('ignores more than 32 bits', () => {
expect(numToString(0x4142434445)).toEqual("EDCB");
});
});