Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
enitrat committed Sep 17, 2024
1 parent e72474d commit bda49a6
Showing 1 changed file with 55 additions and 76 deletions.
131 changes: 55 additions & 76 deletions crates/evm/src/instructions/logging_operations.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,18 @@ mod tests {
let result = vm.exec_log0();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(vm.stack.len() == 0, 'stack should be empty');
assert!(result.is_ok());
assert_eq!(vm.stack.len(), 0);

let mut events = vm.env.state.events;
assert(events.len() == 1, 'context should have one event');
assert_eq!(events.len(), 1);

let event = events.pop_front().unwrap();
assert(event.keys.len() == 0, 'event should not have keys');
assert_eq!(event.keys.len(), 0);

assert(event.data.len() == 31, 'event should have 31 bytes');
assert_eq!(event.data.len(), 31);
let data_expected = u256_to_bytes_array(Bounded::<u256>::MAX).span().slice(0, 31);
assert(event.data.span() == data_expected, 'event data are incorrect');
assert_eq!(event.data.span(), data_expected);
}

#[test]
Expand All @@ -136,19 +136,19 @@ mod tests {
let result = vm.exec_log1();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(vm.stack.len() == 0, 'stack should be empty');
assert!(result.is_ok());
assert_eq!(vm.stack.len(), 0);

let mut events = vm.env.state.events;
assert(events.len() == 1, 'context should have one event');
assert_eq!(events.len(), 1);

let event = events.pop_front().unwrap();
assert(event.keys.len() == 1, 'event should have one key');
assert(*event.keys[0] == 0x0123456789ABCDEF, 'event key is not correct');
assert_eq!(event.keys.len(), 1);
assert_eq!(event.keys[0], 0x0123456789ABCDEF);

assert(event.data.len() == 32, 'event should have 32 bytes');
assert_eq!(event.data.len(), 32);
let data_expected = u256_to_bytes_array(Bounded::<u256>::MAX).span().slice(0, 32);
assert(event.data.span() == data_expected, 'event data are incorrect');
assert_eq!(event.data.span(), data_expected);
}

#[test]
Expand All @@ -167,20 +167,20 @@ mod tests {
let result = vm.exec_log2();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(vm.stack.len() == 0, 'stack should be empty');
assert!(result.is_ok());
assert_eq!(vm.stack.len(), 0);

let mut events = vm.env.state.events;
assert(events.len() == 1, 'context should have one event');
assert_eq!(events.len(), 1);

let event = events.pop_front().unwrap();
assert(event.keys.len() == 2, 'event should have two keys');
assert(*event.keys[0] == 0x0123456789ABCDEF, 'event key is not correct');
assert(*event.keys[1] == Bounded::<u256>::MAX, 'event key is not correct');
assert_eq!(event.keys.len(), 2);
assert_eq!(event.keys[0], 0x0123456789ABCDEF);
assert_eq!(event.keys[1], Bounded::<u256>::MAX);

assert(event.data.len() == 5, 'event should have 5 bytes');
assert_eq!(event.data.len(), 5);
let data_expected = u256_to_bytes_array(Bounded::<u256>::MAX).span().slice(0, 5);
assert(event.data.span() == data_expected, 'event data are incorrect');
assert_eq!(event.data.span(), data_expected);
}

#[test]
Expand All @@ -205,23 +205,20 @@ mod tests {
let result = vm.exec_log3();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(vm.stack.len() == 0, 'stack should be empty');
assert!(result.is_ok());
assert_eq!(vm.stack.len(), 0);

let mut events = vm.env.state.events;
assert(events.len() == 1, 'context should have one event');
assert_eq!(events.len(), 1);

let event = events.pop_front().unwrap();
assert(event.keys.len() == 3, 'event should have 3 keys');
assert(*event.keys[0] == 0x0123456789ABCDEF, 'event key is not correct');
assert(*event.keys[1] == Bounded::<u256>::MAX, 'event key is not correct');
assert(*event.keys[2] == 0x00, 'event key is not correct');
assert_eq!(event.keys.span(), [0x0123456789ABCDEF, Bounded::<u256>::MAX, 0x00].span());

assert(event.data.len() == 40, 'event should have 40 bytes');
assert_eq!(event.data.len(), 40);
let data_expected = u256_to_bytes_array(Bounded::<u256>::MAX).span();
assert(event.data.span().slice(0, 32) == data_expected, 'event data are incorrect');
assert_eq!(event.data.span().slice(0, 32), data_expected);
let data_expected = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF].span();
assert(event.data.span().slice(32, 8) == data_expected, 'event data are incorrect');
assert_eq!(event.data.span().slice(32, 8), data_expected);
}

#[test]
Expand All @@ -247,22 +244,18 @@ mod tests {
let result = vm.exec_log4();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(vm.stack.len() == 0, 'stack should be empty');
assert!(result.is_ok());
assert_eq!(vm.stack.len(), 0);

let mut events = vm.env.state.events;
assert(events.len() == 1, 'context should have one event');
assert_eq!(events.len(), 1);

let event = events.pop_front().unwrap();
assert(event.keys.len() == 4, 'event should have 4 keys');
assert(*event.keys[0] == 0x0123456789ABCDEF, 'event key is not correct');
assert(*event.keys[1] == Bounded::<u256>::MAX, 'event key is not correct');
assert(*event.keys[2] == 0x00, 'event key is not correct');
assert(*event.keys[3] == Bounded::<u256>::MAX, 'event key is not correct');
assert_eq!(event.keys.span(), [0x0123456789ABCDEF, Bounded::<u256>::MAX, 0x00, Bounded::<u256>::MAX].span());

assert(event.data.len() == 10, 'event should have 10 bytes');
assert_eq!(event.data.len(), 10);
let data_expected = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x00, 0x00].span();
assert(event.data.span() == data_expected, 'event data are incorrect');
assert_eq!(event.data.span(), data_expected);
}

#[test]
Expand All @@ -280,9 +273,9 @@ mod tests {
let result = vm.exec_log1();

// Then
assert(result.is_err(), 'should have returned an error');
assert(
result.unwrap_err() == EVMError::WriteInStaticContext, 'err != WriteInStaticContext'
assert!(result.is_err());
assert_eq!(
result.unwrap_err(), EVMError::WriteInStaticContext
);
}

Expand All @@ -301,17 +294,16 @@ mod tests {
let result = vm.exec_log1();

// Then
assert(result.is_ok(), 'should have succeeded');
assert(vm.stack.len() == 0, 'stack should be empty');
assert!(result.is_ok());
assert_eq!(vm.stack.len(), 0);

let mut events = vm.env.state.events;
assert(events.len() == 1, 'context should have one event');
assert_eq!(events.len(), 1);

let event = events.pop_front().unwrap();
assert(event.keys.len() == 1, 'event should have one key');
assert(*event.keys[0] == 0x0123456789ABCDEF, 'event key is not correct');
assert_eq!(event.keys.span(), [0x0123456789ABCDEF].span());

assert(event.data.len() == 0, 'event data should be empty');
assert_eq!(event.data.len(), 0);
}

#[test]
Expand All @@ -329,11 +321,8 @@ mod tests {
let result = vm.exec_log1();

// Then
assert(result.is_err(), 'should return an error');
assert(
result.unwrap_err() == EVMError::TypeConversionError(TYPE_CONVERSION_ERROR),
'err != TypeConversionError'
);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), EVMError::TypeConversionError(TYPE_CONVERSION_ERROR));
}

#[test]
Expand All @@ -351,11 +340,8 @@ mod tests {
let result = vm.exec_log1();

// Then
assert(result.is_err(), 'should return an error');
assert(
result.unwrap_err() == EVMError::TypeConversionError(TYPE_CONVERSION_ERROR),
'err != TypeConversionError'
);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), EVMError::MemoryLimitOOG);
}

#[test]
Expand Down Expand Up @@ -387,32 +373,25 @@ mod tests {
vm.exec_log4().expect('exec_log4 failed');

// Then
assert(vm.stack.len() == 0, 'stack size should be 0');
assert!(vm.stack.is_empty());

let mut events = vm.env.state.events;
assert(events.len() == 2, 'context should have 2 events');
assert_eq!(events.len(), 2);

let event1 = events.pop_front().unwrap();
assert(event1.keys.len() == 3, 'event1 should have 3 keys');
assert(*event1.keys[0] == 0x0123456789ABCDEF, 'event1 key is not correct');
assert(*event1.keys[1] == Bounded::<u256>::MAX, 'event1 key is not correct');
assert(*event1.keys[2] == 0x00, 'event1 key is not correct');
assert_eq!(event1.keys.span(), [0x0123456789ABCDEF, Bounded::<u256>::MAX, 0x00].span());

assert(event1.data.len() == 40, 'event1 should have 40 bytes');
assert_eq!(event1.data.len(), 40);
let data_expected = u256_to_bytes_array(Bounded::<u256>::MAX).span();
assert(event1.data.span().slice(0, 32) == data_expected, 'event1 data are incorrect');
assert_eq!(event1.data.span().slice(0, 32), data_expected);
let data_expected = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF].span();
assert(event1.data.span().slice(32, 8) == data_expected, 'event1 data are incorrect');
assert_eq!(event1.data.span().slice(32, 8), data_expected);

let event2 = events.pop_front().unwrap();
assert(event2.keys.len() == 4, 'event2 should have 4 keys');
assert(*event2.keys[0] == 0x0123456789ABCDEF, 'event2 key is not correct');
assert(*event2.keys[1] == Bounded::<u256>::MAX, 'event2 key is not correct');
assert(*event2.keys[2] == 0x00, 'event2 key is not correct');
assert(*event2.keys[3] == Bounded::<u256>::MAX, 'event2 key is not correct');
assert_eq!(event2.keys.span(), [0x0123456789ABCDEF, Bounded::<u256>::MAX, 0x00, Bounded::<u256>::MAX].span());

assert(event2.data.len() == 10, 'event2 should have 10 bytes');
assert_eq!(event2.data.len(), 10);
let data_expected = [0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x00, 0x00].span();
assert(event2.data.span() == data_expected, 'event2 data are incorrect');
assert_eq!(event2.data.span(), data_expected);
}
}

0 comments on commit bda49a6

Please sign in to comment.