From a9a3f58bbab0403de73a6c4d355182a5e6a88732 Mon Sep 17 00:00:00 2001 From: Thomas Coratger <60488569+tcoratger@users.noreply.github.com> Date: Thu, 29 Feb 2024 13:06:46 +0100 Subject: [PATCH] Add unit test for `getConstants` in Cairo runner (#409) * Add unit test for getConstants in Cairo runner * solve conflicts --------- Co-authored-by: lanaivina <31368580+lana-shanghai@users.noreply.github.com> --- src/vm/runners/cairo_runner.zig | 53 +++++++++++++++++++++++++++++++++ src/vm/types/program.zig | 16 +++++----- 2 files changed, 61 insertions(+), 8 deletions(-) diff --git a/src/vm/runners/cairo_runner.zig b/src/vm/runners/cairo_runner.zig index bf72a00b..9da04920 100644 --- a/src/vm/runners/cairo_runner.zig +++ b/src/vm/runners/cairo_runner.zig @@ -709,6 +709,21 @@ pub const CairoRunner = struct { return res; } + /// Retrieves the constant values used in the CairoRunner instance. + /// + /// This function returns a map containing the constant values used in the CairoRunner instance. + /// The constants are represented as a `StringHashMap` where the keys are the names of the constants + /// and the values are `Felt252` objects. + /// + /// # Arguments + /// - `self`: A reference to the CairoRunner instance. + /// + /// # Returns + /// A `StringHashMap` containing the constant values used in the CairoRunner instance. + pub fn getConstants(self: *Self) std.StringHashMap(Felt252) { + return self.program.constants; + } + pub fn deinit(self: *Self, allocator: Allocator) void { // currently handling the deinit of the json.Parsed(ProgramJson) outside of constructor // otherwise the runner would always assume json in its interface @@ -1378,6 +1393,44 @@ test "CairoRunner: getPermRangeCheckLimits with null range limit" { ); } +test "CairoRunner: get constants" { + // Initialize a default program with built-ins enabled using the testing allocator. + var program = try Program.initDefault(std.testing.allocator, true); + + // Add constants to the program. + try program.constants.put("MAX", Felt252.fromInt(u64, 300)); + try program.constants.put("MIN", Felt252.fromInt(u64, 20)); + + // Initialize a CairoRunner with an empty program, "plain" layout, and empty instructions list. + // Also initialize a CairoVM with an empty trace context. + var cairo_runner = try CairoRunner.init( + std.testing.allocator, + program, + "plain", + ArrayList(MaybeRelocatable).init(std.testing.allocator), + try CairoVM.init( + std.testing.allocator, + .{}, + ), + false, + ); + + // Defer the deinitialization of the CairoRunner object to ensure cleanup after the test. + defer cairo_runner.deinit(std.testing.allocator); + + // Retrieve the constants from the CairoRunner. + const runner_program_constants = cairo_runner.getConstants(); + + // Ensure that the count of constants retrieved matches the expected count (2). + try expectEqual(@as(usize, 2), runner_program_constants.count()); + + // Ensure that the constant value associated with the key "MAX" matches the expected value (300). + try expectEqual(Felt252.fromInt(u64, 300), runner_program_constants.get("MAX")); + + // Ensure that the constant value associated with the key "MIN" matches the expected value (20). + try expectEqual(Felt252.fromInt(u64, 20), runner_program_constants.get("MIN").?); +} + test "CairoRunner: initBuiltins missing builtins allow missing" { var program = try Program.initDefault(std.testing.allocator, true); try program.builtins.appendSlice(&.{ .output, .ecdsa }); diff --git a/src/vm/types/program.zig b/src/vm/types/program.zig index e5195015..6d8807b4 100644 --- a/src/vm/types/program.zig +++ b/src/vm/types/program.zig @@ -339,7 +339,7 @@ pub const Program = struct { .identifiers = identifiers, .reference_manager = try reference_manager.getReferenceList(allocator), }, - .constants = try Self.extractConstants(identifiers, allocator), + .constants = try Self.getConstants(identifiers, allocator), .builtins = builtins, }; } @@ -369,7 +369,7 @@ pub const Program = struct { /// # Returns: /// - A new `std.StringHashMap(Felt252)` instance containing extracted constants. /// - Returns an error of type `ProgramError` if there's an issue processing constants. - pub fn extractConstants( + pub fn getConstants( identifiers: std.StringHashMap(Identifier), allocator: Allocator, ) !std.StringHashMap(Felt252) { @@ -506,7 +506,7 @@ pub const Program = struct { } }; -test "Program: extractConstants should extract the constants from identifiers" { +test "Program: getConstants should extract the constants from identifiers" { // Initialize a map to store identifiers. var identifiers = std.StringHashMap(Identifier).init(std.testing.allocator); // Defer deinitialization to ensure cleanup. @@ -530,8 +530,8 @@ test "Program: extractConstants should extract the constants from identifiers" { }, ); - // Try to extract constants from the identifiers using the `extractConstants` function. - var constants = try Program.extractConstants(identifiers, std.testing.allocator); + // Try to extract constants from the identifiers using the `getConstants` function. + var constants = try Program.getConstants(identifiers, std.testing.allocator); // Defer deinitialization of the constants to ensure cleanup. defer constants.deinit(); @@ -542,7 +542,7 @@ test "Program: extractConstants should extract the constants from identifiers" { try expectEqual(Felt252.zero(), constants.get("__main__.main.SIZEOF_LOCALS").?); } -test "Program: extractConstants should extract the constants from identifiers using large values" { +test "Program: getConstants should extract the constants from identifiers using large values" { // Initialize a map to store identifiers. var identifiers = std.StringHashMap(Identifier).init(std.testing.allocator); // Defer deinitialization to ensure cleanup. @@ -611,8 +611,8 @@ test "Program: extractConstants should extract the constants from identifiers us }, ); - // Try to extract constants from the identifiers using the `extractConstants` function. - var constants = try Program.extractConstants(identifiers, std.testing.allocator); + // Try to extract constants from the identifiers using the `getConstants` function. + var constants = try Program.getConstants(identifiers, std.testing.allocator); // Defer deinitialization of the constants to ensure cleanup. defer constants.deinit();