Skip to content

Commit

Permalink
Add unit test for getConstants in Cairo runner (#409)
Browse files Browse the repository at this point in the history
* Add unit test for getConstants in Cairo runner

* solve conflicts

---------

Co-authored-by: lanaivina <[email protected]>
  • Loading branch information
tcoratger and lana-shanghai authored Feb 29, 2024
1 parent 4248783 commit a9a3f58
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
53 changes: 53 additions & 0 deletions src/vm/runners/cairo_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 });
Expand Down
16 changes: 8 additions & 8 deletions src/vm/types/program.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand All @@ -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();

Expand All @@ -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.
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit a9a3f58

Please sign in to comment.