From eb9b46e34d46902585ccef446e56f0a37936e509 Mon Sep 17 00:00:00 2001 From: Sasha Schwartz Date: Sat, 3 May 2025 09:48:54 -0500 Subject: [PATCH] add Odin lang --- odin/.gitignore | 3 + odin/README.md | 34 +++++++++++ odin/gilded_rose/main.odin | 98 ++++++++++++++++++++++++++++++++ odin/tests/gilded_rose_test.odin | 11 ++++ texttests/config.gr | 3 + 5 files changed, 149 insertions(+) create mode 100644 odin/.gitignore create mode 100644 odin/README.md create mode 100644 odin/gilded_rose/main.odin create mode 100644 odin/tests/gilded_rose_test.odin diff --git a/odin/.gitignore b/odin/.gitignore new file mode 100644 index 00000000..2f0031b3 --- /dev/null +++ b/odin/.gitignore @@ -0,0 +1,3 @@ +*.o +*.bin +*.exe diff --git a/odin/README.md b/odin/README.md new file mode 100644 index 00000000..92e596ec --- /dev/null +++ b/odin/README.md @@ -0,0 +1,34 @@ +# Gilded Rose starting position in Odin + +Learn how to install Odin on your system [here](https://odin-lang.org/docs/install/) + +## Run unit tests from the command line +``` +$ odin test tests +``` +## Run the TextTest fixture on the command line +Build and install the executable + +``` +$ odin build gilded_rose +``` + +Execute it on the command line with an argument for the number of days: + +### macOS: + +``` +$ ./gilded_rose.bin 10 +``` +### Windows: + +``` +$ ./gilded_rose.exe 10 +``` + +## Run the TextTest approval test that comes with this project +There are instructions in the TextTest Readme for setting up TextTest. You will need to specify the executable in `config.gr`. Uncomment this line to use it: + +``` +#executable:${TEXTTEST_HOME}/odin/gilded_rose(include your OS executable extension here) +``` diff --git a/odin/gilded_rose/main.odin b/odin/gilded_rose/main.odin new file mode 100644 index 00000000..cd603525 --- /dev/null +++ b/odin/gilded_rose/main.odin @@ -0,0 +1,98 @@ +package gilded_rose + +import "core:fmt" +import "core:os" +import "core:strconv" + +Item :: struct { + name: string, + sell_in: i32, + quality: i32, +} + +update_quality :: proc(inventory: []Item) { + for &item in inventory { + if item.name != "Aged Brie" && item.name != "Backstage passes to a TAFKAL80ETC concert" { + if item.quality > 0 { + if item.name != "Sulfuras, Hand of Ragnaros" { + item.quality = item.quality - 1 + } + } + } else { + if item.quality < 50 { + item.quality = item.quality + 1 + if item.name == "Backstage passes to a TAFKAL80ETC concert" { + if item.sell_in < 11 { + if item.quality < 50 { + item.quality = item.quality + 1 + } + } + if item.sell_in < 6 { + if item.quality < 50 { + item.quality = item.quality + 1 + } + } + } + } + } + if item.name != "Sulfuras, Hand of Ragnaros" { + item.sell_in = item.sell_in - 1 + } + if item.sell_in < 0 { + if item.name != "Aged Brie" { + if item.name != "Backstage passes to a TAFKAL80ETC concert" { + if item.quality > 0 { + if item.name != "Sulfuras, Hand of Ragnaros" { + item.quality = item.quality - 1 + } + } + } else { + item.quality = item.quality - item.quality + } + } else { + if item.quality < 50 { + item.quality = item.quality + 1 + } + } + } + } +} + +main :: proc() { + days := 2 + if len(os.args) >= 2 { + if val, ok := strconv.parse_int(os.args[1]); ok && val > 0 { + days = val + } else { + fmt.eprintf("Please enter a number greater than 0\n") + return + } + } + + items := []Item { + {"+5 Dexterity Vest", 10, 20}, + {"Aged Brie", 2, 0}, + {"Elixir of the Mongoose", 5, 7}, + {"Sulfuras, Hand of Ragnaros", 0, 80}, + {"Sulfuras, Hand of Ragnaros", -1, 80}, + {"Backstage passes to a TAFKAL80ETC concert", 15, 20}, + {"Backstage passes to a TAFKAL80ETC concert", 10, 49}, + {"Backstage passes to a TAFKAL80ETC concert", 5, 49}, + // This Conjured item does not work properly yet + {"Conjured Mana Cake", 3, 6}, + } + + fmt.println("OMGHAI!") + + for day in 0 ..= days { + day_line := fmt.tprint("-------- day", day, "--------") + fmt.println(day_line) + fmt.println("name, sellIn, quality") + for item in items { + item_line := fmt.tprint(item.name, item.sell_in, item.quality, sep = ", ") + fmt.println(item_line) + } + fmt.println() + update_quality(items) + } +} diff --git a/odin/tests/gilded_rose_test.odin b/odin/tests/gilded_rose_test.odin new file mode 100644 index 00000000..cdbc1e4b --- /dev/null +++ b/odin/tests/gilded_rose_test.odin @@ -0,0 +1,11 @@ +package test_gilded_rose + +import GildedRose "../gilded_rose" +import "core:testing" + +@(test) +update_quality_test :: proc(t: ^testing.T) { + items := []GildedRose.Item{{"food", 0, 0}} + GildedRose.update_quality(items) + testing.expect(t, "fixme" == items[0].name, "Fix this test") +} diff --git a/texttests/config.gr b/texttests/config.gr index b7166cd0..6972127e 100755 --- a/texttests/config.gr +++ b/texttests/config.gr @@ -64,4 +64,7 @@ interpreter:ruby # Settings for the OCaml version # executable:${TEXTTEST_HOME}/ocaml/_build/default/bin/main.exe +# Settings for the Odin version +#executable:${TEXTTEST_HOME}/odin/gilded_rose(include your OS executable extension here) + filename_convention_scheme:standard