Skip to content

Commit

Permalink
Add unit tests for hint utils (keep-starknet-strange#463)
Browse files Browse the repository at this point in the history
* Add unit tests for getRelocatableFromVarName

* Add unit tests for getConstantFromVarName

---------

Co-authored-by: lanaivina <[email protected]>
  • Loading branch information
tcoratger and lana-shanghai authored Mar 17, 2024
1 parent 5b451a9 commit a98a222
Showing 1 changed file with 118 additions and 1 deletion.
119 changes: 118 additions & 1 deletion src/hint_processor/hint_utils.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ const Allocator = std.mem.Allocator;
const HintError = @import("../vm/error.zig").HintError;
const hint_processor_utils = @import("hint_processor_utils.zig");

/// Retrieves a constant value based on its variable name.
///
/// This function searches for a constant with a variable name suffix in the provided hashmap of constants.
/// It iterates over the hashmap keys, comparing them with the variable name suffix.
/// If a match is found, it returns the corresponding constant value.
///
/// # Parameters
/// - `var_name`: The suffix of the variable name associated with the constant.
/// - `constants`: A pointer to the hashmap containing constant names and their corresponding values.
///
/// # Returns
/// Returns the constant value associated with the variable name suffix.
/// If no matching constant is found, it returns an error of type `HintError`.
pub fn getConstantFromVarName(
var_name: []const u8,
constants: *const std.StringHashMap(Felt252),
Expand Down Expand Up @@ -129,7 +142,20 @@ pub fn getAddressFromVarName(
);
}

//Gets the address, as a Relocatable of the variable given by the ids name
/// Retrieves the memory address, as a `Relocatable`, of the variable indicated by its name.
///
/// This function retrieves the memory address of the variable indicated by the provided `var_name`.
/// It looks up the variable name in the `ids_data` hashmap, then calculates the memory address based on the hint reference and AP tracking information.
///
/// # Parameters
/// - `var_name`: The name of the variable to retrieve.
/// - `vm`: A pointer to the Cairo virtual machine.
/// - `ids_data`: A hashmap containing variable names and their corresponding hint references.
/// - `ap_tracking`: The AP tracking data.
///
/// # Returns
/// Returns the memory address of the variable indicated by the variable name as a `Relocatable`.
/// If the variable is not found or if there's an error retrieving the address, it returns an error of type `Relocatable`.
pub fn getRelocatableFromVarName(
var_name: []const u8,
vm: *CairoVM,
Expand Down Expand Up @@ -341,3 +367,94 @@ test "getPtrFromVarName: invalid" {
getPtrFromVarName("value", &vm, ids_data, .{}),
);
}

test "getRelocatableFromVarName: valid" {
// Initializes the Cairo virtual machine.
var vm = try CairoVM.init(std.testing.allocator, .{});
defer vm.deinit(); // Ensure cleanup.

// Sets up memory segments in the virtual machine with an invalid configuration.
try vm.segments.memory.setUpMemory(
std.testing.allocator,
.{.{ .{ 1, 0 }, .{0} }},
);
defer vm.segments.memory.deinitData(std.testing.allocator); // Clean up memory data.

// 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("value", HintReference.initSimple(0));

// Invokes `getPtrFromVarName` to retrieve the pointer from the "value" variable, expecting an error.
try expectEqual(
Relocatable.init(1, 0),
try getRelocatableFromVarName("value", &vm, ids_data, .{}),
);
}

test "getRelocatableFromVarName: invalid" {
// Initializes the Cairo virtual machine.
var vm = try CairoVM.init(std.testing.allocator, .{});
defer vm.deinit(); // Ensure cleanup.

// Sets up memory segments in the virtual machine with an invalid configuration.
try vm.segments.memory.setUpMemory(
std.testing.allocator,
.{.{ .{ 1, 0 }, .{0} }},
);
defer vm.segments.memory.deinitData(std.testing.allocator); // Clean up memory data.

// 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("value", HintReference.initSimple(-8));

// Invokes `getPtrFromVarName` to retrieve the pointer from the "value" variable, expecting an error.
try expectEqual(
HintError.UnknownIdentifier,
getRelocatableFromVarName("value", &vm, ids_data, .{}),
);
}

test "getConstantFromVarName: valid without point in the middle of var name" {
var constants = std.StringHashMap(Felt252).init(std.testing.allocator);
defer constants.deinit();

try constants.put("value", Felt252.fromInt(u256, 2));
try constants.put("ids.MAX_LOW", Felt252.fromInt(u256, 20));

try expectEqual(
Felt252.fromInt(u256, 2),
getConstantFromVarName("value", &constants),
);
}

test "getConstantFromVarName: valid with a point in the middle of var name" {
var constants = std.StringHashMap(Felt252).init(std.testing.allocator);
defer constants.deinit();

try constants.put("value", Felt252.fromInt(u256, 2));
try constants.put("ids.MAX_LOW", Felt252.fromInt(u256, 20));

try expectEqual(
Felt252.fromInt(u256, 20),
getConstantFromVarName("MAX_LOW", &constants),
);
}

test "getConstantFromVarName: invalid" {
var constants = std.StringHashMap(Felt252).init(std.testing.allocator);
defer constants.deinit();

try constants.put("value", Felt252.fromInt(u256, 2));
try constants.put("ids.MAX_LOW", Felt252.fromInt(u256, 20));

try expectError(
HintError.MissingConstant,
getConstantFromVarName("MAX_HIGH", &constants),
);
}

0 comments on commit a98a222

Please sign in to comment.