diff --git a/src/hint_processor/segments.zig b/src/hint_processor/segments.zig index f2766575..69606713 100644 --- a/src/hint_processor/segments.zig +++ b/src/hint_processor/segments.zig @@ -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; @@ -94,24 +95,15 @@ 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(); } @@ -119,6 +111,8 @@ 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; @@ -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( diff --git a/src/integration_tests.zig b/src/integration_tests.zig index 06b71b53..663b9640 100644 --- a/src/integration_tests.zig +++ b/src/integration_tests.zig @@ -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" }, diff --git a/src/vm/memory/memory.zig b/src/vm/memory/memory.zig index 508a72b6..b537fc09 100644 --- a/src/vm/memory/memory.zig +++ b/src/vm/memory/memory.zig @@ -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; }