2
2
const commonTest = require ( '../common-test' ) ;
3
3
4
4
// Wasm Boy library
5
- const WasmBoy = require ( '../../dist/wasmboy.wasm.cjs.js ' ) . WasmBoy ;
5
+ const WasmBoy = require ( '../../dist/wasmboy.wasm.cjs' ) . WasmBoy ;
6
6
7
7
// File management
8
8
const fs = require ( 'fs' ) ;
9
9
10
10
// Assertion
11
11
const assert = require ( 'assert' ) ;
12
12
13
+ // Golden file handling
14
+ const { goldenFileCompareOrCreate, goldenImageDataArrayCompare } = require ( './goldenCompare' ) ;
15
+
13
16
// Some Timeouts for specified test roms
14
17
const TEST_ROM_DEFAULT_TIMEOUT = 7500 ;
15
18
const TEST_ROM_TIMEOUT = { } ;
@@ -25,6 +28,55 @@ WasmBoy.config({
25
28
isGbcEnabled : true
26
29
} ) ;
27
30
31
+ // Audio Golden Test
32
+ // TODO: Remove this with an actual accuracy
33
+ // sound test
34
+ describe ( 'audio golden test' , ( ) => {
35
+ // Define our wasmboy instance
36
+ // Not using arrow functions, as arrow function timeouts were acting up
37
+ beforeEach ( function ( done ) {
38
+ // Set a timeout of 7500, takes a while for wasm module to parse
39
+ this . timeout ( 7500 ) ;
40
+
41
+ // Read the test rom a a Uint8Array and pass to wasmBoy
42
+ const testRomArray = new Uint8Array ( fs . readFileSync ( './test/performance/testroms/back-to-color/back-to-color.gbc' ) ) ;
43
+
44
+ WasmBoy . loadROM ( testRomArray ) . then ( done ) ;
45
+ } ) ;
46
+
47
+ it ( 'should have the same audio buffer' , function ( done ) {
48
+ // Set our timeout
49
+ this . timeout ( TEST_ROM_DEFAULT_TIMEOUT + 2000 ) ;
50
+
51
+ const asyncTask = async ( ) => {
52
+ // Run some frames
53
+ await WasmBoy . _runWasmExport ( 'executeMultipleFrames' , [ 60 ] ) ;
54
+ await WasmBoy . _runWasmExport ( 'executeMultipleFrames' , [ 60 ] ) ;
55
+ await WasmBoy . _runWasmExport ( 'clearAudioBuffer' ) ;
56
+ await WasmBoy . _runWasmExport ( 'executeMultipleFrames' , [ 60 ] ) ;
57
+ await WasmBoy . _runWasmExport ( 'executeMultipleFrames' , [ 60 ] ) ;
58
+
59
+ // Execute a few frames
60
+ const memoryStart = await WasmBoy . _getWasmConstant ( 'AUDIO_BUFFER_LOCATION' ) ;
61
+ const memorySize = await WasmBoy . _getWasmConstant ( 'AUDIO_BUFFER_SIZE' ) ;
62
+ // - 20 to not include the overrun in the audio buffer
63
+ const memory = await WasmBoy . _getWasmMemorySection ( memoryStart , memoryStart + memorySize - 20 ) ;
64
+
65
+ // Get the memory as a normal array
66
+ const audioArray = [ ] ;
67
+ for ( let i = 0 ; i < memory . length ; i ++ ) {
68
+ audioArray . push ( memory [ i ] ) ;
69
+ }
70
+
71
+ goldenFileCompareOrCreate ( './test/accuracy/sound-test.golden.output.json' , audioArray ) ;
72
+ done ( ) ;
73
+ } ;
74
+ asyncTask ( ) ;
75
+ } ) ;
76
+ } ) ;
77
+
78
+ // Graphical Golden Test(s)
79
+ // Simply screenshot the end result of the accuracy test
28
80
const testRomsPath = './test/accuracy/testroms' ;
29
81
30
82
commonTest . getDirectories ( testRomsPath ) . forEach ( directory => {
@@ -74,61 +126,15 @@ commonTest.getDirectories(testRomsPath).forEach(directory => {
74
126
const wasmboyOutputImageTest = async ( ) => {
75
127
await WasmBoy . pause ( ) ;
76
128
77
- console . log ( `Checking results for the following test rom: ${ directory } /${ testRom } ` ) ;
129
+ const testDataPath = testRom . replace ( '.gb' , '.golden.output' ) ;
130
+ const goldenFile = `${ directory } /${ testDataPath } ` ;
131
+
132
+ console . log ( `Checking results for the following test rom: ${ goldenFile } ` ) ;
78
133
79
134
const imageDataArray = await commonTest . getImageDataFromFrame ( ) ;
80
135
81
- // Output a gitignored image of the current tests
82
- const testImagePath = testRom . replace ( '.gb' , '.current.png' ) ;
83
- await commonTest . createImageFromFrame ( imageDataArray , `${ directory } /${ testImagePath } ` ) ;
84
-
85
- // Now compare with the current array if we have it
86
- const testDataPath = testRom . replace ( '.gb' , '.golden.output' ) ;
87
- if ( fs . existsSync ( `${ directory } /${ testDataPath } ` ) ) {
88
- // Compare the file
89
- const goldenOuput = fs . readFileSync ( `${ directory } /${ testDataPath } ` ) ;
90
-
91
- const goldenImageDataArray = JSON . parse ( goldenOuput ) ;
92
-
93
- if ( goldenImageDataArray . length !== imageDataArray . length ) {
94
- assert . equal ( goldenImageDataArray . length === imageDataArray . length , true ) ;
95
- } else {
96
- // Find the differences between the two arrays
97
- const arrayDiff = [ ] ;
98
-
99
- for ( let i = 0 ; i < goldenImageDataArray . length ; i ++ ) {
100
- if ( goldenImageDataArray [ i ] !== imageDataArray [ i ] ) {
101
- arrayDiff . push ( {
102
- index : i ,
103
- goldenElement : goldenImageDataArray [ i ] ,
104
- imageDataElement : imageDataArray [ i ]
105
- } ) ;
106
- }
107
- }
108
-
109
- // Check if we found differences
110
- if ( arrayDiff . length > 0 ) {
111
- console . log ( 'Differences found in expected (golden) output:' ) ;
112
- console . log ( arrayDiff ) ;
113
- }
114
- assert . equal ( arrayDiff . length , 0 ) ;
115
- }
116
-
117
- done ( ) ;
118
- } else {
119
- // Either we didn't have it because this is the first time running this test rom,
120
- // or we wanted to update expected output, so we deleted the file
121
- console . warn ( `No output found in: ${ directory } /${ testDataPath } , Creating expected (golden) output...` ) ;
122
-
123
- // Create the output file
124
- // Stringify our image data
125
- const imageDataStringified = JSON . stringify ( imageDataArray ) ;
126
- fs . writeFileSync ( `${ directory } /${ testDataPath } ` , imageDataStringified ) ;
127
-
128
- const testImagePath = testRom . replace ( '.gb' , '.golden.png' ) ;
129
- await commonTest . createImageFromFrame ( imageDataArray , `${ directory } /${ testImagePath } ` ) ;
130
- done ( ) ;
131
- }
136
+ await goldenImageDataArrayCompare ( goldenFile , imageDataArray , directory , testRom ) ;
137
+ done ( ) ;
132
138
} ;
133
139
wasmboyOutputImageTest ( ) ;
134
140
} , timeToWaitForTestRom ) ;
0 commit comments