From 988231e8b35a0b19dbdf5fe6f673fee0093d2de2 Mon Sep 17 00:00:00 2001 From: Nenad <xinef.it@gmail.com> Date: Wed, 25 Dec 2024 20:02:12 +0100 Subject: [PATCH] Add Concept Exercise Metadata files: the-realm-of-echoes (#326) --- .../the-realm-of-echoes/.docs/hints.md | 27 ++++++ .../the-realm-of-echoes/.docs/introduction.md | 46 ++++++++++ .../the-realm-of-echoes/.meta/config.json | 2 +- .../the-realm-of-echoes/.meta/design.md | 36 ++++++++ .../tests/the_realm_of_echoes.cairo | 83 +++++++++++++++++-- 5 files changed, 184 insertions(+), 10 deletions(-) diff --git a/exercises/concept/the-realm-of-echoes/.docs/hints.md b/exercises/concept/the-realm-of-echoes/.docs/hints.md index e69de29b..9e5c7101 100644 --- a/exercises/concept/the-realm-of-echoes/.docs/hints.md +++ b/exercises/concept/the-realm-of-echoes/.docs/hints.md @@ -0,0 +1,27 @@ +# Hints + +## General + +- [The Cairo Book: ByteArrays][book-bytearrays] +- [Starknet By Example: Strings & ByteArrays][sbe-strings] + +### 1. Format a magical chant + +- Use the `format!` macro to combine multiple `ByteArray` inputs into a single string. +- Separate the chants with a delimiter such as `"-"` to match the expected output. +- Ensure the output is returned as a `ByteArray`. + +### 2. Implement a `Display` trait for `EchoStone` + +- The `Display` trait allows you to define how the `EchoStone` should appear when printed with `{}`. +- Use the `write!` macro within the `fmt` function to format the output string. +- Include both `power` and `duration` fields in a clear and concise format, e.g., `EchoStone [power: X, duration: Y]`. + +### 3. Implement a `Debug` trait for `EchoStone` + +- The `Debug` trait is used for debugging purposes, allowing for more detailed or structured output when printed with `{:?}`. +- Follow a clear debugging format, such as `Debugging EchoStone: { power: X, duration: Y }`. +- Use the `write!` macro to create the debug output. + +[book-bytearrays]: https://book.cairo-lang.org/ch02-02-data-types.html#string-types +[sbe-strings]: https://starknet-by-example.voyager.online/getting-started/basics/bytearrays-strings diff --git a/exercises/concept/the-realm-of-echoes/.docs/introduction.md b/exercises/concept/the-realm-of-echoes/.docs/introduction.md index e69de29b..defc29ef 100644 --- a/exercises/concept/the-realm-of-echoes/.docs/introduction.md +++ b/exercises/concept/the-realm-of-echoes/.docs/introduction.md @@ -0,0 +1,46 @@ +# Introduction + +Printing in Cairo allows you to display messages or debug information during program execution. + +## Basics + +Cairo provides two macros for printing: + +- `println!`: Outputs a message followed by a newline. +- `print!`: Outputs a message without a newline. + +```rust +println!("Hello, Cairo!"); +println!("x = {}, y = {}", 10, 20); +``` + +Placeholders `{}` are replaced with provided values. + +## Formatting Strings + +Use `format!` to create a `ByteArray` without immediately printing: + +```rust +let result = format!("{}-{}-{}", "tic", "tac", "toe"); +println!("{}", result); // Output: tic-tac-toe +``` + +## Custom Data Types + +For custom types, implement `Display` or derive `Debug` for printing: + +```rust +#[derive(Debug)] +struct Point { x: u8, y: u8 } + +let p = Point { x: 3, y: 4 }; +println!("{:?}", p); // Debug output: Point { x: 3, y: 4 } +``` + +## Hexadecimal Printing + +Use `{:x}` to print integers as hexadecimal: + +```rust +println!("{:x}", 255); // Output: ff +``` diff --git a/exercises/concept/the-realm-of-echoes/.meta/config.json b/exercises/concept/the-realm-of-echoes/.meta/config.json index 8af744c3..83352bc6 100644 --- a/exercises/concept/the-realm-of-echoes/.meta/config.json +++ b/exercises/concept/the-realm-of-echoes/.meta/config.json @@ -16,5 +16,5 @@ "Scarb.toml" ] }, - "blurb": "<blurb>" + "blurb": "Learn printing and formatting values with the art of EchoSpeak." } diff --git a/exercises/concept/the-realm-of-echoes/.meta/design.md b/exercises/concept/the-realm-of-echoes/.meta/design.md index e69de29b..4737e9f1 100644 --- a/exercises/concept/the-realm-of-echoes/.meta/design.md +++ b/exercises/concept/the-realm-of-echoes/.meta/design.md @@ -0,0 +1,36 @@ +# Design + +## Goal + +Introduce students to the use of formatting traits like `Display` and `Debug` and how to concatenate strings effectively using the `format!` macro. + +## Learning Objectives + +- Understand and implement the `Display` trait for custom types. +- Understand and implement the `Debug` trait for custom types. +- Learn how to use the `format!` macro to concatenate strings and format output. +- Explore the differences between the `Display` and `Debug` traits in formatting. + +## Out of Scope + +- Advanced string manipulation techniques. +- Error handling mechanisms beyond basic usage. +- Interaction with non-primitive custom types within formatting. + +## Concepts + +- printing + +## Prerequisites + +- traits +- strings +- structs + +## Resources to Refer To + +- [Cairo Book - Printing][printing] +- [Cairo Book - Macros][macros] + +[printing]: https://book.cairo-lang.org/ch11-08-printing.html +[macros]: https://book.cairo-lang.org/ch11-05-macros.html diff --git a/exercises/concept/the-realm-of-echoes/tests/the_realm_of_echoes.cairo b/exercises/concept/the-realm-of-echoes/tests/the_realm_of_echoes.cairo index 69e31eab..95601ebc 100644 --- a/exercises/concept/the-realm-of-echoes/tests/the_realm_of_echoes.cairo +++ b/exercises/concept/the-realm-of-echoes/tests/the_realm_of_echoes.cairo @@ -1,21 +1,86 @@ use the_realm_of_echoes::{EchoStone, format_magical_chant}; +const U32_MAX: u32 = 0xFFFFFFFF; + +#[test] +fn format_magical_chant_basic() { + let chant1 = "abra"; + let chant2 = "cadabra"; + let chant3 = "alakazam"; + assert_eq!(format_magical_chant(chant1, chant2, chant3), "abra-cadabra-alakazam"); +} + +#[test] +#[ignore] +fn format_magical_chant_empty_strings() { + let chant1 = ""; + let chant2 = ""; + let chant3 = ""; + assert_eq!(format_magical_chant(chant1, chant2, chant3), "--"); +} + +#[test] +#[ignore] +fn format_magical_chant_mixed_empty_and_non_empty() { + let chant1 = "abra"; + let chant2 = ""; + let chant3 = "alakazam"; + assert_eq!(format_magical_chant(chant1, chant2, chant3), "abra--alakazam"); +} + +#[test] +#[ignore] +fn format_magical_chant_with_special_characters() { + let chant1 = "ab@ra"; + let chant2 = "ca!dabra"; + let chant3 = "ala#kazam"; + assert_eq!(format_magical_chant(chant1, chant2, chant3), "ab@ra-ca!dabra-ala#kazam"); +} + +#[test] +#[ignore] +fn echo_stone_display_basic() { + let stone = EchoStone { power: 100, duration: 50 }; + assert_eq!(format!("{stone}"), "EchoStone [power: 100, duration: 50]"); +} + #[test] -fn test_format_magical_chant() { - let result = format_magical_chant("Spark", "Shine", "Glow"); - assert_eq!(result, "Spark-Shine-Glow"); +#[ignore] +fn echo_stone_display_zero_values() { + let stone = EchoStone { power: 0, duration: 0 }; + assert_eq!(format!("{stone}"), "EchoStone [power: 0, duration: 0]"); +} + + +#[test] +#[ignore] +fn echo_stone_display_large_values() { + let stone = EchoStone { power: U32_MAX, duration: U32_MAX }; + assert_eq!( + format!("{stone}"), format!("EchoStone [power: {}, duration: {}]", U32_MAX, U32_MAX), + ); +} + +#[test] +#[ignore] +fn echo_stone_debug_basic() { + let stone = EchoStone { power: 100, duration: 50 }; + assert_eq!(format!("{stone:?}"), "Debugging EchoStone: { power: 100, duration: 50 }"); } #[test] #[ignore] -fn test_stringify_echo_stone() { - let stone = EchoStone { power: 100, duration: 300 }; - assert_eq!(format!("{stone}"), "EchoStone [power: 100, duration: 300]"); +fn echo_stone_debug_zero_values() { + let stone = EchoStone { power: 0, duration: 0 }; + assert_eq!(format!("{stone:?}"), "Debugging EchoStone: { power: 0, duration: 0 }"); } #[test] #[ignore] -fn test_debug_echo_stone() { - let stone = EchoStone { power: 100, duration: 300 }; - assert_eq!(format!("{:?}", stone), "Debugging EchoStone: { power: 100, duration: 300 }"); +fn echo_stone_debug_large_values() { + let stone = EchoStone { power: U32_MAX, duration: U32_MAX }; + assert_eq!( + format!("{stone:?}"), + format!("Debugging EchoStone: {{ power: {}, duration: {} }}", U32_MAX, U32_MAX), + ); }