-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df3e55c
commit 7073f22
Showing
12 changed files
with
3,556 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Instructions | ||
|
||
Calculate the number of grains of wheat on a chessboard given that the number on each square doubles. | ||
|
||
There once was a wise servant who saved the life of a prince. | ||
The king promised to pay whatever the servant could dream up. | ||
Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. | ||
One grain on the first square of a chess board, with the number of grains doubling on each successive square. | ||
|
||
There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on). | ||
|
||
Write code that shows: | ||
|
||
- how many grains were on a given square, and | ||
- the total number of grains on the chessboard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"keiravillekode" | ||
], | ||
"files": { | ||
"solution": [ | ||
"grains.s" | ||
], | ||
"test": [ | ||
"grains_test.c" | ||
], | ||
"example": [ | ||
".meta/example.s" | ||
] | ||
}, | ||
"blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.", | ||
"source": "The CodeRanch Cattle Drive, Assignment 6", | ||
"source_url": "https://coderanch.com/wiki/718824/Grains" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
.text | ||
.globl square | ||
.globl total | ||
|
||
/* extern uint64_t square(int64_t number); */ | ||
square: | ||
sub x0, x0, #1 | ||
cmp x0, #64 | ||
bhs .invalid /* unsigned >= */ | ||
|
||
mov x1, #1 | ||
lsl x0, x1, x0 /* 1 << (number - 1) */ | ||
ret | ||
|
||
.invalid: | ||
mov x0, #0 | ||
ret | ||
|
||
/* extern uint64_t total(void); */ | ||
total: | ||
mov x0, #-1 /* all 64 bits set */ | ||
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# 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. | ||
|
||
[9fbde8de-36b2-49de-baf2-cd42d6f28405] | ||
description = "returns the number of grains on the square -> grains on square 1" | ||
|
||
[ee1f30c2-01d8-4298-b25d-c677331b5e6d] | ||
description = "returns the number of grains on the square -> grains on square 2" | ||
|
||
[10f45584-2fc3-4875-8ec6-666065d1163b] | ||
description = "returns the number of grains on the square -> grains on square 3" | ||
|
||
[a7cbe01b-36f4-4601-b053-c5f6ae055170] | ||
description = "returns the number of grains on the square -> grains on square 4" | ||
|
||
[c50acc89-8535-44e4-918f-b848ad2817d4] | ||
description = "returns the number of grains on the square -> grains on square 16" | ||
|
||
[acd81b46-c2ad-4951-b848-80d15ed5a04f] | ||
description = "returns the number of grains on the square -> grains on square 32" | ||
|
||
[c73b470a-5efb-4d53-9ac6-c5f6487f227b] | ||
description = "returns the number of grains on the square -> grains on square 64" | ||
|
||
[1d47d832-3e85-4974-9466-5bd35af484e3] | ||
description = "returns the number of grains on the square -> square 0 is invalid" | ||
|
||
[61974483-eeb2-465e-be54-ca5dde366453] | ||
description = "returns the number of grains on the square -> negative square is invalid" | ||
|
||
[a95e4374-f32c-45a7-a10d-ffec475c012f] | ||
description = "returns the number of grains on the square -> square greater than 64 is invalid" | ||
|
||
[6eb07385-3659-4b45-a6be-9dc474222750] | ||
description = "returns the total number of grains on the board" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.text | ||
.globl square | ||
.globl total | ||
|
||
square: | ||
ret | ||
|
||
total: | ||
ret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#include "vendor/unity.h" | ||
|
||
#include <stdint.h> | ||
|
||
extern uint64_t square(int64_t number); | ||
extern uint64_t total(void); | ||
|
||
void setUp(void) { | ||
} | ||
|
||
void tearDown(void) { | ||
} | ||
|
||
void test_grains_on_square_1(void) { | ||
TEST_ASSERT_EQUAL_UINT64(1U, square(1)); | ||
} | ||
|
||
void test_grains_on_square_2(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT64(2U, square(2)); | ||
} | ||
|
||
void test_grains_on_square_3(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT64(4U, square(3)); | ||
} | ||
|
||
void test_grains_on_square_4(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT64(8U, square(4)); | ||
} | ||
|
||
void test_grains_on_square_16(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT64(32768U, square(16)); | ||
} | ||
|
||
void test_grains_on_square_32(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT64(2147483648U, square(32)); | ||
} | ||
|
||
void test_grains_on_square_64(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT64(9223372036854775808U, square(64)); | ||
} | ||
|
||
void test_square_0_is_invalid(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT64(0U, square(0)); | ||
} | ||
|
||
void test_negative_square_is_invalid(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT64(0U, square(-1)); | ||
} | ||
|
||
void test_square_greater_than_64_is_invalid(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT64(0U, square(65)); | ||
} | ||
|
||
void test_returns_the_total_number_of_grains_on_the_board(void) { | ||
TEST_IGNORE(); | ||
TEST_ASSERT_EQUAL_UINT64(18446744073709551615U, total()); | ||
} | ||
|
||
int main(void) { | ||
UNITY_BEGIN(); | ||
RUN_TEST(test_grains_on_square_1); | ||
RUN_TEST(test_grains_on_square_2); | ||
RUN_TEST(test_grains_on_square_3); | ||
RUN_TEST(test_grains_on_square_4); | ||
RUN_TEST(test_grains_on_square_16); | ||
RUN_TEST(test_grains_on_square_32); | ||
RUN_TEST(test_grains_on_square_64); | ||
RUN_TEST(test_square_0_is_invalid); | ||
RUN_TEST(test_negative_square_is_invalid); | ||
RUN_TEST(test_square_greater_than_64_is_invalid); | ||
RUN_TEST(test_returns_the_total_number_of_grains_on_the_board); | ||
return UNITY_END(); | ||
} |
Oops, something went wrong.