Skip to content

Commit

Permalink
Add two-fer exercise (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Oct 21, 2024
1 parent ce9af72 commit bf7cd3f
Show file tree
Hide file tree
Showing 13 changed files with 3,522 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "two-fer",
"name": "Two-Fer",
"uuid": "4f341658-cc4d-406b-98be-1839d446baf6",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "bob",
"name": "Bob",
Expand Down
24 changes: 24 additions & 0 deletions exercises/practice/two-fer/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Instructions

Your task is to determine what you will say as you give away the extra cookie.

If you know the person's name (e.g. if they're named Do-yun), then you will say:

```text
One for Do-yun, one for me.
```

If you don't know the person's name, you will say _you_ instead.

```text
One for you, one for me.
```

Here are some examples:

| Name | Dialogue |
| :----- | :-------------------------- |
| Alice | One for Alice, one for me. |
| Bohdan | One for Bohdan, one for me. |
| | One for you, one for me. |
| Zaphod | One for Zaphod, one for me. |
8 changes: 8 additions & 0 deletions exercises/practice/two-fer/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Introduction

In some English accents, when you say "two for" quickly, it sounds like "two fer".
Two-for-one is a way of saying that if you buy one, you also get one for free.
So the phrase "two-fer" often implies a two-for-one offer.

Imagine a bakery that has a holiday offer where you can buy two cookies for the price of one ("two-fer one!").
You take the offer and (very generously) decide to give the extra cookie to someone else in the queue.
18 changes: 18 additions & 0 deletions exercises/practice/two-fer/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"two_fer.s"
],
"test": [
"two_fer_test.c"
],
"example": [
".meta/example.s"
]
},
"blurb": "Create a sentence of the form \"One for X, one for me.\".",
"source_url": "https://github.com/exercism/problem-specifications/issues/757"
}
36 changes: 36 additions & 0 deletions exercises/practice/two-fer/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
.section .rodata
prefix: .string "One for "
you: .string "you"
suffix: .string ", one for me."

.macro LOAD str
adrp x2, \str
add x2, x2, :lo12:\str
.endm

.macro APPEND str

.copy_\str:
ldrb w3, [x2], #1 /* load byte, with post-increment */
strb w3, [x0], #1 /* store byte, post-increment */
cbnz w3, .copy_\str

sub x0, x0, #1
.endm

.text
.globl two_fer

/* extern void two_fer(char *buffer, const char *name); */
two_fer:
LOAD prefix
APPEND prefix

LOAD you
tst x1, x1
csel x2, x1, x2, ne /* name if non-null, otherwise you */
APPEND you

LOAD suffix
APPEND suffix
ret
19 changes: 19 additions & 0 deletions exercises/practice/two-fer/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[1cf3e15a-a3d7-4a87-aeb3-ba1b43bc8dce]
description = "no name given"

[b4c6dbb8-b4fb-42c2-bafd-10785abe7709]
description = "a name given"

[3549048d-1a6e-4653-9a79-b0bda163e8d5]
description = "another name given"
36 changes: 36 additions & 0 deletions exercises/practice/two-fer/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
5 changes: 5 additions & 0 deletions exercises/practice/two-fer/two_fer.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl two_fer

two_fer:
ret
44 changes: 44 additions & 0 deletions exercises/practice/two-fer/two_fer_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "vendor/unity.h"

#include <stddef.h>

#define BUFFER_SIZE 80

extern void two_fer(char *buffer, const char *name);

void setUp(void) {
}

void tearDown(void) {
}

void test_no_name_given(void) {
char buffer[BUFFER_SIZE];

two_fer(buffer, NULL);
TEST_ASSERT_EQUAL_STRING("One for you, one for me.", buffer);
}

void test_a_name_given(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

two_fer(buffer, "Alice");
TEST_ASSERT_EQUAL_STRING("One for Alice, one for me.", buffer);
}

void test_another_name_given(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

two_fer(buffer, "Bob");
TEST_ASSERT_EQUAL_STRING("One for Bob, one for me.", buffer);
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_no_name_given);
RUN_TEST(test_a_name_given);
RUN_TEST(test_another_name_given);
return UNITY_END();
}
Loading

0 comments on commit bf7cd3f

Please sign in to comment.