From 486f554d5b3e686439e6db76dadc4756e1c1d0ea Mon Sep 17 00:00:00 2001 From: Ian Flanigan Date: Sun, 13 Aug 2023 00:50:48 +0200 Subject: [PATCH] Improve Disk II test reliability (#195) Before, the garbage test had only about a 92% chance of success because it relied on random numbers not being equal. This change just repeats the test 5 times and allows up to 1 failure. This increases the chance of the test passing to a hair less that 99.8%. --- test/js/cards/disk2.spec.ts | 45 +++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/test/js/cards/disk2.spec.ts b/test/js/cards/disk2.spec.ts index 2ab9ee0..6c056ea 100644 --- a/test/js/cards/disk2.spec.ts +++ b/test/js/cards/disk2.spec.ts @@ -677,28 +677,35 @@ describe('DiskII', () => { diskII.ioSwitch(0x82); // coil 1 off diskII.ioSwitch(0x8e); // read mode - // Read 5 nibbles - const nibbles: byte[] = []; - let read = false; - while (nibbles.length < 5) { - cycles++; - const nibble = diskII.ioSwitch(0x8c); // read data - const qa = nibble & 0x80; - if (qa && !read) { - nibbles.push(nibble); - read = true; + // Try this test 5 times because we could get unlucky. + let failures = 0; + for (let i = 0; i < 5; i++) { + // Read 5 nibbles + const nibbles: byte[] = []; + let read = false; + while (nibbles.length < 5) { + cycles++; + const nibble = diskII.ioSwitch(0x8c); // read data + const qa = nibble & 0x80; + if (qa && !read) { + nibbles.push(nibble); + read = true; + } + if (!qa && read) { + read = false; + } } - if (!qa && read) { - read = false; + // Test that the first doesn't equal any of the others. + // (Yes, this test could fail with some bad luck.) + let equal = false; + for (let i = 1; i < 5; i++) { + equal ||= nibbles[0] === nibbles[i]; + } + if (equal) { + failures++; } } - // Test that the first doesn't equal any of the others. - // (Yes, this test could fail with some bad luck.) - let equal = false; - for (let i = 1; i < 5; i++) { - equal ||= nibbles[0] === nibbles[i]; - } - expect(equal).not.toBeTruthy(); + expect(failures).toBeLessThan(2); }); it('disk spins at a consistent speed', () => {