Skip to content

Commit

Permalink
Added some more unit tests, to check the check the world functions an…
Browse files Browse the repository at this point in the history
…d also made the +0x80 only happen on 8 bit per sample outputs since that will just switch the sign of negative vaules which is not needed normally
  • Loading branch information
kmp1 committed Feb 21, 2015
1 parent 0266832 commit 0ec4926
Show file tree
Hide file tree
Showing 2 changed files with 326 additions and 5 deletions.
19 changes: 18 additions & 1 deletion src/tzx.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ var tzx = (function () {
if (typeof output.addSample !== "function") {
throw "output contains addSample but it is not a function.";
}

if (!output.hasOwnProperty("getSampleSize")) {
throw "output does not contain the function '" +
"getSampleSize()' - this should be a function that " +
"returns the number of bits in a sample in the output.";
}

if (typeof output.getSampleSize !== "function") {
throw "output contains getSampleSize but it is not a " +
"function.";
}
}

function validateInputAndGetWrapperIfPossible() {
Expand Down Expand Up @@ -267,7 +278,13 @@ var tzx = (function () {
}

function addSampleToOutput(data) {
var sample = data + 0x80;
var sample;

if (output.getSampleSize() === 8) {
sample = data + 0x80;
} else {
sample = data;
}

output.addSample(sample);
}
Expand Down
312 changes: 308 additions & 4 deletions test/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,310 @@ var wav = require("../node_modules/wav.js/src/wav.js");
var fs = require('fs');
var constants = require('constants');


exports.validateSettingsChecks = function (test) {
var threw = false, output =
{ getFrequency: function () { return 44100; }, addSample: function () {}, getSampleSize: function() { return 8; } },
input = {
getLength: function() { return 10; },
getByte: function(index) { }
};

try {
tzx.convertTzxToAudio(null, input, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing null argument failed");

threw = false;
try {
tzx.convertTzxToAudio(undefined, input, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing no argument failed");

threw = false;
try {
tzx.convertTzxToAudio({
highAmplitude: 5
}, input, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing subset argument failed");

threw = false;
try {
tzx.convertTzxToAudio({
highAmplitude: 5,
clockSpeed: 3500000
}, input, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing subset argument failed");

threw = false;
try {
tzx.convertTzxToAudio({
highAmplitude: 5,
clockSpeed: 3500000,
pilotPulse: 1
}, input, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing subset argument failed");

threw = false;
try {
tzx.convertTzxToAudio({
highAmplitude: 5,
clockSpeed: 3500000,
pilotPulse: 1,
sync1Pulse: 12
}, input, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing subset argument failed");

threw = false;
try {
tzx.convertTzxToAudio({
highAmplitude: 5,
clockSpeed: 3500000,
pilotPulse: 1,
sync1Pulse: 12,
sync2Pulse: 7
}, input, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing subset argument failed");

threw = false;
try {
tzx.convertTzxToAudio({
highAmplitude: 5,
clockSpeed: 3500000,
pilotPulse: 1,
sync1Pulse: 12,
sync2Pulse: 7,
bit0Pulse: 1
}, input, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing subset argument failed");

threw = false;
try {
tzx.convertTzxToAudio({
highAmplitude: 5,
clockSpeed: 3500000,
pilotPulse: 1,
sync1Pulse: 12,
sync2Pulse: 7,
bit0Pulse: 1,
bit1Pulse: 3
}, input, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing subset argument failed");

threw = false;
try {
tzx.convertTzxToAudio({
highAmplitude: 5,
clockSpeed: 3500000,
pilotPulse: 1,
sync1Pulse: 12,
sync2Pulse: 7,
bit0Pulse: 1,
bit1Pulse: 3,
headerPilotLength: 10
}, input, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing subset argument failed");

threw = false;
try {
tzx.convertTzxToAudio({
highAmplitude: 5,
clockSpeed: 3500000,
pilotPulse: 1,
sync1Pulse: 12,
sync2Pulse: 7,
bit0Pulse: 1,
bit1Pulse: 3,
headerPilotLength: 10,
dataPilotLength: 5
}, input, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing subset argument failed");
test.done();
};

exports.validateOutputChecks = function (test) {
var threw = false, settings = tzx.MachineSettings.ZXSpectrum48, input = {
getLength: function() { return 10; },
getByte: function(index) { }
};

try {
tzx.convertTzxToAudio(settings, input, null);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing null argument failed");

threw = false;
try {
tzx.convertTzxToAudio(settings, input);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing no argument failed");

threw = false;
try {
tzx.convertTzxToAudio(settings, input, {});
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing empty failed");

threw = false;
try {
tzx.convertTzxToAudio(settings, input, { getFrequency: 100 });
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing property failed");

threw = false;
try {
tzx.convertTapToAudio(settings, input, { getFrequency: function () { return 100; } });
} catch (e) {
threw = true;
}

test.equal(threw, true, "Missing function failed");

threw = false;
try {
tzx.convertTzxToAudio(settings, input, { getFrequency: function () { return 100; }, addSample: 100 });
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing function and property failed");

threw = false;
try {
tzx.convertTzxToAudio(settings, input, { getFrequency: function () { return 100; }, addSample: function () {} });
} catch (e) {
threw = true;
}

test.equal(threw, true, "Missing getSampleSize function");

threw = false;
try {
tzx.convertTzxToAudio(settings, input, { getFrequency: function () { return 100; }, addSample: function () {}, getSampleSize: 8 });
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing function and property failed");

test.done();
};

exports.validateInputChecks = function (test) {
var threw = false, output =
{ getFrequency: function () { return 44100; }, addSample: function () {}, getSampleSize: function() { return 8; } },
settings = tzx.MachineSettings.ZXSpectrum48;

try {
tzx.convertTzxToAudio(settings, null, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing null argument failed");

threw = false;
try {
tzx.convertTzxToAudio(settings, undefined, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing no argument failed");

threw = false;
try {
tzx.convertTzxToAudio(settings, {}, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing empty failed");

threw = false;
try {
tzx.convertTzxToAudio(settings, { getLength: 100 }, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing property failed");

threw = false;
try {
tzx.convertTzxToAudio(settings, { getLength: function () { return 100; } }, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Missing function failed");

threw = false;
try {
tzx.convertTzxToAudio(settings, { getLength: function () { return 100; }, getByte: 100 }, output);
} catch (e) {
threw = true;
}

test.equal(threw, true, "Passing function and property failed");

test.done();
};

function compareByteArrays(array1, array2) {
if (array2.length != array1.length) {
return false;
Expand All @@ -17,7 +321,7 @@ function compareByteArrays(array1, array2) {
return true;
}

function createAudioOutput(input) {
function createInputObject(input) {

var tzxFile = fs.readFileSync(input);

Expand All @@ -34,7 +338,7 @@ exports.testTzxWithOnlyBlock10Data = function(test) {
var wave = wav.create(1, 44100, wav.SampleSize.EIGHT);

var details = tzx.convertTzxToAudio(tzx.MachineSettings.ZXSpectrum48,
createAudioOutput("test/input/simple.tzx"), wave);
createInputObject("test/input/simple.tzx"), wave);

var rawWaveData = wave.toByteArray();

Expand All @@ -52,7 +356,7 @@ exports.testBasicTap = function(test) {
var wave = wav.create(1, 44100, wav.SampleSize.EIGHT);

var details = tzx.convertTapToAudio(tzx.MachineSettings.ZXSpectrum48,
createAudioOutput("test/input/simple.tap"), wave);
createInputObject("test/input/simple.tap"), wave);

var rawWaveData = wave.toByteArray();

Expand All @@ -70,7 +374,7 @@ exports.testTzxWithFastDataBlock = function(test) {
var wave = wav.create(1, 44100, wav.SampleSize.EIGHT);

var details = tzx.convertTzxToAudio(tzx.MachineSettings.ZXSpectrum48,
createAudioOutput("test/input/fast_index.tzx"), wave);
createInputObject("test/input/fast_index.tzx"), wave);

var rawWaveData = wave.toByteArray();

Expand Down

0 comments on commit 0ec4926

Please sign in to comment.