This hands-on session focuses on implementing functions that use loops and vector operations in Move. You will complete the protocol::basic2 module to pass all the provided test cases. The module includes functions to sum numbers up to n, calculate the factorial of n, and sum the elements of a vector.
The project includes a test module (protocol::basic2_test) that verifies the correct implementation of each function in protocol::basic2.
- Sum Up to
n(sum_up_to_n): Tests the function that sums all numbers from 0 tonusing a loop. - Factorial Calculation (
factorial): Validates that the function correctly computes the factorial ofnusing awhileloop. - Sum Vector Elements (
sum_vector_elements): Checks that the function sums all elements in a given vector.
-
Ensure your Move development environment is set up and the
aptosCLI is installed. -
Navigate to your project directory in the terminal.
-
Run the tests with the following command:
aptos move testThis command will execute the test cases in
protocol::basic2_testand display the results.
Your task is to complete the functions in the protocol::basic2 module so that all test cases pass. Below are descriptions of each function:
-
Sum Up to
n(sum_up_to_n):- Description: A function that computes the sum of all numbers from 0 to
nusing aloop. - Expected Result: For an input of
5, the function should return15(0 + 1 + 2 + 3 + 4 + 5).
- Description: A function that computes the sum of all numbers from 0 to
-
Factorial (
factorial):- Description: A function that calculates the factorial of
nusing awhileloop. - Expected Result: For an input of
5, the function should return120(5! = 5 _ 4 _ 3 _ 2 _ 1).
- Description: A function that calculates the factorial of
-
Sum Vector Elements (
sum_vector_elements):- Description: A function that sums all the elements in a given
vector<u64>. - Expected Result: For a vector
[1, 2, 3, 4], the function should return10(1 + 2 + 3 + 4).
- Description: A function that sums all the elements in a given
- Loop Structures: Familiarize yourself with
loopandwhileconstructs in Move to implement the functions. - Vector Operations: Ensure you know how to iterate over elements in a vector to sum them.
- Error Handling: The functions do not need to handle error cases for this session, but edge cases like
n = 0or an empty vector should be considered.
Completing this hands-on session will give you practical experience with implementing loops, vector operations, and iterative logic in Move.