Skip to content

Commit

Permalink
Increase coverage (#788)
Browse files Browse the repository at this point in the history
* debug codecov

* debug codecov

* add tests

* remove derive tests

* remove derive tests
  • Loading branch information
Jrigada authored Jan 27, 2023
1 parent 3d2666c commit 309c355
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,5 @@ jobs:
- name: Upload coverage to codecov.io
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
39 changes: 39 additions & 0 deletions src/hint_processor/hint_processor_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,32 @@ mod tests {
);
}

#[test]
fn get_offset_value_reference_valid() {
let mut vm = vm!();
vm.memory = memory![((1, 0), 0)];
let mut hint_ref = HintReference::new(0, 0, false, true);
hint_ref.offset1 = OffsetValue::Reference(Register::FP, 2_i32, false);

assert_eq!(
get_offset_value_reference(&vm, &hint_ref, &ApTracking::new(), &hint_ref.offset1),
Ok(mayberelocatable!(1, 2))
);
}

#[test]
fn get_offset_value_reference_invalid() {
let mut vm = vm!();
vm.memory = memory![((1, 0), 0)];
let mut hint_ref = HintReference::new(0, 0, false, true);
hint_ref.offset1 = OffsetValue::Reference(Register::FP, -2_i32, false);

assert_eq!(
get_offset_value_reference(&vm, &hint_ref, &ApTracking::new(), &hint_ref.offset1),
Err(HintError::FailedToGetIds)
);
}

#[test]
fn get_ptr_from_reference_short_path() {
let mut vm = vm!();
Expand Down Expand Up @@ -287,6 +313,19 @@ mod tests {
);
}

#[test]
fn tracking_correction_valid() {
let mut ref_ap_tracking = ApTracking::new();
ref_ap_tracking.group = 1;
let mut hint_ap_tracking = ApTracking::new();
hint_ap_tracking.group = 1;

assert_eq!(
apply_ap_tracking_correction(&relocatable!(1, 0), &ref_ap_tracking, &hint_ap_tracking),
Ok(relocatable!(1, 0))
);
}

#[test]
fn tracking_correction_invalid_group() {
let mut ref_ap_tracking = ApTracking::new();
Expand Down
8 changes: 8 additions & 0 deletions src/types/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,12 @@ mod tests {
let encoded_instruction = Felt::new(4612671187288031229_i64);
assert!(!is_call_instruction(&encoded_instruction, None));
}

#[test]
fn instruction_size() {
let encoded_instruction = Felt::new(1226245742482522112_i64);
let instruction =
decode_instruction(encoded_instruction.to_i64().unwrap(), Some(&Felt::new(2))).unwrap();
assert_eq!(instruction.size(), 2);
}
}
56 changes: 56 additions & 0 deletions src/vm/runners/builtin_runner/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,60 @@ mod tests {
vm.segments.segment_used_sizes = Some(vec![4]);
assert_eq!(builtin.get_used_cells(&vm.segments), Ok(4));
}

#[test]
fn test_get_used_instances_missing_segments() {
let builtin = BuiltinRunner::Output(OutputBuiltinRunner::new(true));
let memory_segment_manager = MemorySegmentManager::new();

assert_eq!(
builtin.get_used_instances(&memory_segment_manager),
Err(MemoryError::MissingSegmentUsedSizes)
);
}

#[test]
fn test_get_used_instances_valid() {
let builtin = BuiltinRunner::Output(OutputBuiltinRunner::new(true));
let mut memory_segment_manager = MemorySegmentManager::new();
memory_segment_manager.segment_used_sizes = Some(vec![0]);

assert_eq!(builtin.get_used_instances(&memory_segment_manager), Ok(0));
}

#[test]
fn test_deduce_memory_cell_output_builtin() {
let builtin = BuiltinRunner::Output(OutputBuiltinRunner::new(true));
let mut vm = vm!();

vm.memory = memory![
((0, 0), (0, 0)),
((0, 1), (0, 1)),
((2, 0), (0, 0)),
((2, 1), 2)
];

vm.segments.segment_used_sizes = Some(vec![0]);

let pointer = Relocatable::from((2, 2));

assert_eq!(builtin.deduce_memory_cell(&pointer, &vm.memory), Ok(None));
}

#[test]
fn test_add_validation_rule() {
let builtin = OutputBuiltinRunner::new(true);
let mut vm = vm!();

vm.memory = memory![
((0, 0), (0, 0)),
((0, 1), (0, 1)),
((2, 0), (0, 0)),
((2, 1), 2)
];

vm.segments.segment_used_sizes = Some(vec![0]);

assert_eq!(builtin.add_validation_rule(&mut vm.memory), Ok(()));
}
}
12 changes: 12 additions & 0 deletions src/vm/runners/builtin_runner/range_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,18 @@ mod tests {
);
}

#[test]
fn test_base() {
let builtin = RangeCheckBuiltinRunner::new(8, 8, true);
assert_eq!(builtin.base(), 0);
}

#[test]
fn test_ratio() {
let builtin = RangeCheckBuiltinRunner::new(8, 8, true);
assert_eq!(builtin.ratio(), 8);
}

#[test]
fn get_used_cells_missing_segment_used_sizes() {
let builtin = BuiltinRunner::RangeCheck(RangeCheckBuiltinRunner::new(256, 8, true));
Expand Down
48 changes: 48 additions & 0 deletions src/vm/runners/builtin_runner/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,27 @@ mod tests {
},
};

#[test]
fn get_used_cells_and_allocated_size_error() {
let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true);
let mut vm = vm!();
vm.current_step = 100;
vm.segments.segment_used_sizes = Some(vec![1]);
assert_eq!(
builtin.get_used_cells_and_allocated_size(&vm),
Err(MemoryError::InsufficientAllocatedCells)
);
}

#[test]
fn get_used_cells_and_allocated_size_valid() {
let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::new(10), true);
let mut vm = vm!();
vm.current_step = 110;
vm.segments.segment_used_sizes = Some(vec![1]);
assert_eq!(builtin.get_used_cells_and_allocated_size(&vm), Ok((1, 22)));
}

#[test]
fn initialize_segments_for_ecdsa() {
let mut builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true);
Expand Down Expand Up @@ -487,4 +508,31 @@ mod tests {
let result = builtin.deduce_memory_cell(&Relocatable::from((0, 5)), &memory);
assert_eq!(result, Ok(None));
}

#[test]
fn test_ratio() {
let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true);
assert_eq!(builtin.ratio(), 512);
}

#[test]
fn test_base() {
let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true);
assert_eq!(builtin.base(), 0);
}

#[test]
fn test_get_memory_segment_addresses() {
let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true);

assert_eq!(builtin.get_memory_segment_addresses(), (0, None));
}

#[test]
fn deduce_memory_cell() {
let memory = Memory::new();
let builtin = SignatureBuiltinRunner::new(&EcdsaInstanceDef::default(), true);
let result = builtin.deduce_memory_cell(&Relocatable::from((0, 5)), &memory);
assert_eq!(result, Ok(None));
}
}

0 comments on commit 309c355

Please sign in to comment.