Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests to int tests #93

Merged
merged 15 commits into from
Jul 18, 2024
2 changes: 1 addition & 1 deletion bin/verify-exercises
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ slug="${1:-*}"

verify_exercise() {
exercises_path="$repo/exercises/$1/$slug"
if ! [[ "$slug" == "*" || -d "$exercises_path" ]]; then
if ! [[ "$slug" == "*" || -d "$exercises_path" ]]; then
return
fi
source_file_name="$2"
Expand Down
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"Scarb.toml"
],
"test": [
"src/tests.cairo"
"tests/%{snake_slug}.cairo"
],
"example": [
".meta/example.cairo"
Expand All @@ -34,7 +34,7 @@
"concept": [
{
"slug": "low-power-embedded-game",
"name": "low-power-embedded-game",
"name": "Low-power Embedded Game",
"uuid": "f3b7ce44-1667-42b4-b792-401d36aee2f1",
"concepts": [
"tuples",
Expand Down
2 changes: 2 additions & 0 deletions docs/ABOUT.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ This means that you can perform time consuming operations on a machine you don't

One major usecase of Cairo is writing smart contracts for [Starknet](https://www.starknet.io/), a Layer 2 built on top of Ethereum. Instead of having all the participants of the network verify all user interactions, only one node, called the prover, executes the programs and generates proofs that the computations were done correctly. These proofs are then verified by an Ethereum smart contract, requiring significantly less computational power, which increases throughput and reduces transaction costs while preserving Ethereum security.

Another notable use case of Cairo is in AI model training. Suppose you have a large dataset and wish to train an AI model on it but lack the necessary infrastructure. You can pay a major AI model provider to train the AI on your data and return the trained model. Since the provider used Cairo to write and train their AI model, they can produce a "proof" that you can use to verify that they did actually use the complete dataset you provided, ensuring the integrity of the training process and guaranteeing that no other data or partial data was used.

Cairo differs significantly from traditional programming languages, particularly in its execution and performance optimizations. Programs can be executed by a prover, similar to other languages, though with some performance overhead due to virtualization of the language. When proofs are verified, efficiency is crucial as verification may occur on small machines, and Cairo has various advantages to improve verification speed. A notable one is [non-determinism][np], which is the idea that you can theoretically use a different algorithm for verifying than for computing. Take the example of sorting an array in Cairo - the prover has to sort the array, while the verifier only needs to check that the array is sorted, which is much cheaper. Additionally, Cairo's memory model is immutable, meaning values cannot be changed once written. Cairo provides abstractions that help developers work with these constraints, but it does not fully simulate mutability. This means that developers are required to be mindful of memory management and data structures to optimize performance.

The home page for Cairo is [cairo-lang.org](https://www.cairo-lang.org/).
Expand Down
9 changes: 7 additions & 2 deletions docs/TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@ cd path/to/exercise
And then run the following command:

```bash
$ scarb cairo-test
scarb cairo-test
```

Only the first test is enabled by default.
After you are ready to pass the next test, remove the ignore flag from the next test (`#[ignore]`).
You can also remove the flag from all the tests at once if you prefer.

To run all tests including the ignored ones, you can remove the `ignore` flag from all the tests, or you can just run:

```bash
scarb cairo-test --include-ignored
```

Feel free to write as little code as possible to get the tests to pass.
The test failures will guide you to what should be written next.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"Scarb.toml"
],
"test": [
"src/tests.cairo"
"tests/low_power_embedded_game.cairo"
],
"exemplar": [
".meta/exemplar.cairo"
Expand Down
3 changes: 0 additions & 3 deletions exercises/concept/low-power-embedded-game/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,3 @@ impl PositionImpl of PositionTrait {
panic!("implement `fn manhattan`")
}
}

#[cfg(test)]
mod tests;
2 changes: 1 addition & 1 deletion exercises/practice/allergies/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"Scarb.toml"
],
"test": [
"src/tests.cairo"
"tests/allergies.cairo"
],
"example": [
".meta/example.cairo"
Expand Down
8 changes: 2 additions & 6 deletions exercises/practice/allergies/.meta/example.cairo
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use core::traits::TryInto;
#[derive(Drop)]
struct Allergies {
score: u32,
}

#[derive(Drop, Debug, PartialEq)]
enum Allergen {
pub enum Allergen {
Eggs,
Peanuts,
Shellfish,
Expand All @@ -17,7 +16,7 @@ enum Allergen {
}

#[generate_trait]
impl AllergiesImpl of AllergiesTrait {
pub impl AllergiesImpl of AllergiesTrait {
fn new(score: u32) -> Allergies {
Allergies { score }
}
Expand Down Expand Up @@ -73,6 +72,3 @@ fn pow(base: u32, mut power: u32) -> u32 {
};
result.try_into().expect('too large to fit output type')
}

#[cfg(test)]
mod tests;
7 changes: 2 additions & 5 deletions exercises/practice/allergies/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
struct Allergies {}

#[derive(Drop, Debug, PartialEq)]
enum Allergen {
pub enum Allergen {
Eggs,
Peanuts,
Shellfish,
Expand All @@ -14,7 +14,7 @@ enum Allergen {
}

#[generate_trait]
impl AllergiesImpl of AllergiesTrait {
pub impl AllergiesImpl of AllergiesTrait {
fn new(score: u32) -> Allergies {
panic!("Given the '{score}' score, construct a new Allergies struct.")
}
Expand All @@ -29,6 +29,3 @@ impl AllergiesImpl of AllergiesTrait {
)
}
}

#[cfg(test)]
mod tests;
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@ fn compare_allergy_vectors(expected: @Array<Allergen>, actual: @Array<Allergen>)
};
}


#[test]
#[ignore]
fn list_when_no_allergies() {
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/anagram/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"Scarb.toml"
],
"test": [
"src/tests.cairo"
"tests/anagram.cairo"
],
"example": [
".meta/example.cairo"
Expand Down
7 changes: 2 additions & 5 deletions exercises/practice/anagram/.meta/example.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ struct Set {
}

#[generate_trait]
impl SetImpl of SetTrait {
pub impl SetImpl of SetTrait {
fn new(values: Array<ByteArray>) -> Set {
Set { values }
}
Expand Down Expand Up @@ -41,7 +41,7 @@ impl SetEq of PartialEq<Set> {
}
}

fn anagrams_for(word: @ByteArray, inputs: @Set) -> Set {
pub fn anagrams_for(word: @ByteArray, inputs: @Set) -> Set {
let mut word_sorted = @sort_ignore_case(word);
let mut anagrams = Set { values: array![] };
let mut i = inputs.values.len();
Expand Down Expand Up @@ -127,6 +127,3 @@ fn lowercase(char: @u8) -> u8 {
*char
}
}

#[cfg(test)]
mod tests;
7 changes: 2 additions & 5 deletions exercises/practice/anagram/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
struct Set {}

#[generate_trait]
impl SetImpl of SetTrait {
pub impl SetImpl of SetTrait {
fn new(values: Array<ByteArray>) -> Set {
panic!()
}
Expand All @@ -18,9 +18,6 @@ impl SetEq of PartialEq<Set> {
}
}

fn anagrams_for(word: @ByteArray, inputs: @Set) -> Set {
pub fn anagrams_for(word: @ByteArray, inputs: @Set) -> Set {
panic!()
}

#[cfg(test)]
mod tests;
2 changes: 1 addition & 1 deletion exercises/practice/armstrong-numbers/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"Scarb.toml"
],
"test": [
"src/tests.cairo"
"tests/armstrong_numbers.cairo"
],
"example": [
".meta/example.cairo"
Expand Down
5 changes: 1 addition & 4 deletions exercises/practice/armstrong-numbers/.meta/example.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn is_armstrong_number(mut num: u128) -> bool {
pub fn is_armstrong_number(mut num: u128) -> bool {
let mut original_num = num;
let digits = count_digits(num);
loop {
Expand Down Expand Up @@ -37,6 +37,3 @@ fn pow(base: u128, mut power: u128) -> u128 {
};
result.try_into().expect('too large to fit output type')
}

#[cfg(test)]
mod tests;
5 changes: 1 addition & 4 deletions exercises/practice/armstrong-numbers/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
fn is_armstrong_number(num: u128) -> bool {
pub fn is_armstrong_number(num: u128) -> bool {
panic!("true if {num} is an armstrong number")
}

#[cfg(test)]
mod tests;
2 changes: 1 addition & 1 deletion exercises/practice/beer-song/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"Scarb.toml"
],
"test": [
"src/tests.cairo"
"tests/beer_song.cairo"
],
"example": [
".meta/example.cairo"
Expand Down
11 changes: 3 additions & 8 deletions exercises/practice/beer-song/.meta/example.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
fn verse(n: u32) -> ByteArray {
pub fn verse(n: u32) -> ByteArray {
match n {
0 => "No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.\n",
1 => "1 bottle of beer on the wall, 1 bottle of beer.\nTake it down and pass it around, no more bottles of beer on the wall.\n",
Expand All @@ -10,8 +10,7 @@ fn verse(n: u32) -> ByteArray {
}
}


fn verses(start: u32, take_count: u32) -> ByteArray {
pub fn verses(start: u32, take_count: u32) -> ByteArray {
assert!(0 <= start && start <= 99, "Start verse must be between 0 and 99");
assert!(0 <= take_count && take_count <= 99, "Count of bottles must be between 0 and 99");
assert!(take_count <= start, "Cannot take down more than {start} bottles");
Expand All @@ -32,10 +31,6 @@ fn verses(start: u32, take_count: u32) -> ByteArray {
lyrics
}

fn song() -> ByteArray {
pub fn song() -> ByteArray {
verses(99, 99)
}

#[cfg(test)]
mod tests;

10 changes: 3 additions & 7 deletions exercises/practice/beer-song/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
fn verse(n: u32) -> ByteArray {
pub fn verse(n: u32) -> ByteArray {
panic!("emit verse {n}")
}

fn verses(start: u32, take_count: u32) -> ByteArray {
pub fn verses(start: u32, take_count: u32) -> ByteArray {
panic!("sing {take_count} verses from {start}")
}

fn song() -> ByteArray {
pub fn song() -> ByteArray {
panic!("sing the whole song")
}

#[cfg(test)]
mod tests;

Loading
Loading