11#[ cfg( test) ]
22mod test {
3+ use nexus_common:: constants:: WORD_SIZE ;
34 use nexus_common:: cpu:: InstructionResult ;
45
56 use nexus_vm:: elf:: ElfFile ;
@@ -72,6 +73,23 @@ mod test {
7273 }
7374 }
7475
76+ /// Parse the output bytes as exit code and output.
77+ fn parse_output < T : DeserializeOwned > (
78+ output : Vec < u8 > ,
79+ ) -> Result < ( u32 , Option < T > ) , postcard:: Error > {
80+ // The first 4 bytes store the exit code.
81+ let exit_code =
82+ u32:: from_le_bytes ( output[ 0 ..4 ] . try_into ( ) . expect ( "Failed to parse exit code" ) ) ;
83+
84+ if output. len ( ) == WORD_SIZE {
85+ Ok ( ( exit_code, None ) )
86+ } else {
87+ // Deserialize the rest as the output.
88+ let output: T = from_bytes ( & output[ WORD_SIZE ..] ) . expect ( "Deserialization failed" ) ;
89+ Ok ( ( exit_code, Some ( output) ) )
90+ }
91+ }
92+
7593 /// Create a temporary directory with a new Cargo project that has nexus_rt as a local dependency.
7694 fn create_tmp_dir ( ) -> TempDir {
7795 // Create a temporary directory.
@@ -197,6 +215,7 @@ mod test {
197215 }
198216
199217 let mut deserialized_output: Option < V > = None ;
218+ let mut exit_code: u32 ;
200219 let ad = vec ! [ 0u8 ; 0xbeef as usize ] ; // placeholder ad until we have use for it
201220
202221 for elf in elfs {
@@ -210,11 +229,12 @@ mod test {
210229 assert_eq ! ( & emulator. execute( ) , & io_args. expected_result) ;
211230
212231 // Deserialize the output.
213- if io_args. expected_output . is_some ( ) {
214- let output_bytes = emulator. get_output ( ) . unwrap ( ) ;
215- deserialized_output =
216- Some ( from_bytes ( & output_bytes) . expect ( "Deserialization failed" ) ) ;
217- }
232+ ( exit_code, deserialized_output) =
233+ parse_output ( emulator. get_output ( ) . unwrap ( ) ) . unwrap ( ) ;
234+ assert_eq ! (
235+ Err ( nexus_vm:: error:: VMError :: VMExited ( exit_code) ) ,
236+ io_args. expected_result
237+ ) ;
218238
219239 // Run a second pass with a linear emulator constructed from the harvard emulator.
220240 if matches ! ( emulator_type, EmulatorType :: TwoPass ) {
@@ -230,11 +250,12 @@ mod test {
230250 assert_eq ! ( & linear_emulator. execute( ) , & io_args. expected_result) ;
231251
232252 // Deserialize the output.
233- if io_args. expected_output . is_some ( ) {
234- let output_bytes = linear_emulator. get_output ( ) . unwrap ( ) ;
235- deserialized_output =
236- Some ( from_bytes ( & output_bytes) . expect ( "Deserialization failed" ) ) ;
237- }
253+ ( exit_code, deserialized_output) =
254+ parse_output ( linear_emulator. get_output ( ) . unwrap ( ) ) . unwrap ( ) ;
255+ assert_eq ! (
256+ Err ( nexus_vm:: error:: VMError :: VMExited ( exit_code) ) ,
257+ io_args. expected_result
258+ ) ;
238259 }
239260 }
240261 EmulatorType :: Linear ( heap_size, stack_size, program_size) => {
@@ -269,11 +290,12 @@ mod test {
269290 assert_eq ! ( & emulator. execute( ) , & io_args. expected_result) ;
270291
271292 // Deserialize the output.
272- if io_args. expected_output . is_some ( ) {
273- let output_bytes = emulator. get_output ( ) . unwrap ( ) ;
274- deserialized_output =
275- Some ( from_bytes ( & output_bytes) . expect ( "Deserialization failed" ) ) ;
276- }
293+ ( exit_code, deserialized_output) =
294+ parse_output ( emulator. get_output ( ) . unwrap ( ) ) . unwrap ( ) ;
295+ assert_eq ! (
296+ Err ( nexus_vm:: error:: VMError :: VMExited ( exit_code) ) ,
297+ io_args. expected_result
298+ ) ;
277299 }
278300 } ;
279301 }
0 commit comments