Skip to content

Commit bc0105c

Browse files
authored
Secret Handshake: Revised the tests and .meta/example.c. (#1070)
* Secret Handshake: Revised the tests and .meta/example.c. - The previous version of tests let solutions with extra actions pass. - The tests now require that each solution returns an array of size 4, and the unused slots of the array be filled with NULL. - .meta/example.c has been revised suitably so that it passes all revised tests. * Secret Handshake: Reformat three C files with clang-format. * Secret Handshake: Removed unnecessary type cast of calloc. * Secret Handshake: Run format.sh for correcting the format of codes. * Secret Handshake: Format fix of one more file. * Update exercises/practice/secret-handshake/test_secret_handshake.c Co-authored-by: Ryan Hartlage <[email protected]> --------- Co-authored-by: Ryan Hartlage <[email protected]> [no important files changed]
1 parent 3a2b9d4 commit bc0105c

File tree

2 files changed

+39
-75
lines changed

2 files changed

+39
-75
lines changed
Lines changed: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,21 @@
11
#include "secret_handshake.h"
2+
#include <stdint.h>
23
#include <stdlib.h>
3-
#include <stdio.h>
4-
#include <stdbool.h>
54

6-
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
5+
const char *actions[] = { "wink", "double blink", "close your eyes", "jump" };
76

8-
typedef struct {
9-
const char *cmd;
10-
const size_t num;
11-
} cmd_t;
12-
13-
static const cmd_t cmds[] = {
14-
{ "wink", 1 }, { "double blink", 2 }, { "close your eyes", 4 }, { "jump", 8 }
15-
};
16-
17-
static const size_t CMD_COUNT = ARRAY_SIZE(cmds);
18-
static const size_t CMD_REVERSE = 16;
19-
20-
/* bitcount: count the number of set bits in n */
21-
static size_t bitcount(size_t num)
7+
const char **commands(const size_t number)
228
{
23-
size_t count;
24-
for (count = 0; num; num = num & (num - 1), count++)
25-
;
26-
return count;
27-
}
28-
29-
const char **commands(size_t number)
30-
{
31-
if (number > 31)
32-
number %= 32;
33-
34-
bool reverse = false;
35-
if ((number & CMD_REVERSE) == CMD_REVERSE) {
36-
number -= CMD_REVERSE;
37-
reverse = true;
9+
const char **action_sequence = calloc(4, sizeof(char *));
10+
size_t k = 0;
11+
if (number & 16) {
12+
for (int i = 3; i >= 0; i--)
13+
if (number & 1 << i)
14+
action_sequence[k++] = actions[i];
15+
} else {
16+
for (int i = 0; i <= 3; i++)
17+
if (number & 1 << i)
18+
action_sequence[k++] = actions[i];
3819
}
39-
40-
const size_t len = bitcount(number);
41-
const char **cmd_array = malloc(sizeof(char *) * (len ? len : 1));
42-
if (cmd_array == NULL) {
43-
fprintf(stderr, "Memory error!\n");
44-
return NULL;
45-
}
46-
47-
if (len == 0) {
48-
cmd_array[0] = NULL;
49-
return cmd_array;
50-
}
51-
52-
size_t pos_cmd_array = 0;
53-
for (size_t i = 0; i < CMD_COUNT; i++) {
54-
size_t k = reverse ? (CMD_COUNT - 1 - i) : i;
55-
if ((number & cmds[k].num) == cmds[k].num)
56-
cmd_array[pos_cmd_array++] = cmds[k].cmd;
57-
}
58-
59-
return cmd_array;
20+
return action_sequence;
6021
}

exercises/practice/secret-handshake/test_secret_handshake.c

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
#include "test-framework/unity.h"
21
#include "secret_handshake.h"
2+
#include "test-framework/unity.h"
33
#include <stdlib.h>
44

5-
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
5+
#define ARRAY_SIZE 4
6+
7+
#undef TEST_IGNORE
8+
#define TEST_IGNORE()
69

710
void setUp(void)
811
{
@@ -14,72 +17,72 @@ void tearDown(void)
1417

1518
static void test_commands_wink_for_1(void)
1619
{
17-
const char *expected[] = { "wink" };
20+
const char *expected[] = { "wink", NULL, NULL, NULL };
1821
const char **actual = commands(1);
19-
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
22+
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE);
2023
free(actual);
2124
}
2225

2326
static void test_commands_double_blink_for_10(void)
2427
{
2528
TEST_IGNORE(); // delete this line to run test
26-
const char *expected[] = { "double blink" };
29+
const char *expected[] = { "double blink", NULL, NULL, NULL };
2730
const char **actual = commands(2);
28-
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
31+
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE);
2932
free(actual);
3033
}
3134

3235
static void test_commands_close_your_eyes_for_100(void)
3336
{
3437
TEST_IGNORE();
35-
const char *expected[] = { "close your eyes" };
38+
const char *expected[] = { "close your eyes", NULL, NULL, NULL };
3639
const char **actual = commands(4);
37-
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
40+
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE);
3841
free(actual);
3942
}
4043

4144
static void test_commands_jump_for_1000(void)
4245
{
4346
TEST_IGNORE();
44-
const char *expected[] = { "jump" };
47+
const char *expected[] = { "jump", NULL, NULL, NULL };
4548
const char **actual = commands(8);
46-
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
49+
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE);
4750
free(actual);
4851
}
4952

5053
static void test_commands_combine_two_actions(void)
5154
{
5255
TEST_IGNORE();
53-
const char *expected[] = { "wink", "double blink" };
56+
const char *expected[] = { "wink", "double blink", NULL, NULL };
5457
const char **actual = commands(3);
55-
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
58+
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE);
5659
free(actual);
5760
}
5861

5962
static void test_commands_reverse_two_actions(void)
6063
{
6164
TEST_IGNORE();
62-
const char *expected[] = { "double blink", "wink" };
65+
const char *expected[] = { "double blink", "wink", NULL, NULL };
6366
const char **actual = commands(19);
64-
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
67+
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE);
6568
free(actual);
6669
}
6770

6871
static void test_commands_reversing_one_action_gives_the_same_action(void)
6972
{
7073
TEST_IGNORE();
71-
const char *expected[] = { "jump" };
74+
const char *expected[] = { "jump", NULL, NULL, NULL };
7275
const char **actual = commands(24);
73-
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
76+
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE);
7477
free(actual);
7578
}
7679

7780
static void test_commands_reversing_no_actions_still_gives_no_actions(void)
7881
{
7982
TEST_IGNORE();
80-
const char *expected[] = { NULL };
83+
const char *expected[] = { NULL, NULL, NULL, NULL };
8184
const char **actual = commands(16);
82-
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
85+
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE);
8386
free(actual);
8487
}
8588

@@ -89,7 +92,7 @@ static void test_commands_all_possible_actions(void)
8992
const char *expected[] = { "wink", "double blink", "close your eyes",
9093
"jump" };
9194
const char **actual = commands(15);
92-
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
95+
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE);
9396
free(actual);
9497
}
9598

@@ -99,16 +102,16 @@ static void test_commands_reverse_all_possible_actions(void)
99102
const char *expected[] = { "jump", "close your eyes", "double blink",
100103
"wink" };
101104
const char **actual = commands(31);
102-
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
105+
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE);
103106
free(actual);
104107
}
105108

106109
static void test_commands_do_nothing_for_zero(void)
107110
{
108111
TEST_IGNORE();
109-
const char *expected[] = { NULL };
112+
const char *expected[] = { NULL, NULL, NULL, NULL };
110113
const char **actual = commands(0);
111-
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE(expected));
114+
TEST_ASSERT_EQUAL_STRING_ARRAY(expected, actual, ARRAY_SIZE);
112115
free(actual);
113116
}
114117

0 commit comments

Comments
 (0)