Skip to content

Commit

Permalink
Fix Segments integration tests (keep-starknet-strange#488)
Browse files Browse the repository at this point in the history
fix segments integration test

Co-authored-by: Thomas Coratger <[email protected]>
  • Loading branch information
StringNick and tcoratger authored Mar 22, 2024
1 parent 7e6afc5 commit c299ea7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 37 deletions.
38 changes: 10 additions & 28 deletions src/hint_processor/segments.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const Relocatable = @import("../vm/memory/relocatable.zig").Relocatable;
const Allocator = std.mem.Allocator;
const hint_codes = @import("builtin_hint_codes.zig");
const hint_utils = @import("hint_utils.zig");
const testing_utils = @import("testing_utils.zig");
const HintProcessor = @import("hint_processor_def.zig").CairoVMHintProcessor;
const HintData = @import("hint_processor_def.zig").HintData;

Expand Down Expand Up @@ -94,31 +95,24 @@ test "Segments: run relocate segments" {
);
defer vm.segments.memory.deinitData(std.testing.allocator);

// Creates a hashmap containing variable references.
var ids_data = std.StringHashMap(HintReference).init(std.testing.allocator);
defer ids_data.deinit();

// Inserts a valid variable reference into the hashmap.
try ids_data.put("src_ptr", HintReference.initSimple(-2));
try ids_data.put("dest_ptr", HintReference.initSimple(-1));

// Initialize a HintProcessor instance.
const hint_processor: HintProcessor = .{};
// Creates a hashmap containing variable references and insert data.

// Creates a HintData instance representing the hint to relocate segments.
var hint_data = HintData.init(hint_codes.RELOCATE_SEGMENT, ids_data, .{});
var ids_data = try testing_utils.setupIdsForTestWithoutMemory(std.testing.allocator, &.{ "src_ptr", "dest_ptr" });
defer ids_data.deinit();

// Executes the hint using the HintProcessor.
try hint_processor.executeHint(std.testing.allocator, &vm, &hint_data, undefined, undefined);
try testing_utils.runHint(std.testing.allocator, &vm, ids_data, hint_codes.RELOCATE_SEGMENT, undefined, undefined);

// Verifies that the memory relocation operation completes successfully.
// Initialize a HintProcessor instance. // Verifies that the memory relocation operation completes successfully.
try vm.segments.memory.relocateMemory();
}

test "Segments: run temporary array" {
// Initializes the Cairo virtual machine.
var vm = try CairoVM.init(std.testing.allocator, .{});
defer vm.deinit(); // Ensure cleanup.
// Ensure cleanup of memory data after execution.
defer vm.segments.memory.deinitData(std.testing.allocator);

// Sets the frame pointer within the virtual machine to a specific relocatable value.
vm.run_context.fp.* = 1;
Expand All @@ -127,23 +121,11 @@ test "Segments: run temporary array" {
inline for (0..2) |_| _ = try vm.addMemorySegment();

// Creates a hashmap containing variable references.
var ids_data = std.StringHashMap(HintReference).init(std.testing.allocator);
var ids_data = try testing_utils.setupIdsForTestWithoutMemory(std.testing.allocator, &.{"temporary_array"});
defer ids_data.deinit();

// Inserts a valid variable reference into the hashmap.
try ids_data.put("temporary_array", HintReference.initSimple(-1));

// Initialize a HintProcessor instance.
const hint_processor: HintProcessor = .{};

// Creates a HintData instance representing the hint to create a temporary array.
var hint_data = HintData.init(hint_codes.TEMPORARY_ARRAY, ids_data, .{});

// Executes the hint using the HintProcessor.
try hint_processor.executeHint(std.testing.allocator, &vm, &hint_data, undefined, undefined);

// Ensure cleanup of memory data after execution.
defer vm.segments.memory.deinitData(std.testing.allocator);
try testing_utils.runHint(std.testing.allocator, &vm, ids_data, hint_codes.TEMPORARY_ARRAY, undefined, undefined);

// Verifies that the temporary array was successfully created in the memory segments.
try expectEqual(
Expand Down
12 changes: 4 additions & 8 deletions src/integration_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,10 @@ pub fn main() void {
// TODO: hint not implemented ec point
// .{ .pathname = "cairo_programs/reduce.json", .layout = "all_cairo" },

// TODO: failed DiffAssertValues
// .{ .pathname = "cairo_programs/relocate_segments_with_offset.json", .layout = "all_cairo" },
// TODO: failed DiffAssertValues
// .{ .pathname = "cairo_programs/relocate_segments.json", .layout = "all_cairo" },
// TODO: failed DiffAssertValues
// .{ .pathname = "cairo_programs/relocate_temporary_segment_append.json", .layout = "all_cairo" },
// TODO: failed DiffAssertValues
// .{ .pathname = "cairo_programs/relocate_temporary_segment_into_new.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/relocate_segments_with_offset.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/relocate_segments.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/relocate_temporary_segment_append.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/relocate_temporary_segment_into_new.json", .layout = "all_cairo" },

.{ .pathname = "cairo_programs/return.json", .layout = "all_cairo" },
.{ .pathname = "cairo_programs/reversed_register_instructions.json", .layout = "all_cairo" },
Expand Down
5 changes: 4 additions & 1 deletion src/vm/memory/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,10 @@ pub const Memory = struct {
return if (!isSegmentIndexValid or !isOffsetValid)
null
else if (data.items[segment_index].items[address.offset]) |val|
val.maybe_relocatable
switch (val.maybe_relocatable) {
.relocatable => |addr| Self.relocateAddress(addr, &self.relocation_rules) catch unreachable,
else => |_| val.maybe_relocatable,
}
else
null;
}
Expand Down

0 comments on commit c299ea7

Please sign in to comment.