Exercise 1 for CS140

The objective of this exercise is to let you practice and get familiar with the concepts of C pointers and arrays and a simple C unit test API. You should complete this exercise and test in a CSIL machine.

While the number of lines needed for these 5 functions are short, you need to read all C files to understand the requirement, sample code, and the minunit test style. We will use such a test style in other parallel programming assignments. Make sure your implementation covers edge cases such as handling invalid input arguments. Common invalid arguments to consider include NULL pointers, negative array indexes, or non-positive array sizes and they may cause illegal access of memory or meaningless results.

Files minunit.h and minunit.c define macro mu_assert and two functions mu_run_test and mu_print_summary as a very simple API for C unit tests. Their usage is illustated in file exercise_test.c for testing functions set_key_action and mat_vect_mult implemented in exercise.c.

Submission Instructions

Files to submit: exercse.c and exercise_test.c. Please do not submit any additional files.

You submit the above two files through gradescope.com under Assignment "Exercise 1" (available by Jan 10). You will be invited to join Piazza around Jan 8 Thursday. We will announce the GradeScope join code in Piazza before Jan 10


Helpful note

Here is an example of improper design in using minunit for testing set_key_action() in exercise.c

Proper use:
char * set_key_action_test(void){
  char *key="del1";
  int ret=set_key_action(NULL, key, del1);
  mu_assert("Error in set_key_action with NULL value", ret == FAIL);
  return NULL;
}

Notice that the above test includes one case. You may add another test for another case. The released sample includes the two cases in one test.

Improper design:
char * set_key_action_test(void){
  char *key="del1";
  int ret=set_key_action(NULL, key, del1);
  if(ret==FAIL) {
    printf(“Something is wrong\n”);
    return NULL;
  }
  mu_assert("Error in set_key_action with NULL value", ret == FAIL);
  return NULL;
}

Notice that even your own check finds there is an error, the minunit package (mu_run_test() and mu_print_summay()) does not catch this error and the summary printed would be wrong. Your unit test code should let the minunit package catch the error using mu_assert, count errors and report properly.