The Secret Handshaking exercise in C does not check the number of actions

I have just solved the Secret Handling exercise in the C track. The solution function is declared in the form of:

const char** commands(size_t number);

The value returned by the function is supposed to be an array of actions (character strings) of the size depending on number.

I see a couple of problems in this exercise.

  1. Despite that the size of the returned array can be any number between zero and four, the commands function does not pass back the array size. This makes it nearly impossible for the tests to check the array size.
  2. The tests in test_secret_handshake.c actually assumes that the returned array has the correct size, unless no action is expected to be returned (in such case, the tests require that the first element in the array be NULL).

For example, the first test in test_secret_handshake.c expects [“wink”] to be returned from the solution. It, however, gives a pass to any among [“wink”], [“wink”, NULL, NULL, NULL], [“wink”, “double wink”, “close your eyes”, “jump”], [“wink”, “jump”, “jump”, “jump”], etc. I think that the tests should be a bit more tighter.

A possible fix is to change the solution function to:

const char** commands(size_t number, size_t* size);

and let the solution set *size to the size of the array. But this makes all exiting solutions invalid.

Another possible fix is to assume that the size of the returned array is always four, and require that the remaining slots must be filled with NULL. In the first test case, the correct solution would be ["wink", NULL, NULL, NULL]. This is at least consistent with what the current tests do. Also, all existing solutions I have randomly picked seem to be able to survive through this change.

1 Like

I found that “example.c” in the repository of this “Secret Handshaking” exercise fails with the segfault error if the tests are tigntened up in the way I described earlier, because it sometimes allocates less than four slots to the array storing the answer. Given that C does not give us a way to check the size of an dynamically allocated array, I have given up.

This thread is outside of the C subforum so the maintainers may not have seen this.

CC @ahans @ryanplusplus

I like the second solution you proposed. Alternately, we could expect at the array to always have a terminating NULL (and not necessarily have size 4). Are you interested in contributing this change to the exercise @Borderite?

1 Like

@BNAndras Thanks!

@ryanplusplus I like you idea. But a typical solution allocates an array of size 4 by using calloc and put actions (char*) in it. Adding NULL at the end in every answer invalidates the typical solution, when there are four actions (because we need the fifth slot for NULL).

I think that I will revise the test code along the line of my second solution. In this way, future students will clearly see what are expected. I will revised example.c, too, so that it will pass the revised tests. Does this sound OK?

Yep, that sounds good. Thanks!

2 Likes

I have just submitted a PR for the work I promised. Please let me know if you see any problems.

1 Like