From d538c99d1610fa5d18e0ae5a25b66210ced4d05b Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 1 Feb 2014 21:58:23 -0800 Subject: [PATCH 01/68] Added an initial typedef for the method callback. --- src/accel.h | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/accel.h b/src/accel.h index e968fb1..0d43073 100644 --- a/src/accel.h +++ b/src/accel.h @@ -22,6 +22,45 @@ typedef struct { struct internalAccelState *state; } accel_state; +/** + * Callback called whenever a given gesture drops below the affinity/length + * threshold specified when the state is initialized. + * + * A simple accel_callback is as follows: + * + * const int my_callback(accel_state *state, int gesture_id, int affinity_found, bool *reset_gesture) { + * int retval = ACCEL_SUCCESS; + * if (gesture_id == 1) { + * *reset_gesture = true; + * ... + * } else { + * logger->info("unrecognized gesture %i ", gesture_id); + * retval = ACCEL_MIN_RESERVED - 1; + * } + * return retval; + * } + * + * For the callback method, the documentation is as follows: + * @param state A non-NULL pointer to a state variable that holds + * recording metadata. + * @param gesture_id The identifier of the gesture that has been + * triggered. + * @param affinity_found The affinity of the triggered gesture_id to the + * recorded gesture. + * @param reset_gesture Setting reset_gesture to be true will result in the + * gesture being reset after the callback is triggered, + * and setting it to false will prevent the gesture + * from being reset. No default value is promised. + * @return int Returns ACCEL_SUCCESS if successful. Values that are + * not ACCEL_SUCCESS will cause the calling method to + * immediately abort and proxy-return the value + * returned by the callback. + * Implementers wishing to return a custom value should + * refer to the ACCEL_MIN_RESERVED definition inside + * their implementations. + */ +typedef const int (*accel_callback)(accel_state *state, int gesture_id, int affinity_found, bool *reset_gesture); + /** * Creates a state object, essentially a constructor. * @param state Pointer-to-pointer of the state being generated, populated From 8dd6673256a37342beaf50ca9ec67de78a95d0e3 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 1 Feb 2014 22:08:07 -0800 Subject: [PATCH 02/68] affinity -> offset. --- src/accel.h | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/accel.h b/src/accel.h index 0d43073..19c7fcf 100644 --- a/src/accel.h +++ b/src/accel.h @@ -23,12 +23,12 @@ typedef struct { } accel_state; /** - * Callback called whenever a given gesture drops below the affinity/length + * Callback called whenever a given gesture drops below the offset/length * threshold specified when the state is initialized. * * A simple accel_callback is as follows: * - * const int my_callback(accel_state *state, int gesture_id, int affinity_found, bool *reset_gesture) { + * const int my_callback(accel_state *state, int gesture_id, int offset_found, bool *reset_gesture) { * int retval = ACCEL_SUCCESS; * if (gesture_id == 1) { * *reset_gesture = true; @@ -45,7 +45,7 @@ typedef struct { * recording metadata. * @param gesture_id The identifier of the gesture that has been * triggered. - * @param affinity_found The affinity of the triggered gesture_id to the + * @param offset_found The offset of the triggered gesture_id to the * recorded gesture. * @param reset_gesture Setting reset_gesture to be true will result in the * gesture being reset after the callback is triggered, @@ -59,12 +59,12 @@ typedef struct { * refer to the ACCEL_MIN_RESERVED definition inside * their implementations. */ -typedef const int (*accel_callback)(accel_state *state, int gesture_id, int affinity_found, bool *reset_gesture); +typedef const int (*accel_callback)(accel_state *state, int gesture_id, int offset_found, bool *reset_gesture); /** * Creates a state object, essentially a constructor. - * @param state Pointer-to-pointer of the state being generated, populated - * by the method. + * @param state Pointer-to-pointer of the state being generated, + * populated by the method. * The current value of the pointer's pointed (*state) must * be NULL. * @param dimensions The number of dimensions of input that the state @@ -125,4 +125,14 @@ int accel_process_timer_tick(accel_state *state, int *accel_data); */ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *distance); +/** + * For a given state and recorded gesture, resets the gesture's offset state + * entirely. + * @param state A pointer to a non-NULL state variable that holds recording + * metadata. + * @param gesture_id Value that corresponds to a gesture currently being reset. + * @return ACCEL_SUCCESS if successful, an error code otherwise. + */ +int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); + #endif From f620f4f02cfad4e6929a71bf967943a808fb8f6d Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 1 Feb 2014 22:09:35 -0800 Subject: [PATCH 03/68] Update the accel.c file with this. --- src/accel.c | 61 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/src/accel.c b/src/accel.c index 78bb0cc..ebb5f84 100644 --- a/src/accel.c +++ b/src/accel.c @@ -33,7 +33,7 @@ typedef struct { int **normalized_recording; moving_avg_values **moving_avg_values; - int *affinities; + int *offsets; } accel_gesture; typedef struct internalAccelState { @@ -59,7 +59,7 @@ typedef struct internalAccelState { if (state_$->state->gestures != NULL && state_$->state->num_gestures_saved == 0) { return ACCEL_INTERNAL_ERROR; } // Decay rate of values we choose to keep. 1.0 is no decay, 2.0 is a doubling every time we keep them. -// TODO: should we store the affinities as floats instead? +// TODO: should we store the offsets as floats instead? #define ALPHA 1.0 // TODO: include these from a header file? @@ -94,9 +94,9 @@ void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { free(gest->normalized_recording); gest->normalized_recording = NULL; } - if (gest->affinities != NULL) { - free(gest->affinities); - gest->affinities = NULL; + if (gest->offsets != NULL) { + free(gest->offsets); + gest->offsets = NULL; } free(*gesture); @@ -258,6 +258,17 @@ int normalize(int sum) { return (int) cbrt(sum); } +int reset_gesture(accel_gesture *gest, const int dimensions) { + PRECONDITION_NOT_NULL(gest); + for (int i=0; irecording_size; ++i) { + gest->offsets[i] = INT16_MAX; + } + for (int d=0; dmoving_avg_values[d]); + } + return ACCEL_SUCCESS; +} + // TODO: does this work for zero recorded timestamps? int accel_end_record_gesture(accel_state *state, int gesture_id) { PRECONDITION_VALID_STATE(state); @@ -284,13 +295,19 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { return ACCEL_PARAM_ERROR; } - gesture->affinities = (int *) malloc(gesture->recording_size * sizeof(int)); - if (gesture->affinities == NULL) { + gesture->offsets = (int *) malloc(gesture->recording_size * sizeof(int)); + if (gesture->offsets == NULL) { return ACCEL_MALLOC_ERROR; } - gesture->is_recording = false; - gesture->is_recorded = true; + int reset_result = reset_gesture(gesture, state->dimensions); + if (reset_result != ACCEL_SUCCESS) { + free(gesture->offsets); + gesture->offsets = NULL; + } else { + gesture->is_recording = false; + gesture->is_recorded = true; + } for (int i=0; irecording_size; ++i) { gesture->affinities[i] = INT16_MAX; @@ -330,7 +347,7 @@ int handle_evaluation_tick(accel_gesture *gesture, int dimensions) { PRECONDITION_NOT_NULL(gesture); if (gesture->moving_avg_values == NULL || - gesture->affinities == NULL) { + gesture->offsets == NULL) { return ACCEL_INTERNAL_ERROR; } @@ -353,9 +370,9 @@ int handle_evaluation_tick(accel_gesture *gesture, int dimensions) { } } if (i == 0) { - gesture->affinities[i] = cost; + gesture->offsets[i] = cost; } else { - gesture->affinities[i] = MIN(ALPHA * gesture->affinities[i], cost+gesture->affinities[i-1]); + gesture->offsets[i] = MIN(ALPHA * gesture->offsets[i], cost+gesture->offsets[i-1]); } } for (i=1; irecording_size; ++i) { @@ -372,7 +389,7 @@ int handle_evaluation_tick(accel_gesture *gesture, int dimensions) { cost += input_i_d - recording_i_d; } } - gesture->affinities[i] = MIN(gesture->affinities[i], gesture->affinities[i-1] + cost); + gesture->offsets[i] = MIN(gesture->offsets[i], gesture->offsets[i-1] + cost); } return ACCEL_SUCCESS; } @@ -425,13 +442,13 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { return retcode; } -int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *affinity) { +int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *offset) { PRECONDITION_VALID_STATE(state); PRECONDITION_NOT_NULL(gesture_id); - PRECONDITION_NOT_NULL(affinity); + PRECONDITION_NOT_NULL(offset); *gesture_id = ACCEL_NO_VALID_GESTURE; - *affinity = ACCEL_NO_VALID_GESTURE; + *offset = ACCEL_NO_VALID_GESTURE; if (state->state->num_gestures_saved < 0) { return ACCEL_INTERNAL_ERROR; @@ -453,8 +470,8 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *aff return ACCEL_INTERNAL_ERROR; } - if ((*gesture_id == ACCEL_NO_VALID_GESTURE || *affinity == ACCEL_NO_VALID_GESTURE) && - *gesture_id != *affinity) { + if ((*gesture_id == ACCEL_NO_VALID_GESTURE || *offset == ACCEL_NO_VALID_GESTURE) && + *gesture_id != *offset) { return ACCEL_INTERNAL_ERROR; } @@ -468,14 +485,14 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *aff continue; } - if (*affinity == ACCEL_NO_VALID_GESTURE || - gesture->affinities[gesture->recording_size-1] < *affinity) { - *affinity = gesture->affinities[gesture->recording_size-1]; + if (*offset == ACCEL_NO_VALID_GESTURE || + gesture->offsets[gesture->recording_size-1] < *offset) { + *offset = gesture->offsets[gesture->recording_size-1]; *gesture_id = i; } } if (*gesture_id == ACCEL_NO_VALID_GESTURE || - *affinity == ACCEL_NO_VALID_GESTURE) { + *offset == ACCEL_NO_VALID_GESTURE) { return ACCEL_NO_VALID_GESTURE; } return ACCEL_SUCCESS; From bd5d0f925eaeabcb87a3e61eda2884df45d5daaa Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 1 Feb 2014 22:13:00 -0800 Subject: [PATCH 04/68] Synchronized the sample code --- sample/simple-accelerometer/src/accel.c | 86 ++++++++++++++++--------- sample/simple-accelerometer/src/accel.h | 55 ++++++++++++++-- 2 files changed, 108 insertions(+), 33 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index 78bb0cc..d592e22 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -33,7 +33,7 @@ typedef struct { int **normalized_recording; moving_avg_values **moving_avg_values; - int *affinities; + int *offsets; } accel_gesture; typedef struct internalAccelState { @@ -59,7 +59,7 @@ typedef struct internalAccelState { if (state_$->state->gestures != NULL && state_$->state->num_gestures_saved == 0) { return ACCEL_INTERNAL_ERROR; } // Decay rate of values we choose to keep. 1.0 is no decay, 2.0 is a doubling every time we keep them. -// TODO: should we store the affinities as floats instead? +// TODO: should we store the offsets as floats instead? #define ALPHA 1.0 // TODO: include these from a header file? @@ -94,9 +94,9 @@ void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { free(gest->normalized_recording); gest->normalized_recording = NULL; } - if (gest->affinities != NULL) { - free(gest->affinities); - gest->affinities = NULL; + if (gest->offsets != NULL) { + free(gest->offsets); + gest->offsets = NULL; } free(*gesture); @@ -258,6 +258,17 @@ int normalize(int sum) { return (int) cbrt(sum); } +int reset_gesture(accel_gesture *gest, const int dimensions) { + PRECONDITION_NOT_NULL(gest); + for (int i=0; irecording_size; ++i) { + gest->offsets[i] = INT16_MAX; + } + for (int d=0; dmoving_avg_values[d]); + } + return ACCEL_SUCCESS; +} + // TODO: does this work for zero recorded timestamps? int accel_end_record_gesture(accel_state *state, int gesture_id) { PRECONDITION_VALID_STATE(state); @@ -284,21 +295,21 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { return ACCEL_PARAM_ERROR; } - gesture->affinities = (int *) malloc(gesture->recording_size * sizeof(int)); - if (gesture->affinities == NULL) { + gesture->offsets = (int *) malloc(gesture->recording_size * sizeof(int)); + if (gesture->offsets == NULL) { return ACCEL_MALLOC_ERROR; } - gesture->is_recording = false; - gesture->is_recorded = true; - - for (int i=0; irecording_size; ++i) { - gesture->affinities[i] = INT16_MAX; - } - for (int d=0; ddimensions; ++d) { - reset_moving_avg(gesture->moving_avg_values[d]); + int reset_result = reset_gesture(gesture, state->dimensions); + if (reset_result != ACCEL_SUCCESS) { + free(gesture->offsets); + gesture->offsets = NULL; + } else { + gesture->is_recording = false; + gesture->is_recorded = true; } - return ACCEL_SUCCESS; + + return reset_result; } // TODO: check for malloc failure in this function. @@ -330,7 +341,7 @@ int handle_evaluation_tick(accel_gesture *gesture, int dimensions) { PRECONDITION_NOT_NULL(gesture); if (gesture->moving_avg_values == NULL || - gesture->affinities == NULL) { + gesture->offsets == NULL) { return ACCEL_INTERNAL_ERROR; } @@ -353,9 +364,9 @@ int handle_evaluation_tick(accel_gesture *gesture, int dimensions) { } } if (i == 0) { - gesture->affinities[i] = cost; + gesture->offsets[i] = cost; } else { - gesture->affinities[i] = MIN(ALPHA * gesture->affinities[i], cost+gesture->affinities[i-1]); + gesture->offsets[i] = MIN(ALPHA * gesture->offsets[i], cost+gesture->offsets[i-1]); } } for (i=1; irecording_size; ++i) { @@ -372,7 +383,7 @@ int handle_evaluation_tick(accel_gesture *gesture, int dimensions) { cost += input_i_d - recording_i_d; } } - gesture->affinities[i] = MIN(gesture->affinities[i], gesture->affinities[i-1] + cost); + gesture->offsets[i] = MIN(gesture->offsets[i], gesture->offsets[i-1] + cost); } return ACCEL_SUCCESS; } @@ -425,13 +436,13 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { return retcode; } -int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *affinity) { +int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *offset) { PRECONDITION_VALID_STATE(state); PRECONDITION_NOT_NULL(gesture_id); - PRECONDITION_NOT_NULL(affinity); + PRECONDITION_NOT_NULL(offset); *gesture_id = ACCEL_NO_VALID_GESTURE; - *affinity = ACCEL_NO_VALID_GESTURE; + *offset = ACCEL_NO_VALID_GESTURE; if (state->state->num_gestures_saved < 0) { return ACCEL_INTERNAL_ERROR; @@ -453,8 +464,8 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *aff return ACCEL_INTERNAL_ERROR; } - if ((*gesture_id == ACCEL_NO_VALID_GESTURE || *affinity == ACCEL_NO_VALID_GESTURE) && - *gesture_id != *affinity) { + if ((*gesture_id == ACCEL_NO_VALID_GESTURE || *offset == ACCEL_NO_VALID_GESTURE) && + *gesture_id != *offset) { return ACCEL_INTERNAL_ERROR; } @@ -468,15 +479,32 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *aff continue; } - if (*affinity == ACCEL_NO_VALID_GESTURE || - gesture->affinities[gesture->recording_size-1] < *affinity) { - *affinity = gesture->affinities[gesture->recording_size-1]; + if (*offset == ACCEL_NO_VALID_GESTURE || + gesture->offsets[gesture->recording_size-1] < *offset) { + *offset = gesture->offsets[gesture->recording_size-1]; *gesture_id = i; } } if (*gesture_id == ACCEL_NO_VALID_GESTURE || - *affinity == ACCEL_NO_VALID_GESTURE) { + *offset == ACCEL_NO_VALID_GESTURE) { return ACCEL_NO_VALID_GESTURE; } return ACCEL_SUCCESS; } + +int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id) { + PRECONDITION_VALID_STATE(state); + internal_accel_state *ias = state->state; + if (ias->num_gestures_saved <= gesture_id || gesture_id < 0) { + return ACCEL_PARAM_ERROR; + } + accel_gesture *gest = ias->gestures[gesture_id]; + if (gest == NULL) { + return ACCEL_INTERNAL_ERROR; + } + if (!gest->is_recorded || gest->is_recording) { + // Gesture is in the wrong state for resetting. + return ACCEL_PARAM_ERROR; + } + return reset_gesture(gest, state->dimensions); +} diff --git a/sample/simple-accelerometer/src/accel.h b/sample/simple-accelerometer/src/accel.h index c601b21..19c7fcf 100644 --- a/sample/simple-accelerometer/src/accel.h +++ b/sample/simple-accelerometer/src/accel.h @@ -3,8 +3,6 @@ #include -#include "accel_consts.c" - #define ACCEL_SUCCESS 0 #define ACCEL_PARAM_ERROR -1 #define ACCEL_INTERNAL_ERROR -2 @@ -24,10 +22,49 @@ typedef struct { struct internalAccelState *state; } accel_state; +/** + * Callback called whenever a given gesture drops below the offset/length + * threshold specified when the state is initialized. + * + * A simple accel_callback is as follows: + * + * const int my_callback(accel_state *state, int gesture_id, int offset_found, bool *reset_gesture) { + * int retval = ACCEL_SUCCESS; + * if (gesture_id == 1) { + * *reset_gesture = true; + * ... + * } else { + * logger->info("unrecognized gesture %i ", gesture_id); + * retval = ACCEL_MIN_RESERVED - 1; + * } + * return retval; + * } + * + * For the callback method, the documentation is as follows: + * @param state A non-NULL pointer to a state variable that holds + * recording metadata. + * @param gesture_id The identifier of the gesture that has been + * triggered. + * @param offset_found The offset of the triggered gesture_id to the + * recorded gesture. + * @param reset_gesture Setting reset_gesture to be true will result in the + * gesture being reset after the callback is triggered, + * and setting it to false will prevent the gesture + * from being reset. No default value is promised. + * @return int Returns ACCEL_SUCCESS if successful. Values that are + * not ACCEL_SUCCESS will cause the calling method to + * immediately abort and proxy-return the value + * returned by the callback. + * Implementers wishing to return a custom value should + * refer to the ACCEL_MIN_RESERVED definition inside + * their implementations. + */ +typedef const int (*accel_callback)(accel_state *state, int gesture_id, int offset_found, bool *reset_gesture); + /** * Creates a state object, essentially a constructor. - * @param state Pointer-to-pointer of the state being generated, populated - * by the method. + * @param state Pointer-to-pointer of the state being generated, + * populated by the method. * The current value of the pointer's pointed (*state) must * be NULL. * @param dimensions The number of dimensions of input that the state @@ -88,4 +125,14 @@ int accel_process_timer_tick(accel_state *state, int *accel_data); */ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *distance); +/** + * For a given state and recorded gesture, resets the gesture's offset state + * entirely. + * @param state A pointer to a non-NULL state variable that holds recording + * metadata. + * @param gesture_id Value that corresponds to a gesture currently being reset. + * @return ACCEL_SUCCESS if successful, an error code otherwise. + */ +int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); + #endif From 072f9ab4b8d07e02a3d84ac1910f86ffcd187c19 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 1 Feb 2014 22:23:33 -0800 Subject: [PATCH 05/68] Notes, and parameters. --- src/accel.c | 6 +++++- src/accel.h | 7 ++++++- test/accel_test.cc | 18 +++++++++--------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/accel.c b/src/accel.c index ebb5f84..2d8a44c 100644 --- a/src/accel.c +++ b/src/accel.c @@ -143,7 +143,11 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { } // TODO: needs direct testing with invalid objects. -int accel_generate_state(accel_state **state, int dimensions, int window_size) { +int accel_generate_state(accel_state **state, + int dimensions, + int window_size, + accel_callback callback, + const int threshold) { PRECONDITION_NOT_NULL(state); // TODO: write a test for this value. diff --git a/src/accel.h b/src/accel.h index 19c7fcf..5833641 100644 --- a/src/accel.h +++ b/src/accel.h @@ -71,9 +71,14 @@ typedef const int (*accel_callback)(accel_state *state, int gesture_id, int offs * represents. * @param window_size The size of the moving windows used to calculate smoothed * sensor readings. + * @param callback A callback that is triggered whenever a gesture passes a + * threshold. See the ``accel_callback`` typedef for more + * information. + * @param threshold The minimum threshold offset (divided by length) that all + * gestures must be before the callback is called. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_generate_state(accel_state **state, int dimensions, int window_size); +int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback, const int threshold); /** * Destroys the state object at the pointer pointed to by the state pointer. diff --git a/test/accel_test.cc b/test/accel_test.cc index 52fde91..b4fdb08 100644 --- a/test/accel_test.cc +++ b/test/accel_test.cc @@ -6,7 +6,7 @@ const void * void_null = NULL; accel_state *test_fabricate_state(int dimensions) { accel_state *state = NULL; - int result = accel_generate_state(&state, dimensions, 1); + int result = accel_generate_state(&state, dimensions, 1, NULL, 0); EXPECT_EQ(0, result); EXPECT_NE(void_null, state); return state; @@ -28,23 +28,23 @@ void test_burn_state(accel_state ** state) { TEST(AccelFuzzTest, generate_state_null_state) { - int result = accel_generate_state(NULL, 3, 1); + int result = accel_generate_state(NULL, 3, 1, NULL, 0); EXPECT_EQ(result, ACCEL_PARAM_ERROR); } TEST(AccelFuzzTest, generate_state_negative_or_zero_dimensions) { accel_state *state = NULL; // 0 dimensions must fail - int result = accel_generate_state(&state, 0, 1); + int result = accel_generate_state(&state, 0, 1, NULL, 0); EXPECT_EQ(ACCEL_PARAM_ERROR, result); // -1 dimension must fail - result = accel_generate_state(&state, -1, 1); + result = accel_generate_state(&state, -1, 1, NULL, 0); EXPECT_EQ(ACCEL_PARAM_ERROR, result); // 1 dimension must succeed. state = NULL; - result = accel_generate_state(&state, 1, 1); + result = accel_generate_state(&state, 1, 1, NULL, 0); EXPECT_EQ(0, result); // TODO: result's memory is leaked :s } @@ -54,15 +54,15 @@ TEST(AccelFuzzTest, generate_state_invalid_window_size) { int result = 0; // Size 0 must fail - result = accel_generate_state(&state, 1, 0); + result = accel_generate_state(&state, 1, 0, NULL, 0); EXPECT_EQ(ACCEL_PARAM_ERROR, result); // Size -1 must fail - result = accel_generate_state(&state, 1, -1); + result = accel_generate_state(&state, 1, -1, NULL, 0); EXPECT_EQ(ACCEL_PARAM_ERROR, result); // Size 1 must succeed - result = accel_generate_state(&state, 1, 1); + result = accel_generate_state(&state, 1, 1, NULL, 0); EXPECT_EQ(0, result); EXPECT_NE(void_null, state); } @@ -186,7 +186,7 @@ TEST(AccelTest, accel_generate_and_destroy) { accel_state *state = NULL; for (int i=1; i<10; ++i) { EXPECT_EQ(void_null, state) << "i = " << i; - EXPECT_EQ(0, accel_generate_state(&state, 2*i, i)) << "i = " << i; + EXPECT_EQ(0, accel_generate_state(&state, 2*i, i, NULL, 0)) << "i = " << i; EXPECT_EQ(0, accel_destroy_state(&state)) << "i = " << i; EXPECT_EQ(void_null, state) << "i = " << i; } From df2cd2f8cea47ccbfd8232585aae2a7e9dc1fed3 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sun, 16 Feb 2014 13:06:04 -0800 Subject: [PATCH 06/68] Added a threshold variable. --- src/accel.c | 5 +++++ test/accel_test.cc | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/src/accel.c b/src/accel.c index 2d8a44c..73479cc 100644 --- a/src/accel.c +++ b/src/accel.c @@ -38,6 +38,7 @@ typedef struct { typedef struct internalAccelState { int window_size; + int threshold; int num_gestures_saved; accel_gesture **gestures; @@ -159,6 +160,9 @@ int accel_generate_state(accel_state **state, if (window_size <= 0) { return ACCEL_PARAM_ERROR; } + if (threshold <= 0 && callback != NULL) { + return ACCEL_PARAM_ERROR; + } size_t state_size = sizeof(accel_state); size_t internal_state_size = sizeof(internal_accel_state); @@ -185,6 +189,7 @@ int accel_generate_state(accel_state **state, (*state)->dimensions = dimensions; (*state)->state->window_size = window_size > 0 ? window_size : 2; + (*state)->state->threshold = threshold; return ACCEL_SUCCESS; } diff --git a/test/accel_test.cc b/test/accel_test.cc index b4fdb08..ea58699 100644 --- a/test/accel_test.cc +++ b/test/accel_test.cc @@ -49,6 +49,23 @@ TEST(AccelFuzzTest, generate_state_negative_or_zero_dimensions) { // TODO: result's memory is leaked :s } +TEST(AccelFuzzTest, generate_state_invalid_threshold_with_callback_params) { + accel_state *state = NULL; + accel_callback nonNullCallback = (accel_callback) 1; + + // Fails for negatives. + int result = accel_generate_state(&state, 1, 1, nonNullCallback, -1); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // Fails for zero. + result = accel_generate_state(&state, 1, 1, nonNullCallback, 0); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // Succeeds for 1. + result = accel_generate_state(&state, 1, 1, nonNullCallback, 1); + EXPECT_EQ(ACCEL_SUCCESS, result); +} + TEST(AccelFuzzTest, generate_state_invalid_window_size) { accel_state *state = NULL; int result = 0; From f30683b8831c04aba6e29ec6af0d05976ed5433c Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 7 Mar 2014 23:27:50 -0800 Subject: [PATCH 07/68] Added callback to the accel_state struct --- src/accel.c | 2 +- src/accel.h | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/accel.c b/src/accel.c index 73479cc..a08d9ab 100644 --- a/src/accel.c +++ b/src/accel.c @@ -186,7 +186,7 @@ int accel_generate_state(accel_state **state, (*state)->state = internal_state; - + (*state)->callback = callback; (*state)->dimensions = dimensions; (*state)->state->window_size = window_size > 0 ? window_size : 2; (*state)->state->threshold = threshold; diff --git a/src/accel.h b/src/accel.h index 5833641..3ee93e0 100644 --- a/src/accel.h +++ b/src/accel.h @@ -15,12 +15,7 @@ #define ACCEL_VERSION_CODE ACCEL_VERSION_GEN(1, 0, 0, true, false) struct internalAccelState; - -typedef struct { - int dimensions; - - struct internalAccelState *state; -} accel_state; +struct accelState; /** * Callback called whenever a given gesture drops below the offset/length @@ -59,7 +54,15 @@ typedef struct { * refer to the ACCEL_MIN_RESERVED definition inside * their implementations. */ -typedef const int (*accel_callback)(accel_state *state, int gesture_id, int offset_found, bool *reset_gesture); +typedef const int (*accel_callback)(accelState *state, int gesture_id, int offset_found, bool *reset_gesture); + +typedef struct accelState { + int dimensions; + + accel_callback callback; + struct internalAccelState *state; +} accel_state; + /** * Creates a state object, essentially a constructor. From 9b76246b7a87f3fcb0cee9a97c4e28011e25a399 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sun, 9 Mar 2014 17:42:50 -0700 Subject: [PATCH 08/68] Started writing tests that the callback is being called. --- test/accel_test.cc | 65 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/test/accel_test.cc b/test/accel_test.cc index ea58699..e082b58 100644 --- a/test/accel_test.cc +++ b/test/accel_test.cc @@ -2,16 +2,28 @@ #include "../src/accel.h" -const void * void_null = NULL; +const void * VOID_NULL = NULL; -accel_state *test_fabricate_state(int dimensions) { +accel_state *test_fabricate_state_with_callback(int dimensions, accel_callback callback, const int threshold) { accel_state *state = NULL; int result = accel_generate_state(&state, dimensions, 1, NULL, 0); - EXPECT_EQ(0, result); - EXPECT_NE(void_null, state); + EXPECT_EQ(ACCEL_SUCCESS, result); + EXPECT_NE(VOID_NULL, state); return state; } +accel_state *test_fabricate_1d_state_with_callback(accel_callback callback, const int threshold) { + return test_fabricate_state_with_callback(1, callback, threshold); +} + +accel_state *test_fabricate_3d_state_with_callback(accel_callback callback, const int threshold) { + return test_fabricate_state_with_callback(3, callback, threshold); +} + +accel_state *test_fabricate_state(int dimensions) { + return test_fabricate_state_with_callback(dimensions, NULL, 0); +} + accel_state *test_fabricate_1d_state() { return test_fabricate_state(1); } @@ -23,7 +35,7 @@ accel_state *test_fabricate_3d_state() { void test_burn_state(accel_state ** state) { int result = accel_destroy_state(state); EXPECT_EQ(0, result); - EXPECT_EQ(void_null, *state); + EXPECT_EQ(VOID_NULL, *state); } @@ -81,7 +93,42 @@ TEST(AccelFuzzTest, generate_state_invalid_window_size) { // Size 1 must succeed result = accel_generate_state(&state, 1, 1, NULL, 0); EXPECT_EQ(0, result); - EXPECT_NE(void_null, state); + EXPECT_NE(VOID_NULL, state); +} + +TEST(AccelFuzzTest, accel_generate_state_null_callback) { + int result = 0; + accel_state *state = NULL; + + // Null callback must be successful + result = accel_generate_state(&state, 1, 1, NULL, 0); +} + + +// TODO: find a better way to declare this function inline. Global state blows. +const int accel_generate_state_valid_callback_inline_function( + accel_state *state, + int gesture_id, + int offset_found, + bool *reset_gesture) { + // reset the gesture, and + std::cout << "offset found is " << offset_found << ", gesture_id was " << gesture_id << std::endl; + *reset_gesture = true; + return ACCEL_SUCCESS; +} + +TEST(AccelFuzzTest, accel_generate_state_valid_callback) { + int gesture_id = 0; + accel_state *state = NULL; + + // Non-null callback, watch it iterate over this stuff. + state = test_fabricate_1d_state(); + + EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); + + for (int i=0; i<100; ++i) { + EXPECT_EQ(ACCEL_SUCCESS, ) + } } TEST(AccelFuzzTest, accel_destroy_state_invalid_input) { @@ -102,7 +149,7 @@ TEST(AccelFuzzTest, accel_destroy_state_invalid_input) { // Destroy the state result = accel_destroy_state(&state); EXPECT_EQ(0, result); - EXPECT_EQ(void_null, state); + EXPECT_EQ(VOID_NULL, state); } TEST(AccelFuzzTest, accel_start_record_gesture_invalid_input) { @@ -202,10 +249,10 @@ TEST(AccelFuzzTest, accel_find_most_likely_gesture_invalid_input) { TEST(AccelTest, accel_generate_and_destroy) { accel_state *state = NULL; for (int i=1; i<10; ++i) { - EXPECT_EQ(void_null, state) << "i = " << i; + EXPECT_EQ(VOID_NULL, state) << "i = " << i; EXPECT_EQ(0, accel_generate_state(&state, 2*i, i, NULL, 0)) << "i = " << i; EXPECT_EQ(0, accel_destroy_state(&state)) << "i = " << i; - EXPECT_EQ(void_null, state) << "i = " << i; + EXPECT_EQ(VOID_NULL, state) << "i = " << i; } } From ed4adfd63d6bfabecedd96f866a48ef6143688b9 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Mon, 10 Mar 2014 00:24:52 -0700 Subject: [PATCH 09/68] Added Macros to allow me to shim anonymous classes into c. There's gotta be a better way to do this, but #yolo... --- test/testutil.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 test/testutil.h diff --git a/test/testutil.h b/test/testutil.h new file mode 100644 index 0000000..d2b356a --- /dev/null +++ b/test/testutil.h @@ -0,0 +1,24 @@ +#ifndef TEST_CALLBACK_UTIL_H +#define TEST_CALLBACK_UTIL_H + +// TODO: build a list of params +// TODO: build a list of returned values. + +#define TEST_CALLBACK_NAMESPACE(testClass, testName, uniqueIdentifier) \ + generated_##testClass##_##testName##_##uniqueIdentifier##_ + +#define TEST_CALLBACK_NAME(testClass, testName, uniqueIdentifier) \ + TEST_CALLBACK_NAMESPACE(testClass, testName, uniqueIdentifier) + +#define TEST_CALLBACK_COUNTER(testClass, testName, uniqueIdentifier) \ + generated_##testClass##_##testName##_##uniqueIdentifier##_counter + +#define TEST_CALLBACK(returnSignature, testClass, testName, uniqueIdentifier, args...) \ + /* count the number of times the callback was invoked. */ \ + uint TEST_CALLBACK_COUNTER(testClass, testName, uniqueIdentifier) = 0; \ + /* define a method that proxies calls to the real method. */ \ + returnSignature TEST_CALLBACK_NAME(testClass, testName, uniqueIdentifier) (args) { \ + /* increment the counter */ \ + TEST_CALLBACK_COUNTER(testClass, testName, uniqueIdentifier) += 1; + +#endif From 132c6521b01d852dd07c082e1fc1c74f13ac814e Mon Sep 17 00:00:00 2001 From: shalecraig Date: Mon, 10 Mar 2014 00:25:21 -0700 Subject: [PATCH 10/68] Added currently failing test to motivate myself to work. --- test/accel_test.cc | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/test/accel_test.cc b/test/accel_test.cc index e082b58..d1b4d5a 100644 --- a/test/accel_test.cc +++ b/test/accel_test.cc @@ -2,6 +2,8 @@ #include "../src/accel.h" +#include "testutil.h" + const void * VOID_NULL = NULL; accel_state *test_fabricate_state_with_callback(int dimensions, accel_callback callback, const int threshold) { @@ -105,13 +107,7 @@ TEST(AccelFuzzTest, accel_generate_state_null_callback) { } -// TODO: find a better way to declare this function inline. Global state blows. -const int accel_generate_state_valid_callback_inline_function( - accel_state *state, - int gesture_id, - int offset_found, - bool *reset_gesture) { - // reset the gesture, and +TEST_CALLBACK(const int, AccelFuzzTest, accel_generate_state_valid_callback, myTest, accel_state *state, int gesture_id, int offset_found, bool *reset_gesture) std::cout << "offset found is " << offset_found << ", gesture_id was " << gesture_id << std::endl; *reset_gesture = true; return ACCEL_SUCCESS; @@ -122,13 +118,24 @@ TEST(AccelFuzzTest, accel_generate_state_valid_callback) { accel_state *state = NULL; // Non-null callback, watch it iterate over this stuff. - state = test_fabricate_1d_state(); + state = test_fabricate_1d_state_with_callback( + // &TEST_CALLBACK_NAME(AccelFuzzTest, accel_generate_state_valid_callback, myTest), + NULL, + 10); EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); for (int i=0; i<100; ++i) { - EXPECT_EQ(ACCEL_SUCCESS, ) + int data[1] = {i}; + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + } + EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture_id)); + + for (int i=0; i<100; ++i) { + int data[1] = {i}; + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); } + EXPECT_GT(TEST_CALLBACK_COUNTER(AccelFuzzTest, accel_generate_state_valid_callback, myTest), 0); } TEST(AccelFuzzTest, accel_destroy_state_invalid_input) { From 67c087fec6f206ba2c33d243591431829399f41a Mon Sep 17 00:00:00 2001 From: shalecraig Date: Wed, 12 Mar 2014 23:16:57 -0700 Subject: [PATCH 11/68] Updated the testutil header to work. TODO: why didn't the prev one work? --- test/testutil.h | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/test/testutil.h b/test/testutil.h index d2b356a..2323ba7 100644 --- a/test/testutil.h +++ b/test/testutil.h @@ -1,24 +1,20 @@ #ifndef TEST_CALLBACK_UTIL_H #define TEST_CALLBACK_UTIL_H -// TODO: build a list of params -// TODO: build a list of returned values. - -#define TEST_CALLBACK_NAMESPACE(testClass, testName, uniqueIdentifier) \ - generated_##testClass##_##testName##_##uniqueIdentifier##_ +// TODO: this is so hack. #define TEST_CALLBACK_NAME(testClass, testName, uniqueIdentifier) \ - TEST_CALLBACK_NAMESPACE(testClass, testName, uniqueIdentifier) + generated_##testClass##_##testName##_##uniqueIdentifier##_name #define TEST_CALLBACK_COUNTER(testClass, testName, uniqueIdentifier) \ generated_##testClass##_##testName##_##uniqueIdentifier##_counter #define TEST_CALLBACK(returnSignature, testClass, testName, uniqueIdentifier, args...) \ /* count the number of times the callback was invoked. */ \ - uint TEST_CALLBACK_COUNTER(testClass, testName, uniqueIdentifier) = 0; \ + uint generated_##testClass##_##testName##_##uniqueIdentifier##_counter = 0; \ /* define a method that proxies calls to the real method. */ \ - returnSignature TEST_CALLBACK_NAME(testClass, testName, uniqueIdentifier) (args) { \ + returnSignature generated_##testClass##_##testName##_##uniqueIdentifier##_name (args) { \ /* increment the counter */ \ - TEST_CALLBACK_COUNTER(testClass, testName, uniqueIdentifier) += 1; + generated_##testClass##_##testName##_##uniqueIdentifier##_counter += 1; #endif From 750d15fec2da64aca23adeae79250ded73eae089 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Wed, 12 Mar 2014 23:18:34 -0700 Subject: [PATCH 12/68] Added basic tests for callback evaluation. --- src/accel.c | 26 ++++++++++++++++++++++---- test/accel_test.cc | 36 ++++++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/src/accel.c b/src/accel.c index a08d9ab..4d077b4 100644 --- a/src/accel.c +++ b/src/accel.c @@ -163,6 +163,9 @@ int accel_generate_state(accel_state **state, if (threshold <= 0 && callback != NULL) { return ACCEL_PARAM_ERROR; } + if (threshold > 0 && callback == NULL) { + return ACCEL_PARAM_ERROR; + } size_t state_size = sizeof(accel_state); size_t internal_state_size = sizeof(internal_accel_state); @@ -351,9 +354,10 @@ void handle_recording_tick(accel_gesture *gesture, int dimensions) { ++gesture->recording_size; } -int handle_evaluation_tick(accel_gesture *gesture, int dimensions) { +int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gesture_id) { // TODO: load the input at the beginning instead of gesture->recording_size times. PRECONDITION_NOT_NULL(gesture); + int dimensions = state->dimensions; if (gesture->moving_avg_values == NULL || gesture->offsets == NULL) { @@ -400,6 +404,20 @@ int handle_evaluation_tick(accel_gesture *gesture, int dimensions) { } gesture->offsets[i] = MIN(gesture->offsets[i], gesture->offsets[i-1] + cost); } + if (state->callback != NULL) { + if (state->state->threshold <= 0) { + return ACCEL_PARAM_ERROR; + } + float avg_affinity = gesture->offsets[gesture->recording_size-1] * 1.0 / gesture->recording_size; + if (avg_affinity < state->state->threshold) { + bool reset; + int retval = state->callback(state, gesture_id, avg_affinity, &reset); + if (reset == true) { + reset_gesture(gesture, dimensions); + } + return retval; + } + } return ACCEL_SUCCESS; } @@ -408,8 +426,8 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { PRECONDITION_NOT_NULL(accel_data); int retcode = ACCEL_SUCCESS; - for (int gesture_iter = 0; gesture_iter < state->state->num_gestures_saved; ++gesture_iter) { - accel_gesture *gesture = state->state->gestures[gesture_iter]; + for (int gesture_id = 0; gesture_id < state->state->num_gestures_saved; ++gesture_id) { + accel_gesture *gesture = state->state->gestures[gesture_id]; if (gesture == NULL) { retcode = ACCEL_INTERNAL_ERROR; continue; @@ -439,7 +457,7 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { if (gesture->is_recording) { handle_recording_tick(gesture, state->dimensions); } else if (gesture->is_recorded) { - returned = handle_evaluation_tick(gesture, state->dimensions); + returned = handle_evaluation_tick(state, gesture, gesture_id); if (returned != ACCEL_SUCCESS) { retcode = returned; } diff --git a/test/accel_test.cc b/test/accel_test.cc index d1b4d5a..dcfad2f 100644 --- a/test/accel_test.cc +++ b/test/accel_test.cc @@ -8,9 +8,17 @@ const void * VOID_NULL = NULL; accel_state *test_fabricate_state_with_callback(int dimensions, accel_callback callback, const int threshold) { accel_state *state = NULL; - int result = accel_generate_state(&state, dimensions, 1, NULL, 0); + int result = accel_generate_state(&state, dimensions, 1, callback, threshold); EXPECT_EQ(ACCEL_SUCCESS, result); + if (ACCEL_SUCCESS != result) { + int *myNull = NULL; + myNull = 0; + } EXPECT_NE(VOID_NULL, state); + if (VOID_NULL == state) { + int *myNull = NULL; + myNull = 0; + } return state; } @@ -98,6 +106,28 @@ TEST(AccelFuzzTest, generate_state_invalid_window_size) { EXPECT_NE(VOID_NULL, state); } +TEST(AccelFuzzTest, generate_state_threshold_fuzzing) { + accel_state *state = NULL; + + // Threshold of 1 with a non-null callback succeeds + EXPECT_EQ(NULL, state); + EXPECT_EQ(ACCEL_SUCCESS, accel_generate_state(&state, 1, 1, (accel_callback) 0x1, 1)); + accel_destroy_state(&state); + + // Threshold of 1 with a null callback fails + EXPECT_EQ(NULL, state); + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_generate_state(&state, 1, 1, (accel_callback) NULL, 1)); + + // Threshold of 0 with a null callback succeeds + EXPECT_EQ(NULL, state); + EXPECT_EQ(ACCEL_SUCCESS, accel_generate_state(&state, 1, 1, (accel_callback) NULL, 0)); + accel_destroy_state(&state); + + // Threshold of 0 with a non-null callback succeeds + EXPECT_EQ(NULL, state); + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_generate_state(&state, 1, 1, (accel_callback) 0x1, 0)); +} + TEST(AccelFuzzTest, accel_generate_state_null_callback) { int result = 0; accel_state *state = NULL; @@ -108,7 +138,6 @@ TEST(AccelFuzzTest, accel_generate_state_null_callback) { TEST_CALLBACK(const int, AccelFuzzTest, accel_generate_state_valid_callback, myTest, accel_state *state, int gesture_id, int offset_found, bool *reset_gesture) - std::cout << "offset found is " << offset_found << ", gesture_id was " << gesture_id << std::endl; *reset_gesture = true; return ACCEL_SUCCESS; } @@ -119,8 +148,7 @@ TEST(AccelFuzzTest, accel_generate_state_valid_callback) { // Non-null callback, watch it iterate over this stuff. state = test_fabricate_1d_state_with_callback( - // &TEST_CALLBACK_NAME(AccelFuzzTest, accel_generate_state_valid_callback, myTest), - NULL, + &TEST_CALLBACK_NAME(AccelFuzzTest, accel_generate_state_valid_callback, myTest), 10); EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); From 80dbd541cb3b8eb079e69f773f277672ff7e1e03 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Wed, 12 Mar 2014 23:20:03 -0700 Subject: [PATCH 13/68] Ignore dSYM file(s) --- test/.gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/.gitignore b/test/.gitignore index ddc2d6e..e5c940c 100644 --- a/test/.gitignore +++ b/test/.gitignore @@ -1,3 +1,5 @@ test_executable accel_test moving_avg_ticker_test + +*.dSYM From ab15ee71ea90fa781c5b2259ecfa148ab7f3b443 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Wed, 12 Mar 2014 23:20:35 -0700 Subject: [PATCH 14/68] Cleaned up some formatting in accel_test --- test/accel_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/accel_test.cc b/test/accel_test.cc index dcfad2f..499ecd1 100644 --- a/test/accel_test.cc +++ b/test/accel_test.cc @@ -400,7 +400,7 @@ TEST(AccelTest, end_to_end_test_multiple_recordings) { test_burn_state(&state); } -int main (int argc, char** argv) { +int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); int returnValue; From afc6514e89bb2411a351bad350f9d8478cb7eeea Mon Sep 17 00:00:00 2001 From: shalecraig Date: Thu, 13 Mar 2014 09:06:12 -0700 Subject: [PATCH 15/68] Fixing incorrect comment --- test/accel_test.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/accel_test.cc b/test/accel_test.cc index 499ecd1..ab1378f 100644 --- a/test/accel_test.cc +++ b/test/accel_test.cc @@ -123,7 +123,7 @@ TEST(AccelFuzzTest, generate_state_threshold_fuzzing) { EXPECT_EQ(ACCEL_SUCCESS, accel_generate_state(&state, 1, 1, (accel_callback) NULL, 0)); accel_destroy_state(&state); - // Threshold of 0 with a non-null callback succeeds + // Threshold of 0 with a non-null callback fails EXPECT_EQ(NULL, state); EXPECT_EQ(ACCEL_PARAM_ERROR, accel_generate_state(&state, 1, 1, (accel_callback) 0x1, 0)); } From 97f1ca099055a44fdb1dbf220ee956f85487d9cd Mon Sep 17 00:00:00 2001 From: shalecraig Date: Thu, 13 Mar 2014 22:43:18 -0700 Subject: [PATCH 16/68] Added clang-format file --- .clang-format | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..5dc68c0 --- /dev/null +++ b/.clang-format @@ -0,0 +1,52 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +ConstructorInitializerIndentWidth: 4 +AlignEscapedNewlinesLeft: false +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: true +AlwaysBreakTemplateDeclarations: false +AlwaysBreakBeforeMultilineStrings: false +BreakBeforeBinaryOperators: false +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BinPackParameters: true +ColumnLimit: 120 +ConstructorInitializerAllOnOneLineOrOnePerLine: false +DerivePointerBinding: false +ExperimentalAutoDetectBinPacking: false +IndentCaseLabels: false +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakString: 1000 +PenaltyBreakFirstLessLess: 120 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PointerBindsToType: false +SpacesBeforeTrailingComments: 1 +Cpp11BracedListStyle: true +Standard: Cpp11 +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +BreakBeforeBraces: Attach +IndentFunctionDeclarationAfterType: false +SpacesInParentheses: false +SpacesInAngles: false +SpaceInEmptyParentheses: false +SpacesInCStyleCastParentheses: false +SpacesInContainerLiterals: true +SpaceBeforeAssignmentOperators: true +ContinuationIndentWidth: 4 +CommentPragmas: '^ IWYU pragma:' +SpaceBeforeParens: ControlStatements +... + From 4c434de399ecf103fa7423cfba5ef157cacbedc4 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Thu, 13 Mar 2014 22:43:31 -0700 Subject: [PATCH 17/68] Formatted all code. WOOHOO! --- src/accel.c | 166 +++++++++++++++++++-------------- src/accel.h | 8 +- src/moving_avg_ticker.c | 26 ++++-- src/moving_avg_ticker.h | 1 - src/pebble_makeup.h | 8 +- test/accel_test.cc | 124 +++++++++++++++--------- test/moving_avg_ticker_test.cc | 18 ++-- test/testutil.h | 16 ++-- 8 files changed, 217 insertions(+), 150 deletions(-) diff --git a/src/accel.c b/src/accel.c index 4d077b4..8ef5d46 100644 --- a/src/accel.c +++ b/src/accel.c @@ -44,34 +44,56 @@ typedef struct internalAccelState { accel_gesture **gestures; } internal_accel_state; -#define PRECONDITION_NOT_NULL(foo_$) \ - if (foo_$ == NULL) { return ACCEL_PARAM_ERROR; } - -#define PRECONDITION_NULL(foo_$) \ - if (foo_$ != NULL) { return ACCEL_PARAM_ERROR; } - -#define PRECONDITION_VALID_STATE(state_$) \ - if (state_$ == NULL) { return ACCEL_PARAM_ERROR; } \ - if (state_$->state == NULL) { return ACCEL_INTERNAL_ERROR; } \ - if (state_$->dimensions <= 0) { return ACCEL_INTERNAL_ERROR; } \ - if (state_$->state->window_size <= 0) { return ACCEL_INTERNAL_ERROR; } \ - if (state_$->state->num_gestures_saved < 0) { return ACCEL_INTERNAL_ERROR; } \ - if (state_$->state->gestures == NULL && state_$->state->num_gestures_saved != 0) { return ACCEL_INTERNAL_ERROR; } \ - if (state_$->state->gestures != NULL && state_$->state->num_gestures_saved == 0) { return ACCEL_INTERNAL_ERROR; } +#define PRECONDITION_NOT_NULL(foo_$) \ + if (foo_$ == NULL) { \ + return ACCEL_PARAM_ERROR; \ + } + +#define PRECONDITION_NULL(foo_$) \ + if (foo_$ != NULL) { \ + return ACCEL_PARAM_ERROR; \ + } + +#define PRECONDITION_VALID_STATE(state_$) \ + if (state_$ == NULL) { \ + return ACCEL_PARAM_ERROR; \ + } \ + if (state_$->state == NULL) { \ + return ACCEL_INTERNAL_ERROR; \ + } \ + if (state_$->dimensions <= 0) { \ + return ACCEL_INTERNAL_ERROR; \ + } \ + if (state_$->state->window_size <= 0) { \ + return ACCEL_INTERNAL_ERROR; \ + } \ + if (state_$->state->num_gestures_saved < 0) { \ + return ACCEL_INTERNAL_ERROR; \ + } \ + if (state_$->state->gestures == NULL && state_$->state->num_gestures_saved != 0) { \ + return ACCEL_INTERNAL_ERROR; \ + } \ + if (state_$->state->gestures != NULL && state_$->state->num_gestures_saved == 0) { \ + return ACCEL_INTERNAL_ERROR; \ + } // Decay rate of values we choose to keep. 1.0 is no decay, 2.0 is a doubling every time we keep them. // TODO: should we store the offsets as floats instead? #define ALPHA 1.0 // TODO: include these from a header file? -#define MAX(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a > _b ? _a : _b; }) -#define MIN(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a < _b ? _a : _b; }) +#define MAX(a, b) \ + ({ \ + __typeof__(a) _a = (a); \ + __typeof__(b) _b = (b); \ + _a > _b ? _a : _b; \ + }) +#define MIN(a, b) \ + ({ \ + __typeof__(a) _a = (a); \ + __typeof__(b) _b = (b); \ + _a < _b ? _a : _b; \ + }) void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { if (gesture == NULL || *gesture == NULL) { @@ -81,12 +103,12 @@ void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { accel_gesture *gest = *gesture; if (gest->moving_avg_values != NULL) { - for (int i=0; imoving_avg_values[i])); } } if (gest->normalized_recording != NULL) { - for (int i=0; irecording_size; ++i) { + for (int i = 0; i < gest->recording_size; ++i) { if (gest->normalized_recording[i] != NULL) { free(gest->normalized_recording[i]); gest->normalized_recording[i] = NULL; @@ -104,7 +126,6 @@ void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { *gesture = NULL; } - int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { PRECONDITION_VALID_STATE(state); PRECONDITION_NOT_NULL(gesture); @@ -113,7 +134,7 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { PRECONDITION_NULL((*gesture)); size_t gesture_size = sizeof(accel_gesture); - *gesture = (accel_gesture *) malloc(gesture_size); + *gesture = (accel_gesture *)malloc(gesture_size); if (*gesture == NULL) { return ACCEL_MALLOC_ERROR; } @@ -122,18 +143,19 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { (*gesture)->is_recorded = false; (*gesture)->normalized_recording = NULL; - (*gesture)->moving_avg_values = (moving_avg_values **) my_calloc(state->dimensions, sizeof(moving_avg_values *)); + (*gesture)->moving_avg_values = (moving_avg_values **)my_calloc(state->dimensions, sizeof(moving_avg_values *)); if ((*gesture)->moving_avg_values == NULL) { free((*gesture)); *gesture = NULL; return ACCEL_MALLOC_ERROR; } - for (int i=0; idimensions; ++i) { + for (int i = 0; i < state->dimensions; ++i) { // TODO: these two shouldn't both be the same.... - int result = allocate_moving_avg(state->state->window_size, state->state->window_size, &((*gesture)->moving_avg_values[i])); + int result = allocate_moving_avg(state->state->window_size, state->state->window_size, + &((*gesture)->moving_avg_values[i])); if (result != ACCEL_SUCCESS) { - for (int j=0; jmoving_avg_values[i])); } accel_destroy_gesture(gesture, state->dimensions); @@ -144,10 +166,7 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { } // TODO: needs direct testing with invalid objects. -int accel_generate_state(accel_state **state, - int dimensions, - int window_size, - accel_callback callback, +int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback, const int threshold) { PRECONDITION_NOT_NULL(state); @@ -170,8 +189,8 @@ int accel_generate_state(accel_state **state, size_t state_size = sizeof(accel_state); size_t internal_state_size = sizeof(internal_accel_state); - internal_accel_state *internal_state = (internal_accel_state *) malloc(internal_state_size); - *state = (accel_state *) malloc(state_size); + internal_accel_state *internal_state = (internal_accel_state *)malloc(internal_state_size); + *state = (accel_state *)malloc(state_size); if (*state == NULL || internal_state == NULL) { if (state != NULL) { free(*state); @@ -206,7 +225,7 @@ int accel_destroy_state(accel_state **state) { internal_accel_state *istate = (*state)->state; if (istate->gestures != NULL) { /* TODO: remove all additional fields inside the accel_state variable */ - for (int i=0; inum_gestures_saved; ++i) { + for (int i = 0; i < istate->num_gestures_saved; ++i) { accel_gesture *gest = (istate->gestures[i]); accel_destroy_gesture(&(gest), dimensions); } @@ -228,7 +247,9 @@ int accel_start_record_gesture(accel_state *state, int *gesture) { PRECONDITION_NOT_NULL(gesture); if (state->state->num_gestures_saved != 0) { - accel_gesture **tmp = (accel_gesture **)my_realloc(state->state->gestures, (state->state->num_gestures_saved + 1)*sizeof(accel_gesture *), (state->state->num_gestures_saved)*sizeof(accel_gesture *)); + accel_gesture **tmp = (accel_gesture **)my_realloc( + state->state->gestures, (state->state->num_gestures_saved + 1) * sizeof(accel_gesture *), + (state->state->num_gestures_saved) * sizeof(accel_gesture *)); if (tmp == NULL) { return ACCEL_MALLOC_ERROR; } @@ -250,7 +271,8 @@ int accel_start_record_gesture(accel_state *state, int *gesture) { free(state->state->gestures); state->state->gestures = NULL; } else { - accel_gesture ** tmp = (accel_gesture **)my_realloc(state->state->gestures, state->state->num_gestures_saved - 1, state->state->num_gestures_saved); + accel_gesture **tmp = (accel_gesture **)my_realloc( + state->state->gestures, state->state->num_gestures_saved - 1, state->state->num_gestures_saved); if (tmp != NULL) { // If tmp is null, we don't really care that realloc failed, since a future use of realloc will help us. state->state->gestures = tmp; @@ -266,16 +288,14 @@ int accel_start_record_gesture(accel_state *state, int *gesture) { // The uWave paper suggests a mapping from [-20, 20]->[-15, 15], but cube root // should to work better for variable ranges. // TODO: revisit this decision. -int normalize(int sum) { - return (int) cbrt(sum); -} +int normalize(int sum) { return (int)cbrt(sum); } int reset_gesture(accel_gesture *gest, const int dimensions) { PRECONDITION_NOT_NULL(gest); - for (int i=0; irecording_size; ++i) { + for (int i = 0; i < gest->recording_size; ++i) { gest->offsets[i] = INT16_MAX; } - for (int d=0; dmoving_avg_values[d]); } return ACCEL_SUCCESS; @@ -307,7 +327,7 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { return ACCEL_PARAM_ERROR; } - gesture->offsets = (int *) malloc(gesture->recording_size * sizeof(int)); + gesture->offsets = (int *)malloc(gesture->recording_size * sizeof(int)); if (gesture->offsets == NULL) { return ACCEL_MALLOC_ERROR; } @@ -332,24 +352,30 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { // TODO: check for malloc failure in this function. // TODO: this should return error types instead of being void. - // Follow-up: find usages of this method. +// Follow-up: find usages of this method. void handle_recording_tick(accel_gesture *gesture, int dimensions) { - if (gesture == NULL) { return; } + if (gesture == NULL) { + return; + } // TODO: grow exponentially, not linearly. Linear growth allocates too frequently. if (gesture->recording_size != 0) { - gesture->normalized_recording = (int **) my_realloc(gesture->normalized_recording, (gesture->recording_size + 1) * sizeof(int *), gesture->recording_size * sizeof(int *)); + gesture->normalized_recording = + (int **)my_realloc(gesture->normalized_recording, (gesture->recording_size + 1) * sizeof(int *), + gesture->recording_size * sizeof(int *)); if (gesture->normalized_recording == NULL) { return; } } else { - gesture->normalized_recording = (int **) malloc(sizeof(int *)); + gesture->normalized_recording = (int **)malloc(sizeof(int *)); } - gesture->normalized_recording[gesture->recording_size] = (int *) malloc(sizeof(int) * dimensions); - for (int i=0; inormalized_recording[gesture->recording_size] = (int *)malloc(sizeof(int) * dimensions); + for (int i = 0; i < dimensions; ++i) { // TODO: fix this int/float business. // TODO: check resultant output. - get_latest_frame_moving_avg(gesture->moving_avg_values[i], &(gesture->normalized_recording[gesture->recording_size][i])); - gesture->normalized_recording[gesture->recording_size][i] = normalize(gesture->normalized_recording[gesture->recording_size][i]); + get_latest_frame_moving_avg(gesture->moving_avg_values[i], + &(gesture->normalized_recording[gesture->recording_size][i])); + gesture->normalized_recording[gesture->recording_size][i] = + normalize(gesture->normalized_recording[gesture->recording_size][i]); } ++gesture->recording_size; } @@ -359,8 +385,7 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu PRECONDITION_NOT_NULL(gesture); int dimensions = state->dimensions; - if (gesture->moving_avg_values == NULL || - gesture->offsets == NULL) { + if (gesture->moving_avg_values == NULL || gesture->offsets == NULL) { return ACCEL_INTERNAL_ERROR; } @@ -369,7 +394,7 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu --i; int cost = 0; - for (int d=0; dnormalized_recording[i][d]; int input_i_d = 0; // TODO: complain about invalid return values. @@ -385,12 +410,12 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu if (i == 0) { gesture->offsets[i] = cost; } else { - gesture->offsets[i] = MIN(ALPHA * gesture->offsets[i], cost+gesture->offsets[i-1]); + gesture->offsets[i] = MIN(ALPHA * gesture->offsets[i], cost + gesture->offsets[i - 1]); } } - for (i=1; irecording_size; ++i) { + for (i = 1; i < gesture->recording_size; ++i) { int cost = 0; - for (int d=0; dnormalized_recording[i][d]; int input_i_d = 0; // TODO: complain about invalid return values. @@ -402,13 +427,13 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu cost += input_i_d - recording_i_d; } } - gesture->offsets[i] = MIN(gesture->offsets[i], gesture->offsets[i-1] + cost); + gesture->offsets[i] = MIN(gesture->offsets[i], gesture->offsets[i - 1] + cost); } if (state->callback != NULL) { if (state->state->threshold <= 0) { return ACCEL_PARAM_ERROR; } - float avg_affinity = gesture->offsets[gesture->recording_size-1] * 1.0 / gesture->recording_size; + float avg_affinity = gesture->offsets[gesture->recording_size - 1] * 1.0 / gesture->recording_size; if (avg_affinity < state->state->threshold) { bool reset; int retval = state->callback(state, gesture_id, avg_affinity, &reset); @@ -443,7 +468,7 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { // If the moving average is at a final line. bool avg_line = false; int returned = ACCEL_SUCCESS; - for (int d=0; ddimensions && returned == 0; ++d) { + for (int d = 0; d < state->dimensions && returned == 0; ++d) { returned = append_to_moving_avg(gesture->moving_avg_values[d], accel_data[d], &avg_line); } if (returned != ACCEL_SUCCESS) { @@ -451,7 +476,9 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { continue; } - if (!avg_line) { continue; } + if (!avg_line) { + continue; + } returned = ACCEL_SUCCESS; if (gesture->is_recording) { @@ -489,7 +516,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off return ACCEL_INTERNAL_ERROR; } - for (int i=0; istate->num_gestures_saved; ++i) { + for (int i = 0; i < state->state->num_gestures_saved; ++i) { accel_gesture *gesture = state->state->gestures[i]; // TODO: this should be tested. @@ -497,8 +524,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off return ACCEL_INTERNAL_ERROR; } - if ((*gesture_id == ACCEL_NO_VALID_GESTURE || *offset == ACCEL_NO_VALID_GESTURE) && - *gesture_id != *offset) { + if ((*gesture_id == ACCEL_NO_VALID_GESTURE || *offset == ACCEL_NO_VALID_GESTURE) && *gesture_id != *offset) { return ACCEL_INTERNAL_ERROR; } @@ -512,14 +538,12 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off continue; } - if (*offset == ACCEL_NO_VALID_GESTURE || - gesture->offsets[gesture->recording_size-1] < *offset) { - *offset = gesture->offsets[gesture->recording_size-1]; + if (*offset == ACCEL_NO_VALID_GESTURE || gesture->offsets[gesture->recording_size - 1] < *offset) { + *offset = gesture->offsets[gesture->recording_size - 1]; *gesture_id = i; } } - if (*gesture_id == ACCEL_NO_VALID_GESTURE || - *offset == ACCEL_NO_VALID_GESTURE) { + if (*gesture_id == ACCEL_NO_VALID_GESTURE || *offset == ACCEL_NO_VALID_GESTURE) { return ACCEL_NO_VALID_GESTURE; } return ACCEL_SUCCESS; diff --git a/src/accel.h b/src/accel.h index 3ee93e0..7df4445 100644 --- a/src/accel.h +++ b/src/accel.h @@ -9,8 +9,8 @@ #define ACCEL_MALLOC_ERROR -3 #define ACCEL_NO_VALID_GESTURE -4 -#define ACCEL_VERSION_GEN(major, minor, point, isBeta, isAlpha) \ - (4*(100*((100*major)+minor)+point) + 3 - (isAlpha?2:0) - (isBeta?1:0)) +#define ACCEL_VERSION_GEN(major, minor, point, isBeta, isAlpha) \ + (4 * (100 * ((100 * major) + minor) + point) + 3 - (isAlpha ? 2 : 0) - (isBeta ? 1 : 0)) #define ACCEL_VERSION_CODE ACCEL_VERSION_GEN(1, 0, 0, true, false) @@ -63,7 +63,6 @@ typedef struct accelState { struct internalAccelState *state; } accel_state; - /** * Creates a state object, essentially a constructor. * @param state Pointer-to-pointer of the state being generated, @@ -81,7 +80,8 @@ typedef struct accelState { * gestures must be before the callback is called. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback, const int threshold); +int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback, + const int threshold); /** * Destroys the state object at the pointer pointed to by the state pointer. diff --git a/src/moving_avg_ticker.c b/src/moving_avg_ticker.c index 6856502..eb243d7 100644 --- a/src/moving_avg_ticker.c +++ b/src/moving_avg_ticker.c @@ -3,8 +3,10 @@ #include "moving_avg_ticker.h" -#define PRECONDITION_NOT_NULL(foo) \ - if (foo == NULL) { return MOVING_AVG_PARAM_ERROR; } +#define PRECONDITION_NOT_NULL(foo) \ + if (foo == NULL) { \ + return MOVING_AVG_PARAM_ERROR; \ + } int precondition_valid_moving_avg_values(moving_avg_values *input) { PRECONDITION_NOT_NULL(input); @@ -44,17 +46,17 @@ int allocate_moving_avg(int num_wbuf, int subtotal_sizes, moving_avg_values **al } size_t size = sizeof(moving_avg_values); - *allocated = (moving_avg_values *) malloc(size); + *allocated = (moving_avg_values *)malloc(size); if (allocated == NULL) { return MOVING_AVG_MALLOC_ERROR; } memset(*allocated, 0, size); (*allocated)->max_subtotal_size = subtotal_sizes; - int *wbuf = (int *) calloc(num_wbuf, sizeof(int)); + int *wbuf = (int *)calloc(num_wbuf, sizeof(int)); if (wbuf == NULL) { // Run away, fast! - free (allocated); + free(allocated); *allocated = NULL; return MOVING_AVG_MALLOC_ERROR; } @@ -76,9 +78,11 @@ int reset_moving_avg(moving_avg_values *reset) { return 0; } -int append_to_moving_avg(moving_avg_values *value, int appended, bool* is_at_end) { +int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end) { int is_valid_return_value = precondition_valid_moving_avg_values(value); - if (is_valid_return_value != 0) {return is_valid_return_value;} + if (is_valid_return_value != 0) { + return is_valid_return_value; + } PRECONDITION_NOT_NULL(is_at_end); @@ -100,15 +104,17 @@ int append_to_moving_avg(moving_avg_values *value, int appended, bool* is_at_end int get_latest_frame_moving_avg(moving_avg_values *value, int *frame) { int is_valid_return_value = precondition_valid_moving_avg_values(value); - if (is_valid_return_value != 0) {return is_valid_return_value;} + if (is_valid_return_value != 0) { + return is_valid_return_value; + } PRECONDITION_NOT_NULL(frame); float sum = 0; - for (int i=0; iwbuf_len; ++i) { + for (int i = 0; i < value->wbuf_len; ++i) { sum += value->wbuf[i] * 1.0 / value->wbuf_len; } - *frame = (int) sum; + *frame = (int)sum; return 0; } diff --git a/src/moving_avg_ticker.h b/src/moving_avg_ticker.h index 66afc5a..7192eba 100644 --- a/src/moving_avg_ticker.h +++ b/src/moving_avg_ticker.h @@ -18,7 +18,6 @@ typedef struct moving_avg_values { int max_subtotal_size; } moving_avg_values; - int allocate_moving_avg(int num_wbuf, int subtotal_sizes, moving_avg_values **allocated); int reset_moving_avg(moving_avg_values *reset); diff --git a/src/pebble_makeup.h b/src/pebble_makeup.h index 4982a9d..5b4d194 100644 --- a/src/pebble_makeup.h +++ b/src/pebble_makeup.h @@ -18,7 +18,8 @@ void *my_realloc(void *old_ptr, size_t new_size, size_t old_size) { } void *p = malloc(new_size); - if (!p) return NULL; + if (!p) + return NULL; size_t min_size = new_size < old_size ? new_size : old_size; memcpy(p, old_ptr, min_size); @@ -27,8 +28,9 @@ void *my_realloc(void *old_ptr, size_t new_size, size_t old_size) { } void *my_calloc(size_t num, size_t size) { - void * allocd = malloc(num * size); - if (allocd == NULL) return allocd; + void *allocd = malloc(num * size); + if (allocd == NULL) + return allocd; memset(allocd, 0, num * size); return allocd; } diff --git a/test/accel_test.cc b/test/accel_test.cc index ab1378f..cca0c8e 100644 --- a/test/accel_test.cc +++ b/test/accel_test.cc @@ -4,7 +4,7 @@ #include "testutil.h" -const void * VOID_NULL = NULL; +const void *VOID_NULL = NULL; accel_state *test_fabricate_state_with_callback(int dimensions, accel_callback callback, const int threshold) { accel_state *state = NULL; @@ -30,25 +30,18 @@ accel_state *test_fabricate_3d_state_with_callback(accel_callback callback, cons return test_fabricate_state_with_callback(3, callback, threshold); } -accel_state *test_fabricate_state(int dimensions) { - return test_fabricate_state_with_callback(dimensions, NULL, 0); -} +accel_state *test_fabricate_state(int dimensions) { return test_fabricate_state_with_callback(dimensions, NULL, 0); } -accel_state *test_fabricate_1d_state() { - return test_fabricate_state(1); -} +accel_state *test_fabricate_1d_state() { return test_fabricate_state(1); } -accel_state *test_fabricate_3d_state() { - return test_fabricate_state(3); -} +accel_state *test_fabricate_3d_state() { return test_fabricate_state(3); } -void test_burn_state(accel_state ** state) { +void test_burn_state(accel_state **state) { int result = accel_destroy_state(state); EXPECT_EQ(0, result); EXPECT_EQ(VOID_NULL, *state); } - TEST(AccelFuzzTest, generate_state_null_state) { int result = accel_generate_state(NULL, 3, 1, NULL, 0); EXPECT_EQ(result, ACCEL_PARAM_ERROR); @@ -73,7 +66,7 @@ TEST(AccelFuzzTest, generate_state_negative_or_zero_dimensions) { TEST(AccelFuzzTest, generate_state_invalid_threshold_with_callback_params) { accel_state *state = NULL; - accel_callback nonNullCallback = (accel_callback) 1; + accel_callback nonNullCallback = (accel_callback)1; // Fails for negatives. int result = accel_generate_state(&state, 1, 1, nonNullCallback, -1); @@ -111,21 +104,21 @@ TEST(AccelFuzzTest, generate_state_threshold_fuzzing) { // Threshold of 1 with a non-null callback succeeds EXPECT_EQ(NULL, state); - EXPECT_EQ(ACCEL_SUCCESS, accel_generate_state(&state, 1, 1, (accel_callback) 0x1, 1)); + EXPECT_EQ(ACCEL_SUCCESS, accel_generate_state(&state, 1, 1, (accel_callback)0x1, 1)); accel_destroy_state(&state); // Threshold of 1 with a null callback fails EXPECT_EQ(NULL, state); - EXPECT_EQ(ACCEL_PARAM_ERROR, accel_generate_state(&state, 1, 1, (accel_callback) NULL, 1)); + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_generate_state(&state, 1, 1, (accel_callback)NULL, 1)); // Threshold of 0 with a null callback succeeds EXPECT_EQ(NULL, state); - EXPECT_EQ(ACCEL_SUCCESS, accel_generate_state(&state, 1, 1, (accel_callback) NULL, 0)); + EXPECT_EQ(ACCEL_SUCCESS, accel_generate_state(&state, 1, 1, (accel_callback)NULL, 0)); accel_destroy_state(&state); // Threshold of 0 with a non-null callback fails EXPECT_EQ(NULL, state); - EXPECT_EQ(ACCEL_PARAM_ERROR, accel_generate_state(&state, 1, 1, (accel_callback) 0x1, 0)); + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_generate_state(&state, 1, 1, (accel_callback)0x1, 0)); } TEST(AccelFuzzTest, accel_generate_state_null_callback) { @@ -136,10 +129,10 @@ TEST(AccelFuzzTest, accel_generate_state_null_callback) { result = accel_generate_state(&state, 1, 1, NULL, 0); } - -TEST_CALLBACK(const int, AccelFuzzTest, accel_generate_state_valid_callback, myTest, accel_state *state, int gesture_id, int offset_found, bool *reset_gesture) - *reset_gesture = true; - return ACCEL_SUCCESS; +TEST_CALLBACK(const int, AccelFuzzTest, accel_generate_state_valid_callback, myTest, accel_state *state, int gesture_id, + int offset_found, bool *reset_gesture) +*reset_gesture = true; +return ACCEL_SUCCESS; } TEST(AccelFuzzTest, accel_generate_state_valid_callback) { @@ -148,18 +141,17 @@ TEST(AccelFuzzTest, accel_generate_state_valid_callback) { // Non-null callback, watch it iterate over this stuff. state = test_fabricate_1d_state_with_callback( - &TEST_CALLBACK_NAME(AccelFuzzTest, accel_generate_state_valid_callback, myTest), - 10); + &TEST_CALLBACK_NAME(AccelFuzzTest, accel_generate_state_valid_callback, myTest), 10); EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); - for (int i=0; i<100; ++i) { + for (int i = 0; i < 100; ++i) { int data[1] = {i}; EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); } EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture_id)); - for (int i=0; i<100; ++i) { + for (int i = 0; i < 100; ++i) { int data[1] = {i}; EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); } @@ -231,7 +223,7 @@ TEST(AccelFuzzTest, accel_end_record_gesture_invalid_input) { EXPECT_EQ(0, accel_process_timer_tick(state, data)); result = accel_end_record_gesture(state, gesture); - EXPECT_EQ(0, result) << "gesture " << gesture << " couldn't be recorded correctly" << std::endl; + EXPECT_EQ(0, result) << "gesture " << gesture << " couldn't be recorded correctly" << std::endl; test_burn_state(&state); } @@ -283,9 +275,9 @@ TEST(AccelFuzzTest, accel_find_most_likely_gesture_invalid_input) { TEST(AccelTest, accel_generate_and_destroy) { accel_state *state = NULL; - for (int i=1; i<10; ++i) { + for (int i = 1; i < 10; ++i) { EXPECT_EQ(VOID_NULL, state) << "i = " << i; - EXPECT_EQ(0, accel_generate_state(&state, 2*i, i, NULL, 0)) << "i = " << i; + EXPECT_EQ(0, accel_generate_state(&state, 2 * i, i, NULL, 0)) << "i = " << i; EXPECT_EQ(0, accel_destroy_state(&state)) << "i = " << i; EXPECT_EQ(VOID_NULL, state) << "i = " << i; } @@ -296,13 +288,13 @@ TEST(AccelTest, start_recording_and_close_many_gestures) { state = test_fabricate_1d_state(); int data[1] = {0}; - for (int i=0; i<10; ++i) { + for (int i = 0; i < 10; ++i) { int gesture = 0; ASSERT_EQ(0, accel_start_record_gesture(state, &gesture)); ASSERT_EQ(i, gesture); ASSERT_EQ(0, accel_process_timer_tick(state, data)); } - for (int i=0; i<10; ++i) { + for (int i = 0; i < 10; ++i) { ASSERT_EQ(0, accel_end_record_gesture(state, i)); } test_burn_state(&state); @@ -317,7 +309,7 @@ TEST(AccelTest, record_incredibly_long_sequence) { EXPECT_EQ(0, gesture); int data[] = {1}; - for (int i=0; i<10000; ++i) { + for (int i = 0; i < 10000; ++i) { EXPECT_EQ(0, accel_process_timer_tick(state, data)); } @@ -334,16 +326,16 @@ TEST(AccelTest, end_to_end_test_single_recording) { EXPECT_EQ(0, gesture); int data[] = {1}; - for (int i=0; i<10; ++i) { - data[0] = i*100; + for (int i = 0; i < 10; ++i) { + data[0] = i * 100; EXPECT_EQ(0, accel_process_timer_tick(state, data)); } EXPECT_EQ(0, accel_end_record_gesture(state, gesture)); int prev_affinity = 0; - for (int i=0; i<10; ++i) { - data[0] = i*100; + for (int i = 0; i < 10; ++i) { + data[0] = i * 100; int gesture_found = 1; int affinity_of_gesture = 1; ASSERT_EQ(0, accel_process_timer_tick(state, data)); @@ -368,7 +360,7 @@ TEST(AccelTest, end_to_end_test_multiple_recordings) { EXPECT_EQ(0, first_gesture); int data[] = {1}; - for (int i=0; i<10; ++i) { + for (int i = 0; i < 10; ++i) { data[0] = i; EXPECT_EQ(0, accel_process_timer_tick(state, data)); } @@ -379,16 +371,16 @@ TEST(AccelTest, end_to_end_test_multiple_recordings) { EXPECT_EQ(0, accel_start_record_gesture(state, &second_gesture)); EXPECT_NE(first_gesture, second_gesture); - for (int i=0; i<10; ++i) { - data[0] = i*i; + for (int i = 0; i < 10; ++i) { + data[0] = i * i; EXPECT_EQ(0, accel_process_timer_tick(state, data)); } EXPECT_EQ(0, accel_end_record_gesture(state, second_gesture)); int prev_affinity = 0; - for (int i=0; i<10; ++i) { - data[0] = i*2; + for (int i = 0; i < 10; ++i) { + data[0] = i * 2; int gesture_found = 1; int affinity_of_gesture = 1; ASSERT_EQ(0, accel_process_timer_tick(state, data)); @@ -400,18 +392,62 @@ TEST(AccelTest, end_to_end_test_multiple_recordings) { test_burn_state(&state); } -int main(int argc, char** argv) { +TEST(AccelTest, test_fuzz_reset_affinities) { + accel_state *state = NULL; + + // Null accel states. + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(NULL, 0)); + + // No recorded accelerations + state = test_fabricate_1d_state(); + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, 0)); + + int gesture_id = 0; + EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); + + // A recording gesture with no data. + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); + + int data[1] = {0}; + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + + // A recording gesture with some data. + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); + + EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture_id)); + + // No ticks have been recorded. + EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); + + int found_gesture = 1; + int found_distance = 1; + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &found_gesture, &found_distance)); + + int after_reset_gesture = 1; + int after_reset_distance = 1; + EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); + EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &after_reset_gesture, &after_reset_distance)); + + EXPECT_NE(found_distance, after_reset_distance); + + test_burn_state(&state); +} + +int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); int returnValue; - //Do whatever setup here you will need for your tests here + // Do whatever setup here you will need for your tests here // // - returnValue = RUN_ALL_TESTS(); + returnValue = RUN_ALL_TESTS(); - //Do Your teardown here if required + // Do Your teardown here if required // // diff --git a/test/moving_avg_ticker_test.cc b/test/moving_avg_ticker_test.cc index 59bcd5c..b3b939d 100644 --- a/test/moving_avg_ticker_test.cc +++ b/test/moving_avg_ticker_test.cc @@ -2,7 +2,7 @@ #include "../src/moving_avg_ticker.h" -const void * void_null = NULL; +const void *void_null = NULL; TEST(MovingAvgTicker, InvalidInputValues) { @@ -15,7 +15,7 @@ TEST(MovingAvgTicker, InvalidInputValues) { EXPECT_EQ(MOVING_AVG_PARAM_ERROR, retval); // Pass in a non-null pointer-to-pointer. - moving_avg_values *initial_non_null_value = (moving_avg_values *) 1; + moving_avg_values *initial_non_null_value = (moving_avg_values *)1; allocated = initial_non_null_value; retval = allocate_moving_avg(1, 1, &allocated); EXPECT_EQ(initial_non_null_value, allocated); @@ -189,7 +189,7 @@ TEST(MovingAvgTicker, AppendsCorrectly2_2) { TEST(MovingAvgTicker, AppendToInvalid) { int retval = 0; bool tru = true; - retval = append_to_moving_avg((moving_avg_values *) NULL, 1, &tru); + retval = append_to_moving_avg((moving_avg_values *)NULL, 1, &tru); EXPECT_EQ(MOVING_AVG_PARAM_ERROR, retval); } @@ -217,7 +217,7 @@ TEST(MovingAvgTicker, InvalidLatestFrameParams) { EXPECT_EQ(0, retval); EXPECT_NE(void_null, allocated); - retval = get_latest_frame_moving_avg(allocated, (int *) NULL); + retval = get_latest_frame_moving_avg(allocated, (int *)NULL); EXPECT_EQ(MOVING_AVG_PARAM_ERROR, retval); EXPECT_EQ(0, free_moving_avg(&allocated)); @@ -242,7 +242,7 @@ TEST(MovingAvgTickerFuzzTest, allocate_moving_avg) { EXPECT_EQ(MOVING_AVG_PARAM_ERROR, allocate_moving_avg(1, 0, NULL)); // Test with non-NULL pointer-pointer - allocated = (moving_avg_values *) 0x1; + allocated = (moving_avg_values *)0x1; EXPECT_EQ(MOVING_AVG_PARAM_ERROR, allocate_moving_avg(1, 1, &allocated)); // Test with success, to validate that there was only one difference between this and the above tests. @@ -322,18 +322,18 @@ TEST(MovingAvgTickerFuzzTest, free_moving_avg) { EXPECT_EQ(void_null, allocated); } -int main (int argc, char** argv) { +int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); int returnValue; - //Do whatever setup here you will need for your tests here + // Do whatever setup here you will need for your tests here // // - returnValue = RUN_ALL_TESTS(); + returnValue = RUN_ALL_TESTS(); - //Do Your teardown here if required + // Do Your teardown here if required // // diff --git a/test/testutil.h b/test/testutil.h index 2323ba7..b57cb94 100644 --- a/test/testutil.h +++ b/test/testutil.h @@ -3,18 +3,18 @@ // TODO: this is so hack. -#define TEST_CALLBACK_NAME(testClass, testName, uniqueIdentifier) \ +#define TEST_CALLBACK_NAME(testClass, testName, uniqueIdentifier) \ generated_##testClass##_##testName##_##uniqueIdentifier##_name -#define TEST_CALLBACK_COUNTER(testClass, testName, uniqueIdentifier) \ +#define TEST_CALLBACK_COUNTER(testClass, testName, uniqueIdentifier) \ generated_##testClass##_##testName##_##uniqueIdentifier##_counter -#define TEST_CALLBACK(returnSignature, testClass, testName, uniqueIdentifier, args...) \ - /* count the number of times the callback was invoked. */ \ - uint generated_##testClass##_##testName##_##uniqueIdentifier##_counter = 0; \ - /* define a method that proxies calls to the real method. */ \ - returnSignature generated_##testClass##_##testName##_##uniqueIdentifier##_name (args) { \ - /* increment the counter */ \ +#define TEST_CALLBACK(returnSignature, testClass, testName, uniqueIdentifier, args...) \ + /* count the number of times the callback was invoked. */ \ + uint generated_##testClass##_##testName##_##uniqueIdentifier##_counter = 0; \ + /* define a method that proxies calls to the real method. */ \ + returnSignature generated_##testClass##_##testName##_##uniqueIdentifier##_name(args) { \ + /* increment the counter */ \ generated_##testClass##_##testName##_##uniqueIdentifier##_counter += 1; #endif From d1a1f901b5cc226cb9329cf83be712da799ddbff Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 15 Mar 2014 12:42:12 -0700 Subject: [PATCH 18/68] Updated accel library version for sample-accel --- sample/simple-accelerometer/src/accel.c | 195 +++++++++++------- sample/simple-accelerometer/src/accel.h | 28 ++- .../src/moving_avg_ticker.c | 26 ++- .../src/moving_avg_ticker.h | 1 - .../simple-accelerometer/src/pebble_makeup.h | 8 +- 5 files changed, 162 insertions(+), 96 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index d592e22..4b87932 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -38,39 +38,62 @@ typedef struct { typedef struct internalAccelState { int window_size; + int threshold; int num_gestures_saved; accel_gesture **gestures; } internal_accel_state; -#define PRECONDITION_NOT_NULL(foo_$) \ - if (foo_$ == NULL) { return ACCEL_PARAM_ERROR; } - -#define PRECONDITION_NULL(foo_$) \ - if (foo_$ != NULL) { return ACCEL_PARAM_ERROR; } - -#define PRECONDITION_VALID_STATE(state_$) \ - if (state_$ == NULL) { return ACCEL_PARAM_ERROR; } \ - if (state_$->state == NULL) { return ACCEL_INTERNAL_ERROR; } \ - if (state_$->dimensions <= 0) { return ACCEL_INTERNAL_ERROR; } \ - if (state_$->state->window_size <= 0) { return ACCEL_INTERNAL_ERROR; } \ - if (state_$->state->num_gestures_saved < 0) { return ACCEL_INTERNAL_ERROR; } \ - if (state_$->state->gestures == NULL && state_$->state->num_gestures_saved != 0) { return ACCEL_INTERNAL_ERROR; } \ - if (state_$->state->gestures != NULL && state_$->state->num_gestures_saved == 0) { return ACCEL_INTERNAL_ERROR; } +#define PRECONDITION_NOT_NULL(foo_$) \ + if (foo_$ == NULL) { \ + return ACCEL_PARAM_ERROR; \ + } + +#define PRECONDITION_NULL(foo_$) \ + if (foo_$ != NULL) { \ + return ACCEL_PARAM_ERROR; \ + } + +#define PRECONDITION_VALID_STATE(state_$) \ + if (state_$ == NULL) { \ + return ACCEL_PARAM_ERROR; \ + } \ + if (state_$->state == NULL) { \ + return ACCEL_INTERNAL_ERROR; \ + } \ + if (state_$->dimensions <= 0) { \ + return ACCEL_INTERNAL_ERROR; \ + } \ + if (state_$->state->window_size <= 0) { \ + return ACCEL_INTERNAL_ERROR; \ + } \ + if (state_$->state->num_gestures_saved < 0) { \ + return ACCEL_INTERNAL_ERROR; \ + } \ + if (state_$->state->gestures == NULL && state_$->state->num_gestures_saved != 0) { \ + return ACCEL_INTERNAL_ERROR; \ + } \ + if (state_$->state->gestures != NULL && state_$->state->num_gestures_saved == 0) { \ + return ACCEL_INTERNAL_ERROR; \ + } // Decay rate of values we choose to keep. 1.0 is no decay, 2.0 is a doubling every time we keep them. // TODO: should we store the offsets as floats instead? #define ALPHA 1.0 // TODO: include these from a header file? -#define MAX(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a > _b ? _a : _b; }) -#define MIN(a,b) \ - ({ __typeof__ (a) _a = (a); \ - __typeof__ (b) _b = (b); \ - _a < _b ? _a : _b; }) +#define MAX(a, b) \ + ({ \ + __typeof__(a) _a = (a); \ + __typeof__(b) _b = (b); \ + _a > _b ? _a : _b; \ + }) +#define MIN(a, b) \ + ({ \ + __typeof__(a) _a = (a); \ + __typeof__(b) _b = (b); \ + _a < _b ? _a : _b; \ + }) void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { if (gesture == NULL || *gesture == NULL) { @@ -80,12 +103,12 @@ void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { accel_gesture *gest = *gesture; if (gest->moving_avg_values != NULL) { - for (int i=0; imoving_avg_values[i])); } } if (gest->normalized_recording != NULL) { - for (int i=0; irecording_size; ++i) { + for (int i = 0; i < gest->recording_size; ++i) { if (gest->normalized_recording[i] != NULL) { free(gest->normalized_recording[i]); gest->normalized_recording[i] = NULL; @@ -103,7 +126,6 @@ void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { *gesture = NULL; } - int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { PRECONDITION_VALID_STATE(state); PRECONDITION_NOT_NULL(gesture); @@ -112,7 +134,7 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { PRECONDITION_NULL((*gesture)); size_t gesture_size = sizeof(accel_gesture); - *gesture = (accel_gesture *) malloc(gesture_size); + *gesture = (accel_gesture *)malloc(gesture_size); if (*gesture == NULL) { return ACCEL_MALLOC_ERROR; } @@ -121,18 +143,19 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { (*gesture)->is_recorded = false; (*gesture)->normalized_recording = NULL; - (*gesture)->moving_avg_values = (moving_avg_values **) my_calloc(state->dimensions, sizeof(moving_avg_values *)); + (*gesture)->moving_avg_values = (moving_avg_values **)my_calloc(state->dimensions, sizeof(moving_avg_values *)); if ((*gesture)->moving_avg_values == NULL) { free((*gesture)); *gesture = NULL; return ACCEL_MALLOC_ERROR; } - for (int i=0; idimensions; ++i) { + for (int i = 0; i < state->dimensions; ++i) { // TODO: these two shouldn't both be the same.... - int result = allocate_moving_avg(state->state->window_size, state->state->window_size, &((*gesture)->moving_avg_values[i])); + int result = allocate_moving_avg(state->state->window_size, state->state->window_size, + &((*gesture)->moving_avg_values[i])); if (result != ACCEL_SUCCESS) { - for (int j=0; jmoving_avg_values[i])); } accel_destroy_gesture(gesture, state->dimensions); @@ -143,7 +166,8 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { } // TODO: needs direct testing with invalid objects. -int accel_generate_state(accel_state **state, int dimensions, int window_size) { +int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback, + const int threshold) { PRECONDITION_NOT_NULL(state); // TODO: write a test for this value. @@ -155,12 +179,18 @@ int accel_generate_state(accel_state **state, int dimensions, int window_size) { if (window_size <= 0) { return ACCEL_PARAM_ERROR; } + if (threshold <= 0 && callback != NULL) { + return ACCEL_PARAM_ERROR; + } + if (threshold > 0 && callback == NULL) { + return ACCEL_PARAM_ERROR; + } size_t state_size = sizeof(accel_state); size_t internal_state_size = sizeof(internal_accel_state); - internal_accel_state *internal_state = (internal_accel_state *) malloc(internal_state_size); - *state = (accel_state *) malloc(state_size); + internal_accel_state *internal_state = (internal_accel_state *)malloc(internal_state_size); + *state = (accel_state *)malloc(state_size); if (*state == NULL || internal_state == NULL) { if (state != NULL) { free(*state); @@ -178,9 +208,10 @@ int accel_generate_state(accel_state **state, int dimensions, int window_size) { (*state)->state = internal_state; - + (*state)->callback = callback; (*state)->dimensions = dimensions; (*state)->state->window_size = window_size > 0 ? window_size : 2; + (*state)->state->threshold = threshold; return ACCEL_SUCCESS; } @@ -194,7 +225,7 @@ int accel_destroy_state(accel_state **state) { internal_accel_state *istate = (*state)->state; if (istate->gestures != NULL) { /* TODO: remove all additional fields inside the accel_state variable */ - for (int i=0; inum_gestures_saved; ++i) { + for (int i = 0; i < istate->num_gestures_saved; ++i) { accel_gesture *gest = (istate->gestures[i]); accel_destroy_gesture(&(gest), dimensions); } @@ -216,7 +247,9 @@ int accel_start_record_gesture(accel_state *state, int *gesture) { PRECONDITION_NOT_NULL(gesture); if (state->state->num_gestures_saved != 0) { - accel_gesture **tmp = (accel_gesture **)my_realloc(state->state->gestures, (state->state->num_gestures_saved + 1)*sizeof(accel_gesture *), (state->state->num_gestures_saved)*sizeof(accel_gesture *)); + accel_gesture **tmp = (accel_gesture **)my_realloc( + state->state->gestures, (state->state->num_gestures_saved + 1) * sizeof(accel_gesture *), + (state->state->num_gestures_saved) * sizeof(accel_gesture *)); if (tmp == NULL) { return ACCEL_MALLOC_ERROR; } @@ -238,7 +271,8 @@ int accel_start_record_gesture(accel_state *state, int *gesture) { free(state->state->gestures); state->state->gestures = NULL; } else { - accel_gesture ** tmp = (accel_gesture **)my_realloc(state->state->gestures, state->state->num_gestures_saved - 1, state->state->num_gestures_saved); + accel_gesture **tmp = (accel_gesture **)my_realloc( + state->state->gestures, state->state->num_gestures_saved - 1, state->state->num_gestures_saved); if (tmp != NULL) { // If tmp is null, we don't really care that realloc failed, since a future use of realloc will help us. state->state->gestures = tmp; @@ -254,16 +288,14 @@ int accel_start_record_gesture(accel_state *state, int *gesture) { // The uWave paper suggests a mapping from [-20, 20]->[-15, 15], but cube root // should to work better for variable ranges. // TODO: revisit this decision. -int normalize(int sum) { - return (int) cbrt(sum); -} +int normalize(int sum) { return (int)cbrt(sum); } int reset_gesture(accel_gesture *gest, const int dimensions) { PRECONDITION_NOT_NULL(gest); - for (int i=0; irecording_size; ++i) { + for (int i = 0; i < gest->recording_size; ++i) { gest->offsets[i] = INT16_MAX; } - for (int d=0; dmoving_avg_values[d]); } return ACCEL_SUCCESS; @@ -295,7 +327,7 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { return ACCEL_PARAM_ERROR; } - gesture->offsets = (int *) malloc(gesture->recording_size * sizeof(int)); + gesture->offsets = (int *)malloc(gesture->recording_size * sizeof(int)); if (gesture->offsets == NULL) { return ACCEL_MALLOC_ERROR; } @@ -314,34 +346,40 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { // TODO: check for malloc failure in this function. // TODO: this should return error types instead of being void. - // Follow-up: find usages of this method. +// Follow-up: find usages of this method. void handle_recording_tick(accel_gesture *gesture, int dimensions) { - if (gesture == NULL) { return; } + if (gesture == NULL) { + return; + } // TODO: grow exponentially, not linearly. Linear growth allocates too frequently. if (gesture->recording_size != 0) { - gesture->normalized_recording = (int **) my_realloc(gesture->normalized_recording, (gesture->recording_size + 1) * sizeof(int *), gesture->recording_size * sizeof(int *)); + gesture->normalized_recording = + (int **)my_realloc(gesture->normalized_recording, (gesture->recording_size + 1) * sizeof(int *), + gesture->recording_size * sizeof(int *)); if (gesture->normalized_recording == NULL) { return; } } else { - gesture->normalized_recording = (int **) malloc(sizeof(int *)); + gesture->normalized_recording = (int **)malloc(sizeof(int *)); } - gesture->normalized_recording[gesture->recording_size] = (int *) malloc(sizeof(int) * dimensions); - for (int i=0; inormalized_recording[gesture->recording_size] = (int *)malloc(sizeof(int) * dimensions); + for (int i = 0; i < dimensions; ++i) { // TODO: fix this int/float business. // TODO: check resultant output. - get_latest_frame_moving_avg(gesture->moving_avg_values[i], &(gesture->normalized_recording[gesture->recording_size][i])); - gesture->normalized_recording[gesture->recording_size][i] = normalize(gesture->normalized_recording[gesture->recording_size][i]); + get_latest_frame_moving_avg(gesture->moving_avg_values[i], + &(gesture->normalized_recording[gesture->recording_size][i])); + gesture->normalized_recording[gesture->recording_size][i] = + normalize(gesture->normalized_recording[gesture->recording_size][i]); } ++gesture->recording_size; } -int handle_evaluation_tick(accel_gesture *gesture, int dimensions) { +int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gesture_id) { // TODO: load the input at the beginning instead of gesture->recording_size times. PRECONDITION_NOT_NULL(gesture); + int dimensions = state->dimensions; - if (gesture->moving_avg_values == NULL || - gesture->offsets == NULL) { + if (gesture->moving_avg_values == NULL || gesture->offsets == NULL) { return ACCEL_INTERNAL_ERROR; } @@ -350,7 +388,7 @@ int handle_evaluation_tick(accel_gesture *gesture, int dimensions) { --i; int cost = 0; - for (int d=0; dnormalized_recording[i][d]; int input_i_d = 0; // TODO: complain about invalid return values. @@ -366,12 +404,12 @@ int handle_evaluation_tick(accel_gesture *gesture, int dimensions) { if (i == 0) { gesture->offsets[i] = cost; } else { - gesture->offsets[i] = MIN(ALPHA * gesture->offsets[i], cost+gesture->offsets[i-1]); + gesture->offsets[i] = MIN(ALPHA * gesture->offsets[i], cost + gesture->offsets[i - 1]); } } - for (i=1; irecording_size; ++i) { + for (i = 1; i < gesture->recording_size; ++i) { int cost = 0; - for (int d=0; dnormalized_recording[i][d]; int input_i_d = 0; // TODO: complain about invalid return values. @@ -383,7 +421,21 @@ int handle_evaluation_tick(accel_gesture *gesture, int dimensions) { cost += input_i_d - recording_i_d; } } - gesture->offsets[i] = MIN(gesture->offsets[i], gesture->offsets[i-1] + cost); + gesture->offsets[i] = MIN(gesture->offsets[i], gesture->offsets[i - 1] + cost); + } + if (state->callback != NULL) { + if (state->state->threshold <= 0) { + return ACCEL_PARAM_ERROR; + } + float avg_affinity = gesture->offsets[gesture->recording_size - 1] * 1.0 / gesture->recording_size; + if (avg_affinity < state->state->threshold) { + bool reset; + int retval = state->callback(state, gesture_id, avg_affinity, &reset); + if (reset == true) { + reset_gesture(gesture, dimensions); + } + return retval; + } } return ACCEL_SUCCESS; } @@ -393,8 +445,8 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { PRECONDITION_NOT_NULL(accel_data); int retcode = ACCEL_SUCCESS; - for (int gesture_iter = 0; gesture_iter < state->state->num_gestures_saved; ++gesture_iter) { - accel_gesture *gesture = state->state->gestures[gesture_iter]; + for (int gesture_id = 0; gesture_id < state->state->num_gestures_saved; ++gesture_id) { + accel_gesture *gesture = state->state->gestures[gesture_id]; if (gesture == NULL) { retcode = ACCEL_INTERNAL_ERROR; continue; @@ -410,7 +462,7 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { // If the moving average is at a final line. bool avg_line = false; int returned = ACCEL_SUCCESS; - for (int d=0; ddimensions && returned == 0; ++d) { + for (int d = 0; d < state->dimensions && returned == 0; ++d) { returned = append_to_moving_avg(gesture->moving_avg_values[d], accel_data[d], &avg_line); } if (returned != ACCEL_SUCCESS) { @@ -418,13 +470,15 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { continue; } - if (!avg_line) { continue; } + if (!avg_line) { + continue; + } returned = ACCEL_SUCCESS; if (gesture->is_recording) { handle_recording_tick(gesture, state->dimensions); } else if (gesture->is_recorded) { - returned = handle_evaluation_tick(gesture, state->dimensions); + returned = handle_evaluation_tick(state, gesture, gesture_id); if (returned != ACCEL_SUCCESS) { retcode = returned; } @@ -456,7 +510,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off return ACCEL_INTERNAL_ERROR; } - for (int i=0; istate->num_gestures_saved; ++i) { + for (int i = 0; i < state->state->num_gestures_saved; ++i) { accel_gesture *gesture = state->state->gestures[i]; // TODO: this should be tested. @@ -464,8 +518,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off return ACCEL_INTERNAL_ERROR; } - if ((*gesture_id == ACCEL_NO_VALID_GESTURE || *offset == ACCEL_NO_VALID_GESTURE) && - *gesture_id != *offset) { + if ((*gesture_id == ACCEL_NO_VALID_GESTURE || *offset == ACCEL_NO_VALID_GESTURE) && *gesture_id != *offset) { return ACCEL_INTERNAL_ERROR; } @@ -479,14 +532,12 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off continue; } - if (*offset == ACCEL_NO_VALID_GESTURE || - gesture->offsets[gesture->recording_size-1] < *offset) { - *offset = gesture->offsets[gesture->recording_size-1]; + if (*offset == ACCEL_NO_VALID_GESTURE || gesture->offsets[gesture->recording_size - 1] < *offset) { + *offset = gesture->offsets[gesture->recording_size - 1]; *gesture_id = i; } } - if (*gesture_id == ACCEL_NO_VALID_GESTURE || - *offset == ACCEL_NO_VALID_GESTURE) { + if (*gesture_id == ACCEL_NO_VALID_GESTURE || *offset == ACCEL_NO_VALID_GESTURE) { return ACCEL_NO_VALID_GESTURE; } return ACCEL_SUCCESS; diff --git a/sample/simple-accelerometer/src/accel.h b/sample/simple-accelerometer/src/accel.h index 19c7fcf..7df4445 100644 --- a/sample/simple-accelerometer/src/accel.h +++ b/sample/simple-accelerometer/src/accel.h @@ -9,18 +9,13 @@ #define ACCEL_MALLOC_ERROR -3 #define ACCEL_NO_VALID_GESTURE -4 -#define ACCEL_VERSION_GEN(major, minor, point, isBeta, isAlpha) \ - (4*(100*((100*major)+minor)+point) + 3 - (isAlpha?2:0) - (isBeta?1:0)) +#define ACCEL_VERSION_GEN(major, minor, point, isBeta, isAlpha) \ + (4 * (100 * ((100 * major) + minor) + point) + 3 - (isAlpha ? 2 : 0) - (isBeta ? 1 : 0)) #define ACCEL_VERSION_CODE ACCEL_VERSION_GEN(1, 0, 0, true, false) struct internalAccelState; - -typedef struct { - int dimensions; - - struct internalAccelState *state; -} accel_state; +struct accelState; /** * Callback called whenever a given gesture drops below the offset/length @@ -59,7 +54,14 @@ typedef struct { * refer to the ACCEL_MIN_RESERVED definition inside * their implementations. */ -typedef const int (*accel_callback)(accel_state *state, int gesture_id, int offset_found, bool *reset_gesture); +typedef const int (*accel_callback)(accelState *state, int gesture_id, int offset_found, bool *reset_gesture); + +typedef struct accelState { + int dimensions; + + accel_callback callback; + struct internalAccelState *state; +} accel_state; /** * Creates a state object, essentially a constructor. @@ -71,9 +73,15 @@ typedef const int (*accel_callback)(accel_state *state, int gesture_id, int offs * represents. * @param window_size The size of the moving windows used to calculate smoothed * sensor readings. + * @param callback A callback that is triggered whenever a gesture passes a + * threshold. See the ``accel_callback`` typedef for more + * information. + * @param threshold The minimum threshold offset (divided by length) that all + * gestures must be before the callback is called. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_generate_state(accel_state **state, int dimensions, int window_size); +int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback, + const int threshold); /** * Destroys the state object at the pointer pointed to by the state pointer. diff --git a/sample/simple-accelerometer/src/moving_avg_ticker.c b/sample/simple-accelerometer/src/moving_avg_ticker.c index 6856502..eb243d7 100644 --- a/sample/simple-accelerometer/src/moving_avg_ticker.c +++ b/sample/simple-accelerometer/src/moving_avg_ticker.c @@ -3,8 +3,10 @@ #include "moving_avg_ticker.h" -#define PRECONDITION_NOT_NULL(foo) \ - if (foo == NULL) { return MOVING_AVG_PARAM_ERROR; } +#define PRECONDITION_NOT_NULL(foo) \ + if (foo == NULL) { \ + return MOVING_AVG_PARAM_ERROR; \ + } int precondition_valid_moving_avg_values(moving_avg_values *input) { PRECONDITION_NOT_NULL(input); @@ -44,17 +46,17 @@ int allocate_moving_avg(int num_wbuf, int subtotal_sizes, moving_avg_values **al } size_t size = sizeof(moving_avg_values); - *allocated = (moving_avg_values *) malloc(size); + *allocated = (moving_avg_values *)malloc(size); if (allocated == NULL) { return MOVING_AVG_MALLOC_ERROR; } memset(*allocated, 0, size); (*allocated)->max_subtotal_size = subtotal_sizes; - int *wbuf = (int *) calloc(num_wbuf, sizeof(int)); + int *wbuf = (int *)calloc(num_wbuf, sizeof(int)); if (wbuf == NULL) { // Run away, fast! - free (allocated); + free(allocated); *allocated = NULL; return MOVING_AVG_MALLOC_ERROR; } @@ -76,9 +78,11 @@ int reset_moving_avg(moving_avg_values *reset) { return 0; } -int append_to_moving_avg(moving_avg_values *value, int appended, bool* is_at_end) { +int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end) { int is_valid_return_value = precondition_valid_moving_avg_values(value); - if (is_valid_return_value != 0) {return is_valid_return_value;} + if (is_valid_return_value != 0) { + return is_valid_return_value; + } PRECONDITION_NOT_NULL(is_at_end); @@ -100,15 +104,17 @@ int append_to_moving_avg(moving_avg_values *value, int appended, bool* is_at_end int get_latest_frame_moving_avg(moving_avg_values *value, int *frame) { int is_valid_return_value = precondition_valid_moving_avg_values(value); - if (is_valid_return_value != 0) {return is_valid_return_value;} + if (is_valid_return_value != 0) { + return is_valid_return_value; + } PRECONDITION_NOT_NULL(frame); float sum = 0; - for (int i=0; iwbuf_len; ++i) { + for (int i = 0; i < value->wbuf_len; ++i) { sum += value->wbuf[i] * 1.0 / value->wbuf_len; } - *frame = (int) sum; + *frame = (int)sum; return 0; } diff --git a/sample/simple-accelerometer/src/moving_avg_ticker.h b/sample/simple-accelerometer/src/moving_avg_ticker.h index 66afc5a..7192eba 100644 --- a/sample/simple-accelerometer/src/moving_avg_ticker.h +++ b/sample/simple-accelerometer/src/moving_avg_ticker.h @@ -18,7 +18,6 @@ typedef struct moving_avg_values { int max_subtotal_size; } moving_avg_values; - int allocate_moving_avg(int num_wbuf, int subtotal_sizes, moving_avg_values **allocated); int reset_moving_avg(moving_avg_values *reset); diff --git a/sample/simple-accelerometer/src/pebble_makeup.h b/sample/simple-accelerometer/src/pebble_makeup.h index 4982a9d..5b4d194 100644 --- a/sample/simple-accelerometer/src/pebble_makeup.h +++ b/sample/simple-accelerometer/src/pebble_makeup.h @@ -18,7 +18,8 @@ void *my_realloc(void *old_ptr, size_t new_size, size_t old_size) { } void *p = malloc(new_size); - if (!p) return NULL; + if (!p) + return NULL; size_t min_size = new_size < old_size ? new_size : old_size; memcpy(p, old_ptr, min_size); @@ -27,8 +28,9 @@ void *my_realloc(void *old_ptr, size_t new_size, size_t old_size) { } void *my_calloc(size_t num, size_t size) { - void * allocd = malloc(num * size); - if (allocd == NULL) return allocd; + void *allocd = malloc(num * size); + if (allocd == NULL) + return allocd; memset(allocd, 0, num * size); return allocd; } From 963c8bb6b4c0b52696400fd5dc88e3ef0789e490 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Mon, 17 Mar 2014 23:11:36 -0700 Subject: [PATCH 19/68] Formatted simple-accelerometer.c --- .../src/simple-accelerometer.c | 141 +++++++++--------- 1 file changed, 68 insertions(+), 73 deletions(-) diff --git a/sample/simple-accelerometer/src/simple-accelerometer.c b/sample/simple-accelerometer/src/simple-accelerometer.c index 3599367..ab89790 100644 --- a/sample/simple-accelerometer/src/simple-accelerometer.c +++ b/sample/simple-accelerometer/src/simple-accelerometer.c @@ -10,106 +10,101 @@ static int recording_gesture = 0; static bool recording = false; static void select_click_handler(ClickRecognizerRef recognizer, void *context) { - int result = 0; - if (recording) { - text_layer_set_text(text_layer, "Done recording"); - result = accel_end_record_gesture(state, recording_gesture); - if (result == ACCEL_SUCCESS) { - recording = false; - text_layer_set_text(text_layer, "End Successfully"); - } else { - text_layer_set_text(text_layer, "End Unsuccessfully"); - } - } else { - result = accel_start_record_gesture(state, &recording_gesture); - if (result == ACCEL_SUCCESS) { - recording = true; - text_layer_set_text(text_layer, "Start Successfully"); + int result = 0; + if (recording) { + text_layer_set_text(text_layer, "Done recording"); + result = accel_end_record_gesture(state, recording_gesture); + if (result == ACCEL_SUCCESS) { + recording = false; + text_layer_set_text(text_layer, "End Successfully"); + } else { + text_layer_set_text(text_layer, "End Unsuccessfully"); + } } else { - text_layer_set_text(text_layer, "Start Unsuccessfully"); + result = accel_start_record_gesture(state, &recording_gesture); + if (result == ACCEL_SUCCESS) { + recording = true; + text_layer_set_text(text_layer, "Start Successfully"); + } else { + text_layer_set_text(text_layer, "Start Unsuccessfully"); + } } - } } static void up_click_handler(ClickRecognizerRef recognizer, void *context) { - if (state == NULL) { - int result = accel_generate_state(&state, 3, 1); - if (result == ACCEL_SUCCESS) { - text_layer_set_text(text_layer, "Allocated Successfuly"); - } else { - text_layer_set_text(text_layer, "sAllocated Unsuccessfullys"); - } - } else { - int result = accel_destroy_state(&state); - if (result == ACCEL_SUCCESS) { - text_layer_set_text(text_layer, "Successful deletion"); + if (state == NULL) { + int result = accel_generate_state(&state, 3, 1); + if (result == ACCEL_SUCCESS) { + text_layer_set_text(text_layer, "Allocated Successfuly"); + } else { + text_layer_set_text(text_layer, "sAllocated Unsuccessfullys"); + } } else { - text_layer_set_text(text_layer, "Non-successful deletion"); + int result = accel_destroy_state(&state); + if (result == ACCEL_SUCCESS) { + text_layer_set_text(text_layer, "Successful deletion"); + } else { + text_layer_set_text(text_layer, "Non-successful deletion"); + } } - } } static void down_click_handler(ClickRecognizerRef recognizer, void *context) { - AccelData data; - accel_service_peek(&data); - - int accel_data[3] = {data.x, data.y, data.z}; - - int result = accel_process_timer_tick(state, accel_data); - if (result == ACCEL_SUCCESS) { - text_layer_set_text(text_layer, "Successful tick"); - - int gesture_id = 0; - int distance = 0; - accel_find_most_likely_gesture(state, &gesture_id, &distance); - APP_LOG(APP_LOG_LEVEL_INFO, "Distance: %i, gesture: %i", distance, gesture_id); - } else { - text_layer_set_text(text_layer, "Non-successful tick"); - } + AccelData data; + accel_service_peek(&data); + + int accel_data[3] = {data.x, data.y, data.z}; + + int result = accel_process_timer_tick(state, accel_data); + if (result == ACCEL_SUCCESS) { + text_layer_set_text(text_layer, "Successful tick"); + + int gesture_id = 0; + int distance = 0; + accel_find_most_likely_gesture(state, &gesture_id, &distance); + APP_LOG(APP_LOG_LEVEL_INFO, "Distance: %i, gesture: %i", distance, gesture_id); + } else { + text_layer_set_text(text_layer, "Non-successful tick"); + } } static void click_config_provider(void *context) { - window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler); - window_single_click_subscribe(BUTTON_ID_UP, up_click_handler); - window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler); + window_single_click_subscribe(BUTTON_ID_SELECT, select_click_handler); + window_single_click_subscribe(BUTTON_ID_UP, up_click_handler); + window_single_click_subscribe(BUTTON_ID_DOWN, down_click_handler); } static void window_load(Window *window) { - Layer *window_layer = window_get_root_layer(window); - GRect bounds = layer_get_bounds(window_layer); + Layer *window_layer = window_get_root_layer(window); + GRect bounds = layer_get_bounds(window_layer); - text_layer = text_layer_create((GRect) { .origin = { 0, 72 }, .size = { bounds.size.w, 20 } }); - text_layer_set_text(text_layer, "Press a button"); - text_layer_set_text_alignment(text_layer, GTextAlignmentCenter); - layer_add_child(window_layer, text_layer_get_layer(text_layer)); + text_layer = text_layer_create((GRect) {.origin = {0, 72}, .size = {bounds.size.w, 20}}); + text_layer_set_text(text_layer, "Press a button"); + text_layer_set_text_alignment(text_layer, GTextAlignmentCenter); + layer_add_child(window_layer, text_layer_get_layer(text_layer)); } -static void window_unload(Window *window) { - text_layer_destroy(text_layer); -} +static void window_unload(Window *window) { text_layer_destroy(text_layer); } static void init(void) { - window = window_create(); - window_set_click_config_provider(window, click_config_provider); - window_set_window_handlers(window, (WindowHandlers) { - .load = window_load, - .unload = window_unload, - }); - const bool animated = true; - accel_data_service_subscribe(0, NULL); - window_stack_push(window, animated); + window = window_create(); + window_set_click_config_provider(window, click_config_provider); + window_set_window_handlers(window, (WindowHandlers) {.load = window_load, .unload = window_unload, }); + const bool animated = true; + accel_data_service_subscribe(0, NULL); + window_stack_push(window, animated); } static void deinit(void) { - accel_data_service_unsubscribe(); - window_destroy(window); + accel_data_service_unsubscribe(); + window_destroy(window); } int main(void) { - init(); + init(); - APP_LOG(APP_LOG_LEVEL_DEBUG, "Done initializing, pushed window: %p", window); + APP_LOG(APP_LOG_LEVEL_DEBUG, "Done initializing, pushed window: %p", window); - app_event_loop(); - deinit(); + app_event_loop(); + deinit(); } From 072e6afb9b9ea296dfbedaffa7a1bbc248f5c04f Mon Sep 17 00:00:00 2001 From: shalecraig Date: Mon, 17 Mar 2014 23:37:40 -0700 Subject: [PATCH 20/68] I believe I have setup the commit setup script --- s/hooks/auto/pre-commit/clang-format.sh | 5 +++++ s/hooks/config/pre-commit | 21 +++++++++++++++++++++ s/setup.sh | 19 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100755 s/hooks/auto/pre-commit/clang-format.sh create mode 100755 s/hooks/config/pre-commit create mode 100755 s/setup.sh diff --git a/s/hooks/auto/pre-commit/clang-format.sh b/s/hooks/auto/pre-commit/clang-format.sh new file mode 100755 index 0000000..1ed2e03 --- /dev/null +++ b/s/hooks/auto/pre-commit/clang-format.sh @@ -0,0 +1,5 @@ +#!/bin/sh +set -e +pushd $(git rev-parse --show-toplevel || echo ".") +clang-format -i src/*.{c,h} test/*.{cc,h} sample/**/*.{c,h,cc,cpp} +popd diff --git a/s/hooks/config/pre-commit b/s/hooks/config/pre-commit new file mode 100755 index 0000000..4766afa --- /dev/null +++ b/s/hooks/config/pre-commit @@ -0,0 +1,21 @@ +#!/bin/sh + +set -e + +echo ------------------------------------------- +echo -- Pre-commit -- +echo ------------------------------------------- + +# Goto root. +pushd $(git rev-parse --show-toplevel || echo ".") + +# Make the individual scripts runnable +find . -path ./s/hooks/auto/pre-commit/\*.sh -exec chmod +x "{}" \; + +find . -path ./s/hooks/auto/pre-commit/\*.sh -exec sh "{}" \; + +popd + +echo ------------------------------------------- +echo -- Done! -- +echo ------------------------------------------- diff --git a/s/setup.sh b/s/setup.sh new file mode 100755 index 0000000..5afcc89 --- /dev/null +++ b/s/setup.sh @@ -0,0 +1,19 @@ +#!/bin/sh + +set -e + +echo ------------------------------------------- +echo -- Git Hooks -- +echo ------------------------------------------- + +# Goto root. +pushd $(git rev-parse --show-toplevel || echo ".") + +find . -path ./s/hooks/config/\* -exec chmod +x "{}" \; +find . -path ./s/hooks/config/\* -exec cp "{}" .git/hooks \; + +popd + +echo ------------------------------------------- +echo -- Done! -- +echo ------------------------------------------- From 76db08c1dc625cd72da76cb252ca603c1b60befe Mon Sep 17 00:00:00 2001 From: shalecraig Date: Mon, 17 Mar 2014 23:54:26 -0700 Subject: [PATCH 21/68] Cleaned up the clang-format commit hook --- s/hooks/auto/pre-commit/clang-format.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/s/hooks/auto/pre-commit/clang-format.sh b/s/hooks/auto/pre-commit/clang-format.sh index 1ed2e03..4070e1b 100755 --- a/s/hooks/auto/pre-commit/clang-format.sh +++ b/s/hooks/auto/pre-commit/clang-format.sh @@ -1,5 +1,10 @@ #!/bin/sh set -e + pushd $(git rev-parse --show-toplevel || echo ".") -clang-format -i src/*.{c,h} test/*.{cc,h} sample/**/*.{c,h,cc,cpp} + +git diff --name-only --cached | \ + grep -E '\.(h|c|cc|cpp)' | \ + xargs clang-format -i + popd From 12eb59d9a5c459c0c0a3c9fbe0e015b0550fe32f Mon Sep 17 00:00:00 2001 From: shalecraig Date: Tue, 18 Mar 2014 00:45:53 -0700 Subject: [PATCH 22/68] added comment describing the change --- s/hooks/auto/pre-commit/clang-format.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/s/hooks/auto/pre-commit/clang-format.sh b/s/hooks/auto/pre-commit/clang-format.sh index 4070e1b..6dee0bc 100755 --- a/s/hooks/auto/pre-commit/clang-format.sh +++ b/s/hooks/auto/pre-commit/clang-format.sh @@ -3,6 +3,7 @@ set -e pushd $(git rev-parse --show-toplevel || echo ".") +# Use clang-format on changed files. git diff --name-only --cached | \ grep -E '\.(h|c|cc|cpp)' | \ xargs clang-format -i From 1d21e3e5a6dad7711ca48eb819e3b1f4965e7f47 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Wed, 19 Mar 2014 01:23:14 -0700 Subject: [PATCH 23/68] Updated the version to be 1.1.0a --- src/accel.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/accel.h b/src/accel.h index 7df4445..e1c8244 100644 --- a/src/accel.h +++ b/src/accel.h @@ -12,7 +12,7 @@ #define ACCEL_VERSION_GEN(major, minor, point, isBeta, isAlpha) \ (4 * (100 * ((100 * major) + minor) + point) + 3 - (isAlpha ? 2 : 0) - (isBeta ? 1 : 0)) -#define ACCEL_VERSION_CODE ACCEL_VERSION_GEN(1, 0, 0, true, false) +#define ACCEL_VERSION_CODE ACCEL_VERSION_GEN(1, 1, 0, false, true) struct internalAccelState; struct accelState; From ab8bbd1319d294ffaa2b9c49bc1adb307e14ffbb Mon Sep 17 00:00:00 2001 From: shalecraig Date: Wed, 19 Mar 2014 01:27:58 -0700 Subject: [PATCH 24/68] Make sure the version is updated correctly --- test/accel_test.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/accel_test.cc b/test/accel_test.cc index cca0c8e..3ec343c 100644 --- a/test/accel_test.cc +++ b/test/accel_test.cc @@ -436,6 +436,10 @@ TEST(AccelTest, test_fuzz_reset_affinities) { test_burn_state(&state); } +TEST(AccelVersionTest, 1_0_0_to_1_1_0_a) { + EXPECT_GT(ACCEL_VERSION_GEN(1, 1, 0, false, true), ACCEL_VERSION_GEN(1, 0, 0, true, false)); +} + int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); From 9e403c37c89aa9a6d5ab4b783f0739aa4b1d87fd Mon Sep 17 00:00:00 2001 From: shalecraig Date: Wed, 19 Mar 2014 01:37:57 -0700 Subject: [PATCH 25/68] Moved tests around --- test/accel_test.cc | 2 +- test/{testutil.h => callback_util.h} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename test/{testutil.h => callback_util.h} (100%) diff --git a/test/accel_test.cc b/test/accel_test.cc index 3ec343c..e46445a 100644 --- a/test/accel_test.cc +++ b/test/accel_test.cc @@ -2,7 +2,7 @@ #include "../src/accel.h" -#include "testutil.h" +#include "callback_util.h" const void *VOID_NULL = NULL; diff --git a/test/testutil.h b/test/callback_util.h similarity index 100% rename from test/testutil.h rename to test/callback_util.h From aaa920a578c4f79ce6c18485febc76821a73da31 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 00:52:04 -0700 Subject: [PATCH 26/68] Initial cleanup Next is the makefile :D --- src/accel.h | 10 +- src/moving_avg_ticker.h | 8 + src/pebble_makeup.h | 8 + test/accel_test.cc | 459 -------------------- test/{moving_avg_ticker_test.cc => main.cc} | 204 ++++++++- test/util.h | 8 + 6 files changed, 236 insertions(+), 461 deletions(-) delete mode 100644 test/accel_test.cc rename test/{moving_avg_ticker_test.cc => main.cc} (59%) create mode 100644 test/util.h diff --git a/src/accel.h b/src/accel.h index e1c8244..578fb8e 100644 --- a/src/accel.h +++ b/src/accel.h @@ -1,6 +1,10 @@ #ifndef ACCEL_H #define ACCEL_H +#ifdef __cplusplus +extern "C" { +#endif + #include #define ACCEL_SUCCESS 0 @@ -54,7 +58,7 @@ struct accelState; * refer to the ACCEL_MIN_RESERVED definition inside * their implementations. */ -typedef const int (*accel_callback)(accelState *state, int gesture_id, int offset_found, bool *reset_gesture); +typedef const int (*accel_callback)(struct accelState *state, int gesture_id, int offset_found, bool *reset_gesture); typedef struct accelState { int dimensions; @@ -143,4 +147,8 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *dis */ int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/moving_avg_ticker.h b/src/moving_avg_ticker.h index 7192eba..346a9a7 100644 --- a/src/moving_avg_ticker.h +++ b/src/moving_avg_ticker.h @@ -1,6 +1,10 @@ #ifndef ACCEL_AVG_TICKER #define ACCEL_AVG_TICKER +#ifdef __cplusplus +extern "C" { +#endif + #include "accel.h" #define MOVING_AVG_PARAM_ERROR ACCEL_PARAM_ERROR @@ -28,4 +32,8 @@ int get_latest_frame_moving_avg(moving_avg_values *value, int *frame); int free_moving_avg(moving_avg_values **value); +#ifdef __cplusplus +} +#endif + #endif diff --git a/src/pebble_makeup.h b/src/pebble_makeup.h index 5b4d194..ee92618 100644 --- a/src/pebble_makeup.h +++ b/src/pebble_makeup.h @@ -1,6 +1,10 @@ #ifndef ACCEL_PEBBLE_SUPPLEMENTARY #define ACCEL_PEBBLE_SUPPLEMENTARY +#ifdef __cplusplus +extern "C" { +#endif + #include #include #include @@ -35,4 +39,8 @@ void *my_calloc(size_t num, size_t size) { return allocd; } +#ifdef __cplusplus +} +#endif + #endif diff --git a/test/accel_test.cc b/test/accel_test.cc deleted file mode 100644 index e46445a..0000000 --- a/test/accel_test.cc +++ /dev/null @@ -1,459 +0,0 @@ -#include "gtest/gtest.h" - -#include "../src/accel.h" - -#include "callback_util.h" - -const void *VOID_NULL = NULL; - -accel_state *test_fabricate_state_with_callback(int dimensions, accel_callback callback, const int threshold) { - accel_state *state = NULL; - int result = accel_generate_state(&state, dimensions, 1, callback, threshold); - EXPECT_EQ(ACCEL_SUCCESS, result); - if (ACCEL_SUCCESS != result) { - int *myNull = NULL; - myNull = 0; - } - EXPECT_NE(VOID_NULL, state); - if (VOID_NULL == state) { - int *myNull = NULL; - myNull = 0; - } - return state; -} - -accel_state *test_fabricate_1d_state_with_callback(accel_callback callback, const int threshold) { - return test_fabricate_state_with_callback(1, callback, threshold); -} - -accel_state *test_fabricate_3d_state_with_callback(accel_callback callback, const int threshold) { - return test_fabricate_state_with_callback(3, callback, threshold); -} - -accel_state *test_fabricate_state(int dimensions) { return test_fabricate_state_with_callback(dimensions, NULL, 0); } - -accel_state *test_fabricate_1d_state() { return test_fabricate_state(1); } - -accel_state *test_fabricate_3d_state() { return test_fabricate_state(3); } - -void test_burn_state(accel_state **state) { - int result = accel_destroy_state(state); - EXPECT_EQ(0, result); - EXPECT_EQ(VOID_NULL, *state); -} - -TEST(AccelFuzzTest, generate_state_null_state) { - int result = accel_generate_state(NULL, 3, 1, NULL, 0); - EXPECT_EQ(result, ACCEL_PARAM_ERROR); -} - -TEST(AccelFuzzTest, generate_state_negative_or_zero_dimensions) { - accel_state *state = NULL; - // 0 dimensions must fail - int result = accel_generate_state(&state, 0, 1, NULL, 0); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - // -1 dimension must fail - result = accel_generate_state(&state, -1, 1, NULL, 0); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - // 1 dimension must succeed. - state = NULL; - result = accel_generate_state(&state, 1, 1, NULL, 0); - EXPECT_EQ(0, result); - // TODO: result's memory is leaked :s -} - -TEST(AccelFuzzTest, generate_state_invalid_threshold_with_callback_params) { - accel_state *state = NULL; - accel_callback nonNullCallback = (accel_callback)1; - - // Fails for negatives. - int result = accel_generate_state(&state, 1, 1, nonNullCallback, -1); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - // Fails for zero. - result = accel_generate_state(&state, 1, 1, nonNullCallback, 0); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - // Succeeds for 1. - result = accel_generate_state(&state, 1, 1, nonNullCallback, 1); - EXPECT_EQ(ACCEL_SUCCESS, result); -} - -TEST(AccelFuzzTest, generate_state_invalid_window_size) { - accel_state *state = NULL; - int result = 0; - - // Size 0 must fail - result = accel_generate_state(&state, 1, 0, NULL, 0); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - // Size -1 must fail - result = accel_generate_state(&state, 1, -1, NULL, 0); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - // Size 1 must succeed - result = accel_generate_state(&state, 1, 1, NULL, 0); - EXPECT_EQ(0, result); - EXPECT_NE(VOID_NULL, state); -} - -TEST(AccelFuzzTest, generate_state_threshold_fuzzing) { - accel_state *state = NULL; - - // Threshold of 1 with a non-null callback succeeds - EXPECT_EQ(NULL, state); - EXPECT_EQ(ACCEL_SUCCESS, accel_generate_state(&state, 1, 1, (accel_callback)0x1, 1)); - accel_destroy_state(&state); - - // Threshold of 1 with a null callback fails - EXPECT_EQ(NULL, state); - EXPECT_EQ(ACCEL_PARAM_ERROR, accel_generate_state(&state, 1, 1, (accel_callback)NULL, 1)); - - // Threshold of 0 with a null callback succeeds - EXPECT_EQ(NULL, state); - EXPECT_EQ(ACCEL_SUCCESS, accel_generate_state(&state, 1, 1, (accel_callback)NULL, 0)); - accel_destroy_state(&state); - - // Threshold of 0 with a non-null callback fails - EXPECT_EQ(NULL, state); - EXPECT_EQ(ACCEL_PARAM_ERROR, accel_generate_state(&state, 1, 1, (accel_callback)0x1, 0)); -} - -TEST(AccelFuzzTest, accel_generate_state_null_callback) { - int result = 0; - accel_state *state = NULL; - - // Null callback must be successful - result = accel_generate_state(&state, 1, 1, NULL, 0); -} - -TEST_CALLBACK(const int, AccelFuzzTest, accel_generate_state_valid_callback, myTest, accel_state *state, int gesture_id, - int offset_found, bool *reset_gesture) -*reset_gesture = true; -return ACCEL_SUCCESS; -} - -TEST(AccelFuzzTest, accel_generate_state_valid_callback) { - int gesture_id = 0; - accel_state *state = NULL; - - // Non-null callback, watch it iterate over this stuff. - state = test_fabricate_1d_state_with_callback( - &TEST_CALLBACK_NAME(AccelFuzzTest, accel_generate_state_valid_callback, myTest), 10); - - EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); - - for (int i = 0; i < 100; ++i) { - int data[1] = {i}; - EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); - } - EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture_id)); - - for (int i = 0; i < 100; ++i) { - int data[1] = {i}; - EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); - } - EXPECT_GT(TEST_CALLBACK_COUNTER(AccelFuzzTest, accel_generate_state_valid_callback, myTest), 0); -} - -TEST(AccelFuzzTest, accel_destroy_state_invalid_input) { - int result = 0; - - // Destroying x (x = NULL) will fail. - result = accel_destroy_state(NULL); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - // Destroying x (*x = NULL) will fail. - accel_state *state = NULL; - result = accel_destroy_state(&state); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - // Destroying x (*x != NULL) should succeed. - state = test_fabricate_1d_state(); - - // Destroy the state - result = accel_destroy_state(&state); - EXPECT_EQ(0, result); - EXPECT_EQ(VOID_NULL, state); -} - -TEST(AccelFuzzTest, accel_start_record_gesture_invalid_input) { - int result = 0; - int gesture_id = 0; - result = accel_start_record_gesture(NULL, &gesture_id); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - accel_state *state = test_fabricate_1d_state(); - - result = accel_start_record_gesture(state, NULL); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - test_burn_state(&state); -} - -TEST(AccelFuzzTest, accel_end_record_gesture_invalid_input) { - int result = 0; - accel_state *state = NULL; - - // Null state: - result = accel_end_record_gesture(NULL, 1); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - // Negative index: - state = test_fabricate_1d_state(); - result = accel_end_record_gesture(state, -1); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - test_burn_state(&state); - - // Unused index: - state = test_fabricate_1d_state(); - result = accel_end_record_gesture(state, 1); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - test_burn_state(&state); - - // Verify it works for valid indexes. - state = test_fabricate_1d_state(); - int gesture = 1234; - result = accel_start_record_gesture(state, &gesture); - EXPECT_NE(1234, gesture); - EXPECT_EQ(0, result); - int data[1] = {1}; - EXPECT_EQ(0, accel_process_timer_tick(state, data)); - - result = accel_end_record_gesture(state, gesture); - EXPECT_EQ(0, result) << "gesture " << gesture << " couldn't be recorded correctly" << std::endl; - test_burn_state(&state); -} - -TEST(AccelFuzzTest, accel_process_timer_tick_invalid_input) { - int result = 0; - int accel_data = 0; - accel_state *state = NULL; - - // Null state value. - result = accel_process_timer_tick(NULL, &accel_data); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - // Null array input. - state = test_fabricate_1d_state(); - result = accel_process_timer_tick(state, NULL); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - test_burn_state(&state); -} - -TEST(AccelFuzzTest, accel_find_most_likely_gesture_invalid_input) { - int result = 0; - int gesture_id = 0; - int affinity = 0; - accel_state *state = NULL; - - // Null state: - result = accel_find_most_likely_gesture(NULL, &gesture_id, &affinity); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - - // Null gesture id is passed in. - state = test_fabricate_1d_state(); - result = accel_find_most_likely_gesture(state, NULL, &affinity); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - test_burn_state(&state); - - // Null affinity is passed in. - state = test_fabricate_1d_state(); - result = accel_find_most_likely_gesture(state, &gesture_id, NULL); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - test_burn_state(&state); - - // No tests exist, but otherwise valid parameters. - state = test_fabricate_1d_state(); - result = accel_find_most_likely_gesture(state, &gesture_id, &affinity); - EXPECT_EQ(ACCEL_NO_VALID_GESTURE, gesture_id); - EXPECT_EQ(ACCEL_NO_VALID_GESTURE, affinity); - EXPECT_EQ(ACCEL_NO_VALID_GESTURE, result); -} - -TEST(AccelTest, accel_generate_and_destroy) { - accel_state *state = NULL; - for (int i = 1; i < 10; ++i) { - EXPECT_EQ(VOID_NULL, state) << "i = " << i; - EXPECT_EQ(0, accel_generate_state(&state, 2 * i, i, NULL, 0)) << "i = " << i; - EXPECT_EQ(0, accel_destroy_state(&state)) << "i = " << i; - EXPECT_EQ(VOID_NULL, state) << "i = " << i; - } -} - -TEST(AccelTest, start_recording_and_close_many_gestures) { - accel_state *state = NULL; - state = test_fabricate_1d_state(); - - int data[1] = {0}; - for (int i = 0; i < 10; ++i) { - int gesture = 0; - ASSERT_EQ(0, accel_start_record_gesture(state, &gesture)); - ASSERT_EQ(i, gesture); - ASSERT_EQ(0, accel_process_timer_tick(state, data)); - } - for (int i = 0; i < 10; ++i) { - ASSERT_EQ(0, accel_end_record_gesture(state, i)); - } - test_burn_state(&state); -} - -TEST(AccelTest, record_incredibly_long_sequence) { - accel_state *state = NULL; - state = test_fabricate_1d_state(); - - int gesture = 0; - EXPECT_EQ(0, accel_start_record_gesture(state, &gesture)); - EXPECT_EQ(0, gesture); - - int data[] = {1}; - for (int i = 0; i < 10000; ++i) { - EXPECT_EQ(0, accel_process_timer_tick(state, data)); - } - - EXPECT_EQ(0, accel_end_record_gesture(state, gesture)); - test_burn_state(&state); -} - -TEST(AccelTest, end_to_end_test_single_recording) { - accel_state *state = NULL; - state = test_fabricate_1d_state(); - - int gesture = 0; - EXPECT_EQ(0, accel_start_record_gesture(state, &gesture)); - EXPECT_EQ(0, gesture); - - int data[] = {1}; - for (int i = 0; i < 10; ++i) { - data[0] = i * 100; - EXPECT_EQ(0, accel_process_timer_tick(state, data)); - } - - EXPECT_EQ(0, accel_end_record_gesture(state, gesture)); - - int prev_affinity = 0; - for (int i = 0; i < 10; ++i) { - data[0] = i * 100; - int gesture_found = 1; - int affinity_of_gesture = 1; - ASSERT_EQ(0, accel_process_timer_tick(state, data)); - ASSERT_EQ(0, accel_find_most_likely_gesture(state, &gesture_found, &affinity_of_gesture)); - ASSERT_EQ(gesture, gesture_found); - if (i != 0) { - ASSERT_LT(affinity_of_gesture, prev_affinity) << "i=" << i; - } - prev_affinity = affinity_of_gesture; - } - - test_burn_state(&state); -} - -TEST(AccelTest, end_to_end_test_multiple_recordings) { - // g_1(x) = x, g_2(x) = x*x. Sample data is f(x) = 2x, we want to verify that g_1 is chosen over g_2. - accel_state *state = NULL; - state = test_fabricate_1d_state(); - - int first_gesture = 0; - EXPECT_EQ(0, accel_start_record_gesture(state, &first_gesture)); - EXPECT_EQ(0, first_gesture); - - int data[] = {1}; - for (int i = 0; i < 10; ++i) { - data[0] = i; - EXPECT_EQ(0, accel_process_timer_tick(state, data)); - } - - EXPECT_EQ(0, accel_end_record_gesture(state, first_gesture)); - - int second_gesture = 0; - EXPECT_EQ(0, accel_start_record_gesture(state, &second_gesture)); - EXPECT_NE(first_gesture, second_gesture); - - for (int i = 0; i < 10; ++i) { - data[0] = i * i; - EXPECT_EQ(0, accel_process_timer_tick(state, data)); - } - - EXPECT_EQ(0, accel_end_record_gesture(state, second_gesture)); - - int prev_affinity = 0; - for (int i = 0; i < 10; ++i) { - data[0] = i * 2; - int gesture_found = 1; - int affinity_of_gesture = 1; - ASSERT_EQ(0, accel_process_timer_tick(state, data)); - ASSERT_EQ(0, accel_find_most_likely_gesture(state, &gesture_found, &affinity_of_gesture)); - ASSERT_EQ(first_gesture, gesture_found); - prev_affinity = affinity_of_gesture; - } - - test_burn_state(&state); -} - -TEST(AccelTest, test_fuzz_reset_affinities) { - accel_state *state = NULL; - - // Null accel states. - EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(NULL, 0)); - - // No recorded accelerations - state = test_fabricate_1d_state(); - EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, 0)); - - int gesture_id = 0; - EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); - - // A recording gesture with no data. - EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); - - int data[1] = {0}; - EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); - EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); - EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); - - // A recording gesture with some data. - EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); - - EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture_id)); - - // No ticks have been recorded. - EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); - - int found_gesture = 1; - int found_distance = 1; - EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); - EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &found_gesture, &found_distance)); - - int after_reset_gesture = 1; - int after_reset_distance = 1; - EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); - EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &after_reset_gesture, &after_reset_distance)); - - EXPECT_NE(found_distance, after_reset_distance); - - test_burn_state(&state); -} - -TEST(AccelVersionTest, 1_0_0_to_1_1_0_a) { - EXPECT_GT(ACCEL_VERSION_GEN(1, 1, 0, false, true), ACCEL_VERSION_GEN(1, 0, 0, true, false)); -} - -int main(int argc, char **argv) { - ::testing::InitGoogleTest(&argc, argv); - - int returnValue; - - // Do whatever setup here you will need for your tests here - // - // - - returnValue = RUN_ALL_TESTS(); - - // Do Your teardown here if required - // - // - - return returnValue; -} diff --git a/test/moving_avg_ticker_test.cc b/test/main.cc similarity index 59% rename from test/moving_avg_ticker_test.cc rename to test/main.cc index b3b939d..f1d6f08 100644 --- a/test/moving_avg_ticker_test.cc +++ b/test/main.cc @@ -1,8 +1,210 @@ #include "gtest/gtest.h" #include "../src/moving_avg_ticker.h" +#include "../src/accel.h" + +// TODO: use accel_test_util.h +#include "callback_util.h" +#include "util.h" + +accel_state *test_fabricate_state_with_callback(int dimensions, accel_callback callback, const int threshold) { + accel_state *state = NULL; + int result = accel_generate_state(&state, dimensions, 1, callback, threshold); + EXPECT_EQ(ACCEL_SUCCESS, result); + if (ACCEL_SUCCESS != result) { + int *myNull = NULL; + myNull = 0; + } + EXPECT_NE(VOID_NULL, state); + if (VOID_NULL == state) { + int *myNull = NULL; + myNull = 0; + } + return state; +} + +accel_state *test_fabricate_1d_state_with_callback(accel_callback callback, const int threshold) { + return test_fabricate_state_with_callback(1, callback, threshold); +} + +accel_state *test_fabricate_3d_state_with_callback(accel_callback callback, const int threshold) { + return test_fabricate_state_with_callback(3, callback, threshold); +} + +accel_state *test_fabricate_state(int dimensions) { return test_fabricate_state_with_callback(dimensions, NULL, 0); } + +accel_state *test_fabricate_1d_state() { return test_fabricate_state(1); } + +accel_state *test_fabricate_3d_state() { return test_fabricate_state(3); } + +void test_burn_state(accel_state **state) { + int result = accel_destroy_state(state); + EXPECT_EQ(0, result); + EXPECT_EQ(VOID_NULL, *state); +} + +TEST(AccelTest, accel_generate_and_destroy) { + accel_state *state = NULL; + for (int i = 1; i < 10; ++i) { + EXPECT_EQ(VOID_NULL, state) << "i = " << i; + EXPECT_EQ(0, accel_generate_state(&state, 2 * i, i, NULL, 0)) << "i = " << i; + EXPECT_EQ(0, accel_destroy_state(&state)) << "i = " << i; + EXPECT_EQ(VOID_NULL, state) << "i = " << i; + } +} + +TEST(AccelTest, start_recording_and_close_many_gestures) { + accel_state *state = NULL; + state = test_fabricate_1d_state(); + + int data[1] = {0}; + for (int i = 0; i < 10; ++i) { + int gesture = 0; + ASSERT_EQ(0, accel_start_record_gesture(state, &gesture)); + ASSERT_EQ(i, gesture); + ASSERT_EQ(0, accel_process_timer_tick(state, data)); + } + for (int i = 0; i < 10; ++i) { + ASSERT_EQ(0, accel_end_record_gesture(state, i)); + } + test_burn_state(&state); +} + +TEST(AccelTest, record_incredibly_long_sequence) { + accel_state *state = NULL; + state = test_fabricate_1d_state(); + + int gesture = 0; + EXPECT_EQ(0, accel_start_record_gesture(state, &gesture)); + EXPECT_EQ(0, gesture); + + int data[] = {1}; + for (int i = 0; i < 10000; ++i) { + EXPECT_EQ(0, accel_process_timer_tick(state, data)); + } + + EXPECT_EQ(0, accel_end_record_gesture(state, gesture)); + test_burn_state(&state); +} + +TEST(AccelTest, end_to_end_test_single_recording) { + accel_state *state = NULL; + state = test_fabricate_1d_state(); + + int gesture = 0; + EXPECT_EQ(0, accel_start_record_gesture(state, &gesture)); + EXPECT_EQ(0, gesture); + + int data[] = {1}; + for (int i = 0; i < 10; ++i) { + data[0] = i * 100; + EXPECT_EQ(0, accel_process_timer_tick(state, data)); + } + + EXPECT_EQ(0, accel_end_record_gesture(state, gesture)); + + int prev_affinity = 0; + for (int i = 0; i < 10; ++i) { + data[0] = i * 100; + int gesture_found = 1; + int affinity_of_gesture = 1; + ASSERT_EQ(0, accel_process_timer_tick(state, data)); + ASSERT_EQ(0, accel_find_most_likely_gesture(state, &gesture_found, &affinity_of_gesture)); + ASSERT_EQ(gesture, gesture_found); + if (i != 0) { + ASSERT_LT(affinity_of_gesture, prev_affinity) << "i=" << i; + } + prev_affinity = affinity_of_gesture; + } + + test_burn_state(&state); +} + +TEST(AccelTest, end_to_end_test_multiple_recordings) { + // g_1(x) = x, g_2(x) = x*x. Sample data is f(x) = 2x, we want to verify that g_1 is chosen over g_2. + accel_state *state = NULL; + state = test_fabricate_1d_state(); + + int first_gesture = 0; + EXPECT_EQ(0, accel_start_record_gesture(state, &first_gesture)); + EXPECT_EQ(0, first_gesture); + + int data[] = {1}; + for (int i = 0; i < 10; ++i) { + data[0] = i; + EXPECT_EQ(0, accel_process_timer_tick(state, data)); + } + + EXPECT_EQ(0, accel_end_record_gesture(state, first_gesture)); + + int second_gesture = 0; + EXPECT_EQ(0, accel_start_record_gesture(state, &second_gesture)); + EXPECT_NE(first_gesture, second_gesture); + + for (int i = 0; i < 10; ++i) { + data[0] = i * i; + EXPECT_EQ(0, accel_process_timer_tick(state, data)); + } + + EXPECT_EQ(0, accel_end_record_gesture(state, second_gesture)); + + int prev_affinity = 0; + for (int i = 0; i < 10; ++i) { + data[0] = i * 2; + int gesture_found = 1; + int affinity_of_gesture = 1; + ASSERT_EQ(0, accel_process_timer_tick(state, data)); + ASSERT_EQ(0, accel_find_most_likely_gesture(state, &gesture_found, &affinity_of_gesture)); + ASSERT_EQ(first_gesture, gesture_found); + prev_affinity = affinity_of_gesture; + } + + test_burn_state(&state); +} + +TEST(AccelTest, test_fuzz_reset_affinities) { + accel_state *state = NULL; + + // Null accel states. + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(NULL, 0)); + + // No recorded accelerations + state = test_fabricate_1d_state(); + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, 0)); + + int gesture_id = 0; + EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); -const void *void_null = NULL; + // A recording gesture with no data. + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); + + int data[1] = {0}; + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + + // A recording gesture with some data. + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); + + EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture_id)); + + // No ticks have been recorded. + EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); + + int found_gesture = 1; + int found_distance = 1; + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &found_gesture, &found_distance)); + + int after_reset_gesture = 1; + int after_reset_distance = 1; + EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); + EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &after_reset_gesture, &after_reset_distance)); + + EXPECT_NE(found_distance, after_reset_distance); + + test_burn_state(&state); +} TEST(MovingAvgTicker, InvalidInputValues) { diff --git a/test/util.h b/test/util.h new file mode 100644 index 0000000..3abaf73 --- /dev/null +++ b/test/util.h @@ -0,0 +1,8 @@ +#ifndef TEST_UTILS +#define TEST_UTILS + +// TODO: make these two the same. +const void *void_null = NULL; +const void *VOID_NULL = NULL; + +#endif From c77c5d514f93d357579a557ee77f5247d898ac75 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 01:25:21 -0700 Subject: [PATCH 27/68] Updated with new and improved makefile --- Makefile | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ test/Makefile | 28 ---------------------------- 2 files changed, 49 insertions(+), 28 deletions(-) create mode 100644 Makefile delete mode 100644 test/Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d17bbe5 --- /dev/null +++ b/Makefile @@ -0,0 +1,49 @@ +# TODO: update this. +PHONY = default echo/objects echo/sources list tests run bin clean + +SOURCES_C = $(wildcard src/*.c) +TEST_CC = $(wildcard test/*.cc) + +_SOURCES_OBJECTS_TMP = ${SOURCES_C:.c=.o} +_TEST_OBJECTS_TMP = ${TEST_CC:.cc=.o} +SOURCES_OBJECTS = $(subst src,bin,$(_SOURCES_OBJECTS_TMP)) +TEST_OBJECTS = $(subst test,bin,$(_TEST_OBJECTS_TMP)) + +EXEC = tests + +default: + @make all + +echo/objects: + @echo ${SOURCES_OBJECTS} + @echo ${TEST_OBJECTS} + +echo/sources: + @echo ${SOURCES_C} + @echo ${TEST_CC} + +list: + @echo $(PHONY) + +tests: bin/tests + +bin/tests: ${SOURCES_OBJECTS} ${TEST_OBJECTS} bin/libgtest.a + clang++ -Ilib/gtest-1.7.0/include ${SOURCES_OBJECTS} ${TEST_OBJECTS} bin/libgtest.a -o bin/tests + +run: tests + ./bin/tests + +bin/%.o : src/%.c + clang -DIS_NOT_PEBBLE -c $< -o $@ + +bin/%.o : test/%.cc + clang++ -DIS_NOT_PEBBLE -Ilib/gtest-1.7.0/include -c $< -o $@ + +bin/libgtest.a: bin + clang++ -Ilib/gtest-1.7.0/include -Ilib/gtest-1.7.0 -c lib/gtest-1.7.0/src/gtest-all.cc -lpthread -o bin/libgtest.a + +bin: + mkdir -p bin + +clean: + rm -rf bin diff --git a/test/Makefile b/test/Makefile deleted file mode 100644 index d28644e..0000000 --- a/test/Makefile +++ /dev/null @@ -1,28 +0,0 @@ -PHONY = default tests libgtest.a tests clean list - -default: - @make all - -list: - echo $(PHONY) - -# TODO: use variables and stuff, this is super messy. - -tests: moving_avg_ticker_test accel_test - -moving_avg_ticker_test: libgtest.a moving_avg_ticker_test.cc ../src/moving_avg_ticker.h ../src/moving_avg_ticker.c - @clang++ -DIS_NOT_PEBBLE -I../lib/gtest-1.7.0/include -pthread moving_avg_ticker_test.cc ../src/moving_avg_ticker.c libgtest.a -o moving_avg_ticker_test -Wall - -accel_test: libgtest.a accel_test.cc ../src - @clang++ -DIS_NOT_PEBBLE -I../lib/gtest-1.7.0/include -pthread accel_test.cc ../src/accel.c ../src/moving_avg_ticker.c libgtest.a -o accel_test -Wall - -libgtest.a: - clang++ -I../lib/gtest-1.7.0/include -I../lib/gtest-1.7.0 -c ../lib/gtest-1.7.0/src/gtest-all.cc -lpthread - ar -rv libgtest.a gtest-all.o - -run: moving_avg_ticker_test accel_test - ./moving_avg_ticker_test - ./accel_test - -clean: - rm -f libgtest.a gtest-all.o moving_avg_ticker_test accel_test From a0185448f27bf049a7796fb091d32caa479b4831 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 01:25:40 -0700 Subject: [PATCH 28/68] Updated bin folder to makefile --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 9fa3b1b..30950b2 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,6 @@ *.exe *.out *.app + +# Built folder(s) +bin From 40837b15f60deab17bdbfb58c61d5499f0573b5c Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 01:51:15 -0700 Subject: [PATCH 29/68] makefiles are no longer in the test env. --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 6f726d7..acec3e4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ compiler: - gcc - clang script: - - cd test/ - make run branches: only: From 73130a356c49da2eb387e5285ad21d1da1eea838 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 01:51:39 -0700 Subject: [PATCH 30/68] Build all the branches! --- .travis.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index acec3e4..0706d95 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,10 +4,6 @@ compiler: - clang script: - make run -branches: - only: - - master - - 1.8.7 notifications: recipients: - shalecraig@gmail.com From 94d393407f2dcd5566b5cd02e3f2bd7243b742c9 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 02:03:42 -0700 Subject: [PATCH 31/68] Somehow, this wasn't working. I've fixed it. The reset affinities wasn't implemented either :( That needs to be fixed --- src/accel.c | 8 +++---- src/accel.h | 2 +- test/main.cc | 62 ++++++++++++++++++++++++++-------------------------- 3 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/accel.c b/src/accel.c index 8ef5d46..cd18361 100644 --- a/src/accel.c +++ b/src/accel.c @@ -341,16 +341,16 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { gesture->is_recorded = true; } - for (int i=0; irecording_size; ++i) { - gesture->affinities[i] = INT16_MAX; + for (int i = 0; i < gesture->recording_size; ++i) { + gesture->offsets[i] = INT16_MAX; } - for (int d=0; ddimensions; ++d) { + for (int d = 0; d < state->dimensions; ++d) { reset_moving_avg(gesture->moving_avg_values[d]); } return ACCEL_SUCCESS; } -// TODO: check for malloc failure in this function. +// TODO: gracefully handle malloc failure in this function. // TODO: this should return error types instead of being void. // Follow-up: find usages of this method. void handle_recording_tick(accel_gesture *gesture, int dimensions) { diff --git a/src/accel.h b/src/accel.h index 578fb8e..ea79b37 100644 --- a/src/accel.h +++ b/src/accel.h @@ -145,7 +145,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *dis * @param gesture_id Value that corresponds to a gesture currently being reset. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); +// int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); #ifdef __cplusplus } diff --git a/test/main.cc b/test/main.cc index f1d6f08..c7d130a 100644 --- a/test/main.cc +++ b/test/main.cc @@ -162,49 +162,49 @@ TEST(AccelTest, end_to_end_test_multiple_recordings) { test_burn_state(&state); } -TEST(AccelTest, test_fuzz_reset_affinities) { - accel_state *state = NULL; +// TEST(AccelTest, test_fuzz_reset_affinities) { +// accel_state *state = NULL; - // Null accel states. - EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(NULL, 0)); +// // Null accel states. +// EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(NULL, 0)); - // No recorded accelerations - state = test_fabricate_1d_state(); - EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, 0)); +// // No recorded accelerations +// state = test_fabricate_1d_state(); +// EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, 0)); - int gesture_id = 0; - EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); +// int gesture_id = 0; +// EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); - // A recording gesture with no data. - EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); +// // A recording gesture with no data. +// EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); - int data[1] = {0}; - EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); - EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); - EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); +// int data[1] = {0}; +// EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); +// EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); +// EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); - // A recording gesture with some data. - EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); +// // A recording gesture with some data. +// EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); - EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture_id)); +// EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture_id)); - // No ticks have been recorded. - EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); +// // No ticks have been recorded. +// EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); - int found_gesture = 1; - int found_distance = 1; - EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); - EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &found_gesture, &found_distance)); +// int found_gesture = 1; +// int found_distance = 1; +// EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); +// EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &found_gesture, &found_distance)); - int after_reset_gesture = 1; - int after_reset_distance = 1; - EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); - EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &after_reset_gesture, &after_reset_distance)); +// int after_reset_gesture = 1; +// int after_reset_distance = 1; +// EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); +// EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &after_reset_gesture, &after_reset_distance)); - EXPECT_NE(found_distance, after_reset_distance); +// EXPECT_NE(found_distance, after_reset_distance); - test_burn_state(&state); -} +// test_burn_state(&state); +// } TEST(MovingAvgTicker, InvalidInputValues) { From df4b8a7471fcd669f999b05e722adaad9cd57802 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 02:06:26 -0700 Subject: [PATCH 32/68] Build at every commit. Takes <4s, why not? --- s/hooks/auto/pre-commit/build.sh | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100755 s/hooks/auto/pre-commit/build.sh diff --git a/s/hooks/auto/pre-commit/build.sh b/s/hooks/auto/pre-commit/build.sh new file mode 100755 index 0000000..074170e --- /dev/null +++ b/s/hooks/auto/pre-commit/build.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -e + +pushd $(git rev-parse --show-toplevel || echo ".") + +make run + +popd From fb568330bd0c1f0ee5a2d4e828b7c464a9aac3c9 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 02:10:24 -0700 Subject: [PATCH 33/68] Everything that builds a bin requires it. --- Makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index d17bbe5..8de3bdc 100644 --- a/Makefile +++ b/Makefile @@ -27,16 +27,16 @@ list: tests: bin/tests -bin/tests: ${SOURCES_OBJECTS} ${TEST_OBJECTS} bin/libgtest.a +bin/tests: ${SOURCES_OBJECTS} ${TEST_OBJECTS} bin/libgtest.a bin clang++ -Ilib/gtest-1.7.0/include ${SOURCES_OBJECTS} ${TEST_OBJECTS} bin/libgtest.a -o bin/tests run: tests ./bin/tests -bin/%.o : src/%.c +bin/%.o : src/%.c bin clang -DIS_NOT_PEBBLE -c $< -o $@ -bin/%.o : test/%.cc +bin/%.o : test/%.cc bin clang++ -DIS_NOT_PEBBLE -Ilib/gtest-1.7.0/include -c $< -o $@ bin/libgtest.a: bin From 9bd1194f68917290d303fa6bc368205c20cf10a0 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 02:22:22 -0700 Subject: [PATCH 34/68] Removed useless constant modifier --- src/accel.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/accel.h b/src/accel.h index ea79b37..f642230 100644 --- a/src/accel.h +++ b/src/accel.h @@ -58,7 +58,7 @@ struct accelState; * refer to the ACCEL_MIN_RESERVED definition inside * their implementations. */ -typedef const int (*accel_callback)(struct accelState *state, int gesture_id, int offset_found, bool *reset_gesture); +typedef int (*accel_callback)(struct accelState *state, int gesture_id, int offset_found, bool *reset_gesture); typedef struct accelState { int dimensions; From c2c254923781908a745f59547ad0399066249aee Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 02:47:47 -0700 Subject: [PATCH 35/68] Updated the accel to be more conformant to c spec --- src/accel.c | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/src/accel.c b/src/accel.c index cd18361..69a9d7c 100644 --- a/src/accel.c +++ b/src/accel.c @@ -44,36 +44,36 @@ typedef struct internalAccelState { accel_gesture **gestures; } internal_accel_state; -#define PRECONDITION_NOT_NULL(foo_$) \ - if (foo_$ == NULL) { \ +#define PRECONDITION_NOT_NULL(INPUT) \ + if (INPUT == NULL) { \ return ACCEL_PARAM_ERROR; \ } -#define PRECONDITION_NULL(foo_$) \ - if (foo_$ != NULL) { \ +#define PRECONDITION_NULL(INPUT) \ + if (INPUT != NULL) { \ return ACCEL_PARAM_ERROR; \ } -#define PRECONDITION_VALID_STATE(state_$) \ - if (state_$ == NULL) { \ +#define PRECONDITION_VALID_STATE(INPUT_STATE) \ + if (INPUT_STATE == NULL) { \ return ACCEL_PARAM_ERROR; \ } \ - if (state_$->state == NULL) { \ + if (INPUT_STATE->state == NULL) { \ return ACCEL_INTERNAL_ERROR; \ } \ - if (state_$->dimensions <= 0) { \ + if (INPUT_STATE->dimensions <= 0) { \ return ACCEL_INTERNAL_ERROR; \ } \ - if (state_$->state->window_size <= 0) { \ + if (INPUT_STATE->state->window_size <= 0) { \ return ACCEL_INTERNAL_ERROR; \ } \ - if (state_$->state->num_gestures_saved < 0) { \ + if (INPUT_STATE->state->num_gestures_saved < 0) { \ return ACCEL_INTERNAL_ERROR; \ } \ - if (state_$->state->gestures == NULL && state_$->state->num_gestures_saved != 0) { \ + if (INPUT_STATE->state->gestures == NULL && INPUT_STATE->state->num_gestures_saved != 0) { \ return ACCEL_INTERNAL_ERROR; \ } \ - if (state_$->state->gestures != NULL && state_$->state->num_gestures_saved == 0) { \ + if (INPUT_STATE->state->gestures != NULL && INPUT_STATE->state->num_gestures_saved == 0) { \ return ACCEL_INTERNAL_ERROR; \ } @@ -82,18 +82,8 @@ typedef struct internalAccelState { #define ALPHA 1.0 // TODO: include these from a header file? -#define MAX(a, b) \ - ({ \ - __typeof__(a) _a = (a); \ - __typeof__(b) _b = (b); \ - _a > _b ? _a : _b; \ - }) -#define MIN(a, b) \ - ({ \ - __typeof__(a) _a = (a); \ - __typeof__(b) _b = (b); \ - _a < _b ? _a : _b; \ - }) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +// #define MAX(a,b) (((a)>(b))?(a):(b)) void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { if (gesture == NULL || *gesture == NULL) { From 1ab4e6de01ca9a6b0a244a6beae4558418056259 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 02:49:20 -0700 Subject: [PATCH 36/68] Added C and CXX formalized arguments. --- Makefile | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 8de3bdc..54b074d 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,11 @@ TEST_OBJECTS = $(subst test,bin,$(_TEST_OBJECTS_TMP)) EXEC = tests +# removed: -Wl,-z,relro -Wl,-z,now +C_ARGS = -DIS_NOT_PEBBLE -pipe -m64 -ansi -fPIC -g -O3 -fno-exceptions -fstack-protector -fvisibility=hidden -W -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wformat -Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -Werror -Wcomment -Wtrigraphs -Wundef -Wunused-macros -pedantic-errors -std=c99 +# removed: -lpthread, -Wl,-z,relro -Wl,-z,now +CXX_ARGS = -DIS_NOT_PEBBLE -lpthread -pipe -m64 -ansi -fPIC -g -O3 -fno-exceptions -fstack-protector -fvisibility=hidden -W -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wformat -Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -Wall + default: @make all @@ -34,13 +39,13 @@ run: tests ./bin/tests bin/%.o : src/%.c bin - clang -DIS_NOT_PEBBLE -c $< -o $@ + clang ${C_ARGS} -c $< -o $@ bin/%.o : test/%.cc bin - clang++ -DIS_NOT_PEBBLE -Ilib/gtest-1.7.0/include -c $< -o $@ + clang++ ${CXX_ARGS} -Ilib/gtest-1.7.0/include -c $< -o $@ bin/libgtest.a: bin - clang++ -Ilib/gtest-1.7.0/include -Ilib/gtest-1.7.0 -c lib/gtest-1.7.0/src/gtest-all.cc -lpthread -o bin/libgtest.a + clang++ -Ilib/gtest-1.7.0/include -Ilib/gtest-1.7.0 -c lib/gtest-1.7.0/src/gtest-all.cc -o bin/libgtest.a bin: mkdir -p bin From 18d99ad2741bf46416e51c932580a81cf818779f Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 02:52:15 -0700 Subject: [PATCH 37/68] Modified the way we require pthreads Most travis needed it, so I guess I'm building for someone (or something) else --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 54b074d..ca10bbc 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ list: tests: bin/tests bin/tests: ${SOURCES_OBJECTS} ${TEST_OBJECTS} bin/libgtest.a bin - clang++ -Ilib/gtest-1.7.0/include ${SOURCES_OBJECTS} ${TEST_OBJECTS} bin/libgtest.a -o bin/tests + clang++ -Ilib/gtest-1.7.0/include ${SOURCES_OBJECTS} ${TEST_OBJECTS} bin/libgtest.a -lpthread -o bin/tests run: tests ./bin/tests @@ -42,7 +42,7 @@ bin/%.o : src/%.c bin clang ${C_ARGS} -c $< -o $@ bin/%.o : test/%.cc bin - clang++ ${CXX_ARGS} -Ilib/gtest-1.7.0/include -c $< -o $@ + clang++ -Ilib/gtest-1.7.0/include ${CXX_ARGS} -c $< -o $@ bin/libgtest.a: bin clang++ -Ilib/gtest-1.7.0/include -Ilib/gtest-1.7.0 -c lib/gtest-1.7.0/src/gtest-all.cc -o bin/libgtest.a From 10e654cb6c3d5d35f500293a4c6c87bf5b58906f Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 03:23:00 -0700 Subject: [PATCH 38/68] Updated the demo code. This should've been done a while ago, to keep the two in sync. --- sample/simple-accelerometer/src/accel.c | 65 +++++++------------ sample/simple-accelerometer/src/accel.h | 14 +++- .../src/moving_avg_ticker.h | 8 +++ .../simple-accelerometer/src/pebble_makeup.h | 8 +++ 4 files changed, 49 insertions(+), 46 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index 4b87932..69a9d7c 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -44,36 +44,36 @@ typedef struct internalAccelState { accel_gesture **gestures; } internal_accel_state; -#define PRECONDITION_NOT_NULL(foo_$) \ - if (foo_$ == NULL) { \ +#define PRECONDITION_NOT_NULL(INPUT) \ + if (INPUT == NULL) { \ return ACCEL_PARAM_ERROR; \ } -#define PRECONDITION_NULL(foo_$) \ - if (foo_$ != NULL) { \ +#define PRECONDITION_NULL(INPUT) \ + if (INPUT != NULL) { \ return ACCEL_PARAM_ERROR; \ } -#define PRECONDITION_VALID_STATE(state_$) \ - if (state_$ == NULL) { \ +#define PRECONDITION_VALID_STATE(INPUT_STATE) \ + if (INPUT_STATE == NULL) { \ return ACCEL_PARAM_ERROR; \ } \ - if (state_$->state == NULL) { \ + if (INPUT_STATE->state == NULL) { \ return ACCEL_INTERNAL_ERROR; \ } \ - if (state_$->dimensions <= 0) { \ + if (INPUT_STATE->dimensions <= 0) { \ return ACCEL_INTERNAL_ERROR; \ } \ - if (state_$->state->window_size <= 0) { \ + if (INPUT_STATE->state->window_size <= 0) { \ return ACCEL_INTERNAL_ERROR; \ } \ - if (state_$->state->num_gestures_saved < 0) { \ + if (INPUT_STATE->state->num_gestures_saved < 0) { \ return ACCEL_INTERNAL_ERROR; \ } \ - if (state_$->state->gestures == NULL && state_$->state->num_gestures_saved != 0) { \ + if (INPUT_STATE->state->gestures == NULL && INPUT_STATE->state->num_gestures_saved != 0) { \ return ACCEL_INTERNAL_ERROR; \ } \ - if (state_$->state->gestures != NULL && state_$->state->num_gestures_saved == 0) { \ + if (INPUT_STATE->state->gestures != NULL && INPUT_STATE->state->num_gestures_saved == 0) { \ return ACCEL_INTERNAL_ERROR; \ } @@ -82,18 +82,8 @@ typedef struct internalAccelState { #define ALPHA 1.0 // TODO: include these from a header file? -#define MAX(a, b) \ - ({ \ - __typeof__(a) _a = (a); \ - __typeof__(b) _b = (b); \ - _a > _b ? _a : _b; \ - }) -#define MIN(a, b) \ - ({ \ - __typeof__(a) _a = (a); \ - __typeof__(b) _b = (b); \ - _a < _b ? _a : _b; \ - }) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +// #define MAX(a,b) (((a)>(b))?(a):(b)) void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { if (gesture == NULL || *gesture == NULL) { @@ -341,10 +331,16 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { gesture->is_recorded = true; } - return reset_result; + for (int i = 0; i < gesture->recording_size; ++i) { + gesture->offsets[i] = INT16_MAX; + } + for (int d = 0; d < state->dimensions; ++d) { + reset_moving_avg(gesture->moving_avg_values[d]); + } + return ACCEL_SUCCESS; } -// TODO: check for malloc failure in this function. +// TODO: gracefully handle malloc failure in this function. // TODO: this should return error types instead of being void. // Follow-up: find usages of this method. void handle_recording_tick(accel_gesture *gesture, int dimensions) { @@ -542,20 +538,3 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off } return ACCEL_SUCCESS; } - -int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id) { - PRECONDITION_VALID_STATE(state); - internal_accel_state *ias = state->state; - if (ias->num_gestures_saved <= gesture_id || gesture_id < 0) { - return ACCEL_PARAM_ERROR; - } - accel_gesture *gest = ias->gestures[gesture_id]; - if (gest == NULL) { - return ACCEL_INTERNAL_ERROR; - } - if (!gest->is_recorded || gest->is_recording) { - // Gesture is in the wrong state for resetting. - return ACCEL_PARAM_ERROR; - } - return reset_gesture(gest, state->dimensions); -} diff --git a/sample/simple-accelerometer/src/accel.h b/sample/simple-accelerometer/src/accel.h index 7df4445..f642230 100644 --- a/sample/simple-accelerometer/src/accel.h +++ b/sample/simple-accelerometer/src/accel.h @@ -1,6 +1,10 @@ #ifndef ACCEL_H #define ACCEL_H +#ifdef __cplusplus +extern "C" { +#endif + #include #define ACCEL_SUCCESS 0 @@ -12,7 +16,7 @@ #define ACCEL_VERSION_GEN(major, minor, point, isBeta, isAlpha) \ (4 * (100 * ((100 * major) + minor) + point) + 3 - (isAlpha ? 2 : 0) - (isBeta ? 1 : 0)) -#define ACCEL_VERSION_CODE ACCEL_VERSION_GEN(1, 0, 0, true, false) +#define ACCEL_VERSION_CODE ACCEL_VERSION_GEN(1, 1, 0, false, true) struct internalAccelState; struct accelState; @@ -54,7 +58,7 @@ struct accelState; * refer to the ACCEL_MIN_RESERVED definition inside * their implementations. */ -typedef const int (*accel_callback)(accelState *state, int gesture_id, int offset_found, bool *reset_gesture); +typedef int (*accel_callback)(struct accelState *state, int gesture_id, int offset_found, bool *reset_gesture); typedef struct accelState { int dimensions; @@ -141,6 +145,10 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *dis * @param gesture_id Value that corresponds to a gesture currently being reset. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); +// int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); + +#ifdef __cplusplus +} +#endif #endif diff --git a/sample/simple-accelerometer/src/moving_avg_ticker.h b/sample/simple-accelerometer/src/moving_avg_ticker.h index 7192eba..346a9a7 100644 --- a/sample/simple-accelerometer/src/moving_avg_ticker.h +++ b/sample/simple-accelerometer/src/moving_avg_ticker.h @@ -1,6 +1,10 @@ #ifndef ACCEL_AVG_TICKER #define ACCEL_AVG_TICKER +#ifdef __cplusplus +extern "C" { +#endif + #include "accel.h" #define MOVING_AVG_PARAM_ERROR ACCEL_PARAM_ERROR @@ -28,4 +32,8 @@ int get_latest_frame_moving_avg(moving_avg_values *value, int *frame); int free_moving_avg(moving_avg_values **value); +#ifdef __cplusplus +} +#endif + #endif diff --git a/sample/simple-accelerometer/src/pebble_makeup.h b/sample/simple-accelerometer/src/pebble_makeup.h index 5b4d194..ee92618 100644 --- a/sample/simple-accelerometer/src/pebble_makeup.h +++ b/sample/simple-accelerometer/src/pebble_makeup.h @@ -1,6 +1,10 @@ #ifndef ACCEL_PEBBLE_SUPPLEMENTARY #define ACCEL_PEBBLE_SUPPLEMENTARY +#ifdef __cplusplus +extern "C" { +#endif + #include #include #include @@ -35,4 +39,8 @@ void *my_calloc(size_t num, size_t size) { return allocd; } +#ifdef __cplusplus +} +#endif + #endif From 11bed500f8c307f06c1f43e6602b681e83437182 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Fri, 21 Mar 2014 03:24:13 -0700 Subject: [PATCH 39/68] Added commit hook to keep the demo in sync --- s/hooks/auto/pre-commit/update_demo.sh | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100755 s/hooks/auto/pre-commit/update_demo.sh diff --git a/s/hooks/auto/pre-commit/update_demo.sh b/s/hooks/auto/pre-commit/update_demo.sh new file mode 100755 index 0000000..b47453b --- /dev/null +++ b/s/hooks/auto/pre-commit/update_demo.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -e + +pushd $(git rev-parse --show-toplevel || echo ".") + +cp src/* sample/simple-accelerometer/src/ + +popd From fc54eb5af42a8944e16e488abd3d3eb14d248324 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 22 Mar 2014 00:22:04 -0700 Subject: [PATCH 40/68] Implemented accel_reset_affinities_for_gesture --- sample/simple-accelerometer/src/accel.c | 20 ++++++++ sample/simple-accelerometer/src/accel.h | 2 +- src/accel.c | 20 ++++++++ src/accel.h | 2 +- test/main.cc | 62 ++++++++++++------------- 5 files changed, 73 insertions(+), 33 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index 69a9d7c..742464a 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -77,6 +77,13 @@ typedef struct internalAccelState { return ACCEL_INTERNAL_ERROR; \ } +#define PRECONDITION_TRUE_PARAM(TRUE_COND) \ + { \ + if (!TRUE_COND) { \ + return ACCEL_PARAM_ERROR; \ + } \ + } + // Decay rate of values we choose to keep. 1.0 is no decay, 2.0 is a doubling every time we keep them. // TODO: should we store the offsets as floats instead? #define ALPHA 1.0 @@ -538,3 +545,16 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off } return ACCEL_SUCCESS; } + +int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id) { + PRECONDITION_VALID_STATE(state); + PRECONDITION_NOT_NULL(state->state); + PRECONDITION_TRUE_PARAM((state->state->num_gestures_saved > gesture_id)); + + accel_gesture *gesture = state->state->gestures[gesture_id]; + + PRECONDITION_TRUE_PARAM(!gesture->is_recording); + PRECONDITION_TRUE_PARAM(gesture->is_recorded); + + return reset_gesture(gesture, state->dimensions); +} diff --git a/sample/simple-accelerometer/src/accel.h b/sample/simple-accelerometer/src/accel.h index f642230..eda1110 100644 --- a/sample/simple-accelerometer/src/accel.h +++ b/sample/simple-accelerometer/src/accel.h @@ -145,7 +145,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *dis * @param gesture_id Value that corresponds to a gesture currently being reset. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -// int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); +int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); #ifdef __cplusplus } diff --git a/src/accel.c b/src/accel.c index 69a9d7c..742464a 100644 --- a/src/accel.c +++ b/src/accel.c @@ -77,6 +77,13 @@ typedef struct internalAccelState { return ACCEL_INTERNAL_ERROR; \ } +#define PRECONDITION_TRUE_PARAM(TRUE_COND) \ + { \ + if (!TRUE_COND) { \ + return ACCEL_PARAM_ERROR; \ + } \ + } + // Decay rate of values we choose to keep. 1.0 is no decay, 2.0 is a doubling every time we keep them. // TODO: should we store the offsets as floats instead? #define ALPHA 1.0 @@ -538,3 +545,16 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off } return ACCEL_SUCCESS; } + +int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id) { + PRECONDITION_VALID_STATE(state); + PRECONDITION_NOT_NULL(state->state); + PRECONDITION_TRUE_PARAM((state->state->num_gestures_saved > gesture_id)); + + accel_gesture *gesture = state->state->gestures[gesture_id]; + + PRECONDITION_TRUE_PARAM(!gesture->is_recording); + PRECONDITION_TRUE_PARAM(gesture->is_recorded); + + return reset_gesture(gesture, state->dimensions); +} diff --git a/src/accel.h b/src/accel.h index f642230..eda1110 100644 --- a/src/accel.h +++ b/src/accel.h @@ -145,7 +145,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *dis * @param gesture_id Value that corresponds to a gesture currently being reset. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -// int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); +int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); #ifdef __cplusplus } diff --git a/test/main.cc b/test/main.cc index c7d130a..f1d6f08 100644 --- a/test/main.cc +++ b/test/main.cc @@ -162,49 +162,49 @@ TEST(AccelTest, end_to_end_test_multiple_recordings) { test_burn_state(&state); } -// TEST(AccelTest, test_fuzz_reset_affinities) { -// accel_state *state = NULL; +TEST(AccelTest, test_fuzz_reset_affinities) { + accel_state *state = NULL; -// // Null accel states. -// EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(NULL, 0)); + // Null accel states. + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(NULL, 0)); -// // No recorded accelerations -// state = test_fabricate_1d_state(); -// EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, 0)); + // No recorded accelerations + state = test_fabricate_1d_state(); + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, 0)); -// int gesture_id = 0; -// EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); + int gesture_id = 0; + EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); -// // A recording gesture with no data. -// EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); + // A recording gesture with no data. + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); -// int data[1] = {0}; -// EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); -// EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); -// EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + int data[1] = {0}; + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); -// // A recording gesture with some data. -// EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); + // A recording gesture with some data. + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, gesture_id)); -// EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture_id)); + EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture_id)); -// // No ticks have been recorded. -// EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); + // No ticks have been recorded. + EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); -// int found_gesture = 1; -// int found_distance = 1; -// EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); -// EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &found_gesture, &found_distance)); + int found_gesture = 1; + int found_distance = 1; + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &found_gesture, &found_distance)); -// int after_reset_gesture = 1; -// int after_reset_distance = 1; -// EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); -// EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &after_reset_gesture, &after_reset_distance)); + int after_reset_gesture = 1; + int after_reset_distance = 1; + EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); + EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &after_reset_gesture, &after_reset_distance)); -// EXPECT_NE(found_distance, after_reset_distance); + EXPECT_NE(found_distance, after_reset_distance); -// test_burn_state(&state); -// } + test_burn_state(&state); +} TEST(MovingAvgTicker, InvalidInputValues) { From 158cf20f41402d0e564f02bc7b2a333fb446b37e Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 22 Mar 2014 10:10:46 -0700 Subject: [PATCH 41/68] Upped the unit test to be more comprehensive --- test/main.cc | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/test/main.cc b/test/main.cc index f1d6f08..9ad86d3 100644 --- a/test/main.cc +++ b/test/main.cc @@ -191,17 +191,25 @@ TEST(AccelTest, test_fuzz_reset_affinities) { // No ticks have been recorded. EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); - int found_gesture = 1; - int found_distance = 1; + int gesture = 1; + int initial_distance = 1; + int after_run_distance = 1; + int after_reset_distance = 1; + + EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &gesture, &initial_distance)); + + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); - EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &found_gesture, &found_distance)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &gesture, &after_run_distance)); - int after_reset_gesture = 1; - int after_reset_distance = 1; EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); - EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &after_reset_gesture, &after_reset_distance)); + EXPECT_EQ(ACCEL_SUCCESS, accel_find_most_likely_gesture(state, &gesture, &after_reset_distance)); - EXPECT_NE(found_distance, after_reset_distance); + EXPECT_EQ(initial_distance, after_reset_distance); + EXPECT_GE(after_reset_distance, after_run_distance); test_burn_state(&state); } From d3c070be47b7dbbc35009aaad6b58a20cb0c5a33 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 22 Mar 2014 10:11:31 -0700 Subject: [PATCH 42/68] Always clean the directory when committing --- s/hooks/auto/pre-commit/build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/s/hooks/auto/pre-commit/build.sh b/s/hooks/auto/pre-commit/build.sh index 074170e..bc80873 100755 --- a/s/hooks/auto/pre-commit/build.sh +++ b/s/hooks/auto/pre-commit/build.sh @@ -3,6 +3,6 @@ set -e pushd $(git rev-parse --show-toplevel || echo ".") -make run +make clean run popd From 3b6cd77832a63323dd0b1ef98b0bdfc2a0525867 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 22 Mar 2014 10:42:36 -0700 Subject: [PATCH 43/68] Added tests that were somehow missed in transition :( --- test/callback_util.h | 2 +- test/main.cc | 231 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+), 1 deletion(-) diff --git a/test/callback_util.h b/test/callback_util.h index b57cb94..758a7f2 100644 --- a/test/callback_util.h +++ b/test/callback_util.h @@ -11,7 +11,7 @@ #define TEST_CALLBACK(returnSignature, testClass, testName, uniqueIdentifier, args...) \ /* count the number of times the callback was invoked. */ \ - uint generated_##testClass##_##testName##_##uniqueIdentifier##_counter = 0; \ + uint32_t generated_##testClass##_##testName##_##uniqueIdentifier##_counter = 0; \ /* define a method that proxies calls to the real method. */ \ returnSignature generated_##testClass##_##testName##_##uniqueIdentifier##_name(args) { \ /* increment the counter */ \ diff --git a/test/main.cc b/test/main.cc index c7d130a..0a12e88 100644 --- a/test/main.cc +++ b/test/main.cc @@ -43,6 +43,237 @@ void test_burn_state(accel_state **state) { EXPECT_EQ(VOID_NULL, *state); } +TEST(AccelFuzzTest, generate_state_null_state) { + int result = accel_generate_state(NULL, 3, 1, NULL, 0); + EXPECT_EQ(result, ACCEL_PARAM_ERROR); +} + +TEST(AccelFuzzTest, generate_state_negative_or_zero_dimensions) { + accel_state *state = NULL; + // 0 dimensions must fail + int result = accel_generate_state(&state, 0, 1, NULL, 0); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // -1 dimension must fail + result = accel_generate_state(&state, -1, 1, NULL, 0); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // 1 dimension must succeed. + state = NULL; + result = accel_generate_state(&state, 1, 1, NULL, 0); + EXPECT_EQ(0, result); + // TODO: result's memory is leaked :s +} + +TEST(AccelFuzzTest, generate_state_invalid_threshold_with_callback_params) { + accel_state *state = NULL; + accel_callback nonNullCallback = (accel_callback)1; + + // Fails for negatives. + int result = accel_generate_state(&state, 1, 1, nonNullCallback, -1); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // Fails for zero. + result = accel_generate_state(&state, 1, 1, nonNullCallback, 0); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // Succeeds for 1. + result = accel_generate_state(&state, 1, 1, nonNullCallback, 1); + EXPECT_EQ(ACCEL_SUCCESS, result); +} + +TEST(AccelFuzzTest, generate_state_invalid_window_size) { + accel_state *state = NULL; + int result = 0; + + // Size 0 must fail + result = accel_generate_state(&state, 1, 0, NULL, 0); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // Size -1 must fail + result = accel_generate_state(&state, 1, -1, NULL, 0); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // Size 1 must succeed + result = accel_generate_state(&state, 1, 1, NULL, 0); + EXPECT_EQ(0, result); + EXPECT_NE(VOID_NULL, state); +} + +TEST(AccelFuzzTest, generate_state_threshold_fuzzing) { + accel_state *state = NULL; + + // Threshold of 1 with a non-null callback succeeds + EXPECT_EQ(NULL, state); + EXPECT_EQ(ACCEL_SUCCESS, accel_generate_state(&state, 1, 1, (accel_callback)0x1, 1)); + accel_destroy_state(&state); + + // Threshold of 1 with a null callback fails + EXPECT_EQ(NULL, state); + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_generate_state(&state, 1, 1, (accel_callback)NULL, 1)); + + // Threshold of 0 with a null callback succeeds + EXPECT_EQ(NULL, state); + EXPECT_EQ(ACCEL_SUCCESS, accel_generate_state(&state, 1, 1, (accel_callback)NULL, 0)); + accel_destroy_state(&state); + + // Threshold of 0 with a non-null callback fails + EXPECT_EQ(NULL, state); + EXPECT_EQ(ACCEL_PARAM_ERROR, accel_generate_state(&state, 1, 1, (accel_callback)0x1, 0)); +} + +TEST(AccelFuzzTest, accel_generate_state_null_callback) { + int result = 0; + accel_state *state = NULL; + + // Null callback must be successful + result = accel_generate_state(&state, 1, 1, NULL, 0); +} + +TEST_CALLBACK(int, AccelFuzzTest, accel_generate_state_valid_callback, myTest, accel_state *state, int gesture_id, + int offset_found, bool *reset_gesture) +*reset_gesture = true; +return ACCEL_SUCCESS; +} + +TEST(AccelFuzzTest, accel_generate_state_valid_callback) { + int gesture_id = 0; + accel_state *state = NULL; + + // Non-null callback, watch it iterate over this stuff. + state = test_fabricate_1d_state_with_callback( + &TEST_CALLBACK_NAME(AccelFuzzTest, accel_generate_state_valid_callback, myTest), 10); + + EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); + + for (int i = 0; i < 100; ++i) { + int data[1] = {i}; + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + } + EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture_id)); + + for (int i = 0; i < 100; ++i) { + int data[1] = {i}; + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); + } + EXPECT_GT(TEST_CALLBACK_COUNTER(AccelFuzzTest, accel_generate_state_valid_callback, myTest), (uint32_t)0); +} + +TEST(AccelFuzzTest, accel_destroy_state_invalid_input) { + int result = 0; + + // Destroying x (x = NULL) will fail. + result = accel_destroy_state(NULL); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // Destroying x (*x = NULL) will fail. + accel_state *state = NULL; + result = accel_destroy_state(&state); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // Destroying x (*x != NULL) should succeed. + state = test_fabricate_1d_state(); + + // Destroy the state + result = accel_destroy_state(&state); + EXPECT_EQ(0, result); + EXPECT_EQ(VOID_NULL, state); +} + +TEST(AccelFuzzTest, accel_start_record_gesture_invalid_input) { + int result = 0; + int gesture_id = 0; + result = accel_start_record_gesture(NULL, &gesture_id); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + accel_state *state = test_fabricate_1d_state(); + + result = accel_start_record_gesture(state, NULL); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + test_burn_state(&state); +} + +TEST(AccelFuzzTest, accel_end_record_gesture_invalid_input) { + int result = 0; + accel_state *state = NULL; + + // Null state: + result = accel_end_record_gesture(NULL, 1); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // Negative index: + state = test_fabricate_1d_state(); + result = accel_end_record_gesture(state, -1); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + test_burn_state(&state); + + // Unused index: + state = test_fabricate_1d_state(); + result = accel_end_record_gesture(state, 1); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + test_burn_state(&state); + + // Verify it works for valid indexes. + state = test_fabricate_1d_state(); + int gesture = 1234; + result = accel_start_record_gesture(state, &gesture); + EXPECT_NE(1234, gesture); + EXPECT_EQ(0, result); + int data[1] = {1}; + EXPECT_EQ(0, accel_process_timer_tick(state, data)); + + result = accel_end_record_gesture(state, gesture); + EXPECT_EQ(0, result) << "gesture " << gesture << " couldn't be recorded correctly" << std::endl; + test_burn_state(&state); +} + +TEST(AccelFuzzTest, accel_process_timer_tick_invalid_input) { + int result = 0; + int accel_data = 0; + accel_state *state = NULL; + + // Null state value. + result = accel_process_timer_tick(NULL, &accel_data); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // Null array input. + state = test_fabricate_1d_state(); + result = accel_process_timer_tick(state, NULL); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + test_burn_state(&state); +} + +TEST(AccelFuzzTest, accel_find_most_likely_gesture_invalid_input) { + int result = 0; + int gesture_id = 0; + int affinity = 0; + accel_state *state = NULL; + + // Null state: + result = accel_find_most_likely_gesture(NULL, &gesture_id, &affinity); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + + // Null gesture id is passed in. + state = test_fabricate_1d_state(); + result = accel_find_most_likely_gesture(state, NULL, &affinity); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + test_burn_state(&state); + + // Null affinity is passed in. + state = test_fabricate_1d_state(); + result = accel_find_most_likely_gesture(state, &gesture_id, NULL); + EXPECT_EQ(ACCEL_PARAM_ERROR, result); + test_burn_state(&state); + + // No tests exist, but otherwise valid parameters. + state = test_fabricate_1d_state(); + result = accel_find_most_likely_gesture(state, &gesture_id, &affinity); + EXPECT_EQ(ACCEL_NO_VALID_GESTURE, gesture_id); + EXPECT_EQ(ACCEL_NO_VALID_GESTURE, affinity); + EXPECT_EQ(ACCEL_NO_VALID_GESTURE, result); +} + TEST(AccelTest, accel_generate_and_destroy) { accel_state *state = NULL; for (int i = 1; i < 10; ++i) { From d5d3873b5475d774d6fbf1fb88945812e50a2140 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 22 Mar 2014 10:51:22 -0700 Subject: [PATCH 44/68] Removed introduced TODO --- test/main.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/main.cc b/test/main.cc index 0a12e88..9476a7d 100644 --- a/test/main.cc +++ b/test/main.cc @@ -62,7 +62,7 @@ TEST(AccelFuzzTest, generate_state_negative_or_zero_dimensions) { state = NULL; result = accel_generate_state(&state, 1, 1, NULL, 0); EXPECT_EQ(0, result); - // TODO: result's memory is leaked :s + accel_destroy_state(&state); } TEST(AccelFuzzTest, generate_state_invalid_threshold_with_callback_params) { From 9568e8fc21bb0c637286a619eac219acd3114392 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 22 Mar 2014 11:04:27 -0700 Subject: [PATCH 45/68] Added uint32_t import --- test/callback_util.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/callback_util.h b/test/callback_util.h index 758a7f2..8d7f830 100644 --- a/test/callback_util.h +++ b/test/callback_util.h @@ -1,6 +1,18 @@ #ifndef TEST_CALLBACK_UTIL_H #define TEST_CALLBACK_UTIL_H +#ifndef IS_NOT_PEBBLE +#ifndef PEBBLE +#define PEBBLE +#endif +#endif + +#ifdef IS_NOT_PEBBLE +#include +#else +#error // no support yet. +#endif + // TODO: this is so hack. #define TEST_CALLBACK_NAME(testClass, testName, uniqueIdentifier) \ From 9b082779f70acbd4c7c12a21010e26b156ac71c2 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 22 Mar 2014 10:52:35 -0700 Subject: [PATCH 46/68] void_null -> VOID_NULL --- test/main.cc | 34 +++++++++++++++++----------------- test/util.h | 2 -- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/test/main.cc b/test/main.cc index 9476a7d..a588bb0 100644 --- a/test/main.cc +++ b/test/main.cc @@ -459,19 +459,19 @@ TEST(MovingAvgTicker, InvalidInputValues) { TEST(MovingAvgTicker, AllocatesAndFreesCorrectly) { moving_avg_values *allocated = NULL; int retval = allocate_moving_avg(1, 1, &allocated); - EXPECT_NE(void_null, allocated); - EXPECT_NE(void_null, allocated->wbuf); + EXPECT_NE(VOID_NULL, allocated); + EXPECT_NE(VOID_NULL, allocated->wbuf); EXPECT_EQ(retval, 0); retval = free_moving_avg(&allocated); EXPECT_EQ(0, retval); - EXPECT_EQ(void_null, allocated); + EXPECT_EQ(VOID_NULL, allocated); } TEST(MovingAvgTicker, ResetsCorrectly) { moving_avg_values *allocated = NULL; int retval = allocate_moving_avg(1, 1, &allocated); - EXPECT_NE(void_null, allocated); + EXPECT_NE(VOID_NULL, allocated); EXPECT_EQ(0, retval); retval = reset_moving_avg(NULL); @@ -484,7 +484,7 @@ TEST(MovingAvgTicker, ResetsCorrectly) { TEST(MovingAvgTicker, AppendsCorrectly1_1) { moving_avg_values *allocated = NULL; int retval = allocate_moving_avg(1, 1, &allocated); - EXPECT_NE(void_null, allocated); + EXPECT_NE(VOID_NULL, allocated); EXPECT_EQ(0, retval); bool is_at_end = false; @@ -511,7 +511,7 @@ TEST(MovingAvgTicker, AppendsCorrectly1_1) { TEST(MovingAvgTicker, AppendsCorrectly2_1) { moving_avg_values *allocated = NULL; int retval = allocate_moving_avg(2, 1, &allocated); - EXPECT_NE(void_null, allocated); + EXPECT_NE(VOID_NULL, allocated); EXPECT_EQ(0, retval); bool is_at_end = false; @@ -548,7 +548,7 @@ TEST(MovingAvgTicker, AppendsCorrectly2_1) { TEST(MovingAvgTicker, AppendsCorrectly1_2) { moving_avg_values *allocated = NULL; int retval = allocate_moving_avg(1, 2, &allocated); - EXPECT_NE(void_null, allocated); + EXPECT_NE(VOID_NULL, allocated); EXPECT_EQ(0, retval); bool is_at_end = false; @@ -583,7 +583,7 @@ TEST(MovingAvgTicker, AppendsCorrectly1_2) { TEST(MovingAvgTicker, AppendsCorrectly2_2) { moving_avg_values *allocated = NULL; int retval = allocate_moving_avg(2, 2, &allocated); - EXPECT_NE(void_null, allocated); + EXPECT_NE(VOID_NULL, allocated); EXPECT_EQ(0, retval); bool is_at_end = false; @@ -616,7 +616,7 @@ TEST(MovingAvgTicker, AppendsCorrectly2_2) { retval = free_moving_avg(&allocated); EXPECT_EQ(0, retval); - EXPECT_EQ(void_null, allocated); + EXPECT_EQ(VOID_NULL, allocated); } TEST(MovingAvgTicker, AppendToInvalid) { @@ -631,7 +631,7 @@ TEST(MovingAvgTicker, AppendWithInvalidAtEnd) { moving_avg_values *allocated = NULL; retval = allocate_moving_avg(2, 2, &allocated); EXPECT_EQ(0, retval); - EXPECT_NE(void_null, allocated); + EXPECT_NE(VOID_NULL, allocated); retval = append_to_moving_avg(allocated, 1, (bool *)NULL); EXPECT_EQ(MOVING_AVG_PARAM_ERROR, retval); @@ -648,7 +648,7 @@ TEST(MovingAvgTicker, InvalidLatestFrameParams) { moving_avg_values *allocated = NULL; retval = allocate_moving_avg(2, 2, &allocated); EXPECT_EQ(0, retval); - EXPECT_NE(void_null, allocated); + EXPECT_NE(VOID_NULL, allocated); retval = get_latest_frame_moving_avg(allocated, (int *)NULL); EXPECT_EQ(MOVING_AVG_PARAM_ERROR, retval); @@ -681,9 +681,9 @@ TEST(MovingAvgTickerFuzzTest, allocate_moving_avg) { // Test with success, to validate that there was only one difference between this and the above tests. allocated = NULL; EXPECT_EQ(0, allocate_moving_avg(1, 1, &allocated)); - EXPECT_NE(void_null, allocated); + EXPECT_NE(VOID_NULL, allocated); EXPECT_EQ(0, free_moving_avg(&allocated)); - EXPECT_EQ(void_null, allocated); + EXPECT_EQ(VOID_NULL, allocated); } TEST(MovingAvgTickerFuzzTest, reset_moving_avg) { @@ -741,18 +741,18 @@ TEST(MovingAvgTickerFuzzTest, free_moving_avg) { // Test with null wbuf EXPECT_EQ(0, allocate_moving_avg(1, 1, &allocated)); - EXPECT_NE(void_null, allocated); + EXPECT_NE(VOID_NULL, allocated); free(allocated->wbuf); allocated->wbuf = NULL; // TODO: successfully completes, even with invalid input. EXPECT_EQ(0, free_moving_avg(&allocated)); - EXPECT_EQ(void_null, allocated); + EXPECT_EQ(VOID_NULL, allocated); // Test normal path. EXPECT_EQ(0, allocate_moving_avg(1, 1, &allocated)); - EXPECT_NE(void_null, allocated); + EXPECT_NE(VOID_NULL, allocated); EXPECT_EQ(0, free_moving_avg(&allocated)); - EXPECT_EQ(void_null, allocated); + EXPECT_EQ(VOID_NULL, allocated); } int main(int argc, char **argv) { diff --git a/test/util.h b/test/util.h index 3abaf73..789a2ab 100644 --- a/test/util.h +++ b/test/util.h @@ -1,8 +1,6 @@ #ifndef TEST_UTILS #define TEST_UTILS -// TODO: make these two the same. -const void *void_null = NULL; const void *VOID_NULL = NULL; #endif From d0f545cf0ff7a824d1df6be4a6ffbd29a67c759e Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 22 Mar 2014 10:58:40 -0700 Subject: [PATCH 47/68] Cleaned up TODOs --- src/accel.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/accel.c b/src/accel.c index 69a9d7c..cb43f5c 100644 --- a/src/accel.c +++ b/src/accel.c @@ -155,7 +155,6 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { return ACCEL_SUCCESS; } -// TODO: needs direct testing with invalid objects. int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback, const int threshold) { PRECONDITION_NOT_NULL(state); @@ -361,7 +360,7 @@ void handle_recording_tick(accel_gesture *gesture, int dimensions) { gesture->normalized_recording[gesture->recording_size] = (int *)malloc(sizeof(int) * dimensions); for (int i = 0; i < dimensions; ++i) { // TODO: fix this int/float business. - // TODO: check resultant output. + // TODO: complain about invalid return values. get_latest_frame_moving_avg(gesture->moving_avg_values[i], &(gesture->normalized_recording[gesture->recording_size][i])); gesture->normalized_recording[gesture->recording_size][i] = @@ -509,7 +508,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off for (int i = 0; i < state->state->num_gestures_saved; ++i) { accel_gesture *gesture = state->state->gestures[i]; - // TODO: this should be tested. + // TODO: Should this be tested? if (gesture == NULL) { return ACCEL_INTERNAL_ERROR; } From 9e508f26c9265afb87681ebf6666956cd56d9879 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sat, 22 Mar 2014 11:03:58 -0700 Subject: [PATCH 48/68] Added the test file. --- sample/simple-accelerometer/src/accel.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index 69a9d7c..cb43f5c 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -155,7 +155,6 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { return ACCEL_SUCCESS; } -// TODO: needs direct testing with invalid objects. int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback, const int threshold) { PRECONDITION_NOT_NULL(state); @@ -361,7 +360,7 @@ void handle_recording_tick(accel_gesture *gesture, int dimensions) { gesture->normalized_recording[gesture->recording_size] = (int *)malloc(sizeof(int) * dimensions); for (int i = 0; i < dimensions; ++i) { // TODO: fix this int/float business. - // TODO: check resultant output. + // TODO: complain about invalid return values. get_latest_frame_moving_avg(gesture->moving_avg_values[i], &(gesture->normalized_recording[gesture->recording_size][i])); gesture->normalized_recording[gesture->recording_size][i] = @@ -509,7 +508,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off for (int i = 0; i < state->state->num_gestures_saved; ++i) { accel_gesture *gesture = state->state->gestures[i]; - // TODO: this should be tested. + // TODO: Should this be tested? if (gesture == NULL) { return ACCEL_INTERNAL_ERROR; } From 05f6fbfb07f1b26eeeec56b283fca70e76f1ff81 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sun, 23 Mar 2014 12:56:03 -0700 Subject: [PATCH 49/68] Started initial work on the moving_avg_ticker --- .../src/moving_avg_ticker.c | 21 ++++++++++--------- .../src/moving_avg_ticker.h | 1 + src/moving_avg_ticker.c | 21 ++++++++++--------- src/moving_avg_ticker.h | 1 + 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/sample/simple-accelerometer/src/moving_avg_ticker.c b/sample/simple-accelerometer/src/moving_avg_ticker.c index eb243d7..67e05b0 100644 --- a/sample/simple-accelerometer/src/moving_avg_ticker.c +++ b/sample/simple-accelerometer/src/moving_avg_ticker.c @@ -8,6 +8,7 @@ return MOVING_AVG_PARAM_ERROR; \ } +// TODO: make this into a macro. int precondition_valid_moving_avg_values(moving_avg_values *input) { PRECONDITION_NOT_NULL(input); @@ -29,7 +30,7 @@ int precondition_valid_moving_avg_values(moving_avg_values *input) { if (input->max_subtotal_size <= 0) { return MOVING_AVG_INTERNAL_ERROR; } - return 0; + return MOVING_AVG_SUCCESS; } int allocate_moving_avg(int num_wbuf, int subtotal_sizes, moving_avg_values **allocated) { @@ -62,12 +63,12 @@ int allocate_moving_avg(int num_wbuf, int subtotal_sizes, moving_avg_values **al } (*allocated)->wbuf = wbuf; (*allocated)->wbuf_len = num_wbuf; - return 0; + return MOVING_AVG_SUCCESS; } int reset_moving_avg(moving_avg_values *reset) { int value = precondition_valid_moving_avg_values(reset); - if (value != 0) { + if (value != MOVING_AVG_SUCCESS) { return value; } @@ -75,12 +76,12 @@ int reset_moving_avg(moving_avg_values *reset) { reset->wbuf_end = reset->wbuf_len - 1; reset->subtotal = 0; reset->subtotal_size = 0; - return 0; + return MOVING_AVG_SUCCESS; } int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end) { int is_valid_return_value = precondition_valid_moving_avg_values(value); - if (is_valid_return_value != 0) { + if (is_valid_return_value != MOVING_AVG_SUCCESS) { return is_valid_return_value; } @@ -90,7 +91,7 @@ int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end value->subtotal += appended; if (value->subtotal_size != value->max_subtotal_size) { *is_at_end = false; - return 0; + return MOVING_AVG_SUCCESS; } value->wbuf_end = (value->wbuf_end + 1) % value->wbuf_len; @@ -99,12 +100,12 @@ int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end value->subtotal = 0; value->subtotal_size = 0; *is_at_end = true; - return 0; + return MOVING_AVG_SUCCESS; } int get_latest_frame_moving_avg(moving_avg_values *value, int *frame) { int is_valid_return_value = precondition_valid_moving_avg_values(value); - if (is_valid_return_value != 0) { + if (is_valid_return_value != MOVING_AVG_SUCCESS) { return is_valid_return_value; } @@ -115,7 +116,7 @@ int get_latest_frame_moving_avg(moving_avg_values *value, int *frame) { sum += value->wbuf[i] * 1.0 / value->wbuf_len; } *frame = (int)sum; - return 0; + return MOVING_AVG_SUCCESS; } int free_moving_avg(moving_avg_values **value) { @@ -128,5 +129,5 @@ int free_moving_avg(moving_avg_values **value) { } free(*value); *value = NULL; - return 0; + return MOVING_AVG_SUCCESS; } diff --git a/sample/simple-accelerometer/src/moving_avg_ticker.h b/sample/simple-accelerometer/src/moving_avg_ticker.h index 346a9a7..5a4d06d 100644 --- a/sample/simple-accelerometer/src/moving_avg_ticker.h +++ b/sample/simple-accelerometer/src/moving_avg_ticker.h @@ -7,6 +7,7 @@ extern "C" { #include "accel.h" +#define MOVING_AVG_SUCCESS ACCEL_SUCCESS #define MOVING_AVG_PARAM_ERROR ACCEL_PARAM_ERROR #define MOVING_AVG_INTERNAL_ERROR ACCEL_INTERNAL_ERROR #define MOVING_AVG_MALLOC_ERROR ACCEL_MALLOC_ERROR diff --git a/src/moving_avg_ticker.c b/src/moving_avg_ticker.c index eb243d7..67e05b0 100644 --- a/src/moving_avg_ticker.c +++ b/src/moving_avg_ticker.c @@ -8,6 +8,7 @@ return MOVING_AVG_PARAM_ERROR; \ } +// TODO: make this into a macro. int precondition_valid_moving_avg_values(moving_avg_values *input) { PRECONDITION_NOT_NULL(input); @@ -29,7 +30,7 @@ int precondition_valid_moving_avg_values(moving_avg_values *input) { if (input->max_subtotal_size <= 0) { return MOVING_AVG_INTERNAL_ERROR; } - return 0; + return MOVING_AVG_SUCCESS; } int allocate_moving_avg(int num_wbuf, int subtotal_sizes, moving_avg_values **allocated) { @@ -62,12 +63,12 @@ int allocate_moving_avg(int num_wbuf, int subtotal_sizes, moving_avg_values **al } (*allocated)->wbuf = wbuf; (*allocated)->wbuf_len = num_wbuf; - return 0; + return MOVING_AVG_SUCCESS; } int reset_moving_avg(moving_avg_values *reset) { int value = precondition_valid_moving_avg_values(reset); - if (value != 0) { + if (value != MOVING_AVG_SUCCESS) { return value; } @@ -75,12 +76,12 @@ int reset_moving_avg(moving_avg_values *reset) { reset->wbuf_end = reset->wbuf_len - 1; reset->subtotal = 0; reset->subtotal_size = 0; - return 0; + return MOVING_AVG_SUCCESS; } int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end) { int is_valid_return_value = precondition_valid_moving_avg_values(value); - if (is_valid_return_value != 0) { + if (is_valid_return_value != MOVING_AVG_SUCCESS) { return is_valid_return_value; } @@ -90,7 +91,7 @@ int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end value->subtotal += appended; if (value->subtotal_size != value->max_subtotal_size) { *is_at_end = false; - return 0; + return MOVING_AVG_SUCCESS; } value->wbuf_end = (value->wbuf_end + 1) % value->wbuf_len; @@ -99,12 +100,12 @@ int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end value->subtotal = 0; value->subtotal_size = 0; *is_at_end = true; - return 0; + return MOVING_AVG_SUCCESS; } int get_latest_frame_moving_avg(moving_avg_values *value, int *frame) { int is_valid_return_value = precondition_valid_moving_avg_values(value); - if (is_valid_return_value != 0) { + if (is_valid_return_value != MOVING_AVG_SUCCESS) { return is_valid_return_value; } @@ -115,7 +116,7 @@ int get_latest_frame_moving_avg(moving_avg_values *value, int *frame) { sum += value->wbuf[i] * 1.0 / value->wbuf_len; } *frame = (int)sum; - return 0; + return MOVING_AVG_SUCCESS; } int free_moving_avg(moving_avg_values **value) { @@ -128,5 +129,5 @@ int free_moving_avg(moving_avg_values **value) { } free(*value); *value = NULL; - return 0; + return MOVING_AVG_SUCCESS; } diff --git a/src/moving_avg_ticker.h b/src/moving_avg_ticker.h index 346a9a7..5a4d06d 100644 --- a/src/moving_avg_ticker.h +++ b/src/moving_avg_ticker.h @@ -7,6 +7,7 @@ extern "C" { #include "accel.h" +#define MOVING_AVG_SUCCESS ACCEL_SUCCESS #define MOVING_AVG_PARAM_ERROR ACCEL_PARAM_ERROR #define MOVING_AVG_INTERNAL_ERROR ACCEL_INTERNAL_ERROR #define MOVING_AVG_MALLOC_ERROR ACCEL_MALLOC_ERROR From 4c82c3b4a25b810904355e929c2d8c449703418f Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sun, 23 Mar 2014 20:00:57 -0700 Subject: [PATCH 50/68] Fixed changes, 1.1.0 should be at a good state. --- sample/simple-accelerometer/src/simple-accelerometer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample/simple-accelerometer/src/simple-accelerometer.c b/sample/simple-accelerometer/src/simple-accelerometer.c index ab89790..d968f01 100644 --- a/sample/simple-accelerometer/src/simple-accelerometer.c +++ b/sample/simple-accelerometer/src/simple-accelerometer.c @@ -33,7 +33,7 @@ static void select_click_handler(ClickRecognizerRef recognizer, void *context) { static void up_click_handler(ClickRecognizerRef recognizer, void *context) { if (state == NULL) { - int result = accel_generate_state(&state, 3, 1); + int result = accel_generate_state(&state, 3, 1, NULL, 0); if (result == ACCEL_SUCCESS) { text_layer_set_text(text_layer, "Allocated Successfuly"); } else { From fa4276d9104eac2be525612e53829ccc22920e1b Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sun, 23 Mar 2014 20:08:07 -0700 Subject: [PATCH 51/68] Makefile now has more Flags, more PHONY --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index ca10bbc..5b42a62 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # TODO: update this. -PHONY = default echo/objects echo/sources list tests run bin clean +PHONY = default echo/objects echo/sources list tests bin/tests run bin clean SOURCES_C = $(wildcard src/*.c) TEST_CC = $(wildcard test/*.cc) @@ -12,7 +12,7 @@ TEST_OBJECTS = $(subst test,bin,$(_TEST_OBJECTS_TMP)) EXEC = tests # removed: -Wl,-z,relro -Wl,-z,now -C_ARGS = -DIS_NOT_PEBBLE -pipe -m64 -ansi -fPIC -g -O3 -fno-exceptions -fstack-protector -fvisibility=hidden -W -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wformat -Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -Werror -Wcomment -Wtrigraphs -Wundef -Wunused-macros -pedantic-errors -std=c99 +C_ARGS = -DIS_NOT_PEBBLE -DRELEASE -pipe -m64 -ansi -fPIC -fPIE -g -Os -ffunction-sections -fno-exceptions -fstack-protector-all -fvisibility=hidden -W -Wall -Wextra -Wunused-parameter -Wunused-function -Wunused-label -Wpointer-arith -Wformat -Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral -Winit-self -Wuninitialized -Wdeprecated -Wformat-security -Werror -Wcomment -Wtrigraphs -Wundef -Wunused-macros -pedantic-errors -std=c99 # removed: -lpthread, -Wl,-z,relro -Wl,-z,now CXX_ARGS = -DIS_NOT_PEBBLE -lpthread -pipe -m64 -ansi -fPIC -g -O3 -fno-exceptions -fstack-protector -fvisibility=hidden -W -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wformat -Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -Wall From 0da9bf0e81ee8072c8686d340aecc36b620d4bfb Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sun, 23 Mar 2014 20:12:11 -0700 Subject: [PATCH 52/68] Removed lock file. --- sample/simple-accelerometer/.lock-waf_linux2_build | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 sample/simple-accelerometer/.lock-waf_linux2_build diff --git a/sample/simple-accelerometer/.lock-waf_linux2_build b/sample/simple-accelerometer/.lock-waf_linux2_build deleted file mode 100644 index aed0a81..0000000 --- a/sample/simple-accelerometer/.lock-waf_linux2_build +++ /dev/null @@ -1,8 +0,0 @@ -argv = ['/home/vagrant/pebble-dev/PebbleSDK-2.0-BETA5/Pebble/waf', 'configure', 'build'] -environ = {'XDG_RUNTIME_DIR': '/run/user/1000', 'LESS': '-R', 'LC_CTYPE': 'en_US.UTF-8', 'SSH_CLIENT': '10.0.2.2 49994 22', 'LSCOLORS': 'Gxfxcxdxbxegedabagacad', 'LOGNAME': 'vagrant', 'USER': 'vagrant', 'PATH': '/home/vagrant/pebble-dev/PebbleSDK-2.0-BETA5/arm-cs-tools/bin:/home/vagrant/bin:/usr/local/bin:/home/vagrant/pebble-dev/PebbleSDK-2.0-BETA5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games', 'HOME': '/home/vagrant', 'XDG_SESSION_ID': '11', '_': '/home/vagrant/pebble-dev/PebbleSDK-2.0-BETA5/bin/pebble', 'LANG': 'en_US.UTF-8', 'TERM': 'xterm-256color', 'SHELL': '/bin/zsh', 'XDG_SESSION_COOKIE': '16ccae8f6a53e04feed02d6f52cb75dd-1390072954.343424-1847087206', 'SHLVL': '1', 'SSH_TTY': '/dev/pts/1', 'OLDPWD': '/home/vagrant', 'PWD': '/vagrant/mount/code/accel/sample/simple-accelerometer', 'GREP_OPTIONS': '--color=auto', 'MAIL': '/var/mail/vagrant', 'GREP_COLOR': '1;32', 'SSH_CONNECTION': '10.0.2.2 49994 10.0.2.15 22', 'PAGER': 'less'} -files = ['/vagrant/mount/code/accel/sample/simple-accelerometer/wscript'] -hash = -326433459 -options = {'files': '', 'jobs': 1, 'verbose': 0, 'nocache': False, 'progress_bar': 0, 'timestamp': None, 'distcheck_args': None, 'top': '', 'destdir': '', 'keep': 0, 'zones': '', 'debug': False, 'prefix': '/usr/local/', 'download': False, 'force': False, 'targets': '', 'out': ''} -out_dir = '/vagrant/mount/code/accel/sample/simple-accelerometer/build' -run_dir = '/vagrant/mount/code/accel/sample/simple-accelerometer' -top_dir = '/vagrant/mount/code/accel/sample/simple-accelerometer' From 0c436d6bba06b94ecb388908d139073a165fb85b Mon Sep 17 00:00:00 2001 From: shalecraig Date: Sun, 23 Mar 2014 20:23:19 -0700 Subject: [PATCH 53/68] Ignore lock files --- sample/simple-accelerometer/.gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/sample/simple-accelerometer/.gitignore b/sample/simple-accelerometer/.gitignore index 9ed9d4f..1ce1856 100644 --- a/sample/simple-accelerometer/.gitignore +++ b/sample/simple-accelerometer/.gitignore @@ -1,3 +1,5 @@ # Ignore build generated files build + +.lock* From b676dc30104c280871c79fe040d4ee8b988b0bc7 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Mon, 24 Mar 2014 00:34:46 -0700 Subject: [PATCH 54/68] Check against #define, not a inline number. --- test/main.cc | 140 +++++++++++++++++++++++++-------------------------- 1 file changed, 70 insertions(+), 70 deletions(-) diff --git a/test/main.cc b/test/main.cc index 903abe3..bb3edda 100644 --- a/test/main.cc +++ b/test/main.cc @@ -39,7 +39,7 @@ accel_state *test_fabricate_3d_state() { return test_fabricate_state(3); } void test_burn_state(accel_state **state) { int result = accel_destroy_state(state); - EXPECT_EQ(0, result); + EXPECT_EQ(ACCEL_SUCCESS, result); EXPECT_EQ(VOID_NULL, *state); } @@ -61,7 +61,7 @@ TEST(AccelFuzzTest, generate_state_negative_or_zero_dimensions) { // 1 dimension must succeed. state = NULL; result = accel_generate_state(&state, 1, 1, NULL, 0); - EXPECT_EQ(0, result); + EXPECT_EQ(ACCEL_SUCCESS, result); accel_destroy_state(&state); } @@ -96,7 +96,7 @@ TEST(AccelFuzzTest, generate_state_invalid_window_size) { // Size 1 must succeed result = accel_generate_state(&state, 1, 1, NULL, 0); - EXPECT_EQ(0, result); + EXPECT_EQ(ACCEL_SUCCESS, result); EXPECT_NE(VOID_NULL, state); } @@ -176,7 +176,7 @@ TEST(AccelFuzzTest, accel_destroy_state_invalid_input) { // Destroy the state result = accel_destroy_state(&state); - EXPECT_EQ(0, result); + EXPECT_EQ(ACCEL_SUCCESS, result); EXPECT_EQ(VOID_NULL, state); } @@ -219,12 +219,12 @@ TEST(AccelFuzzTest, accel_end_record_gesture_invalid_input) { int gesture = 1234; result = accel_start_record_gesture(state, &gesture); EXPECT_NE(1234, gesture); - EXPECT_EQ(0, result); + EXPECT_EQ(ACCEL_SUCCESS, result); int data[1] = {1}; - EXPECT_EQ(0, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); result = accel_end_record_gesture(state, gesture); - EXPECT_EQ(0, result) << "gesture " << gesture << " couldn't be recorded correctly" << std::endl; + EXPECT_EQ(ACCEL_SUCCESS, result) << "gesture " << gesture << " couldn't be recorded correctly" << std::endl; test_burn_state(&state); } @@ -278,8 +278,8 @@ TEST(AccelTest, accel_generate_and_destroy) { accel_state *state = NULL; for (int i = 1; i < 10; ++i) { EXPECT_EQ(VOID_NULL, state) << "i = " << i; - EXPECT_EQ(0, accel_generate_state(&state, 2 * i, i, NULL, 0)) << "i = " << i; - EXPECT_EQ(0, accel_destroy_state(&state)) << "i = " << i; + EXPECT_EQ(ACCEL_SUCCESS, accel_generate_state(&state, 2 * i, i, NULL, 0)) << "i = " << i; + EXPECT_EQ(ACCEL_SUCCESS, accel_destroy_state(&state)) << "i = " << i; EXPECT_EQ(VOID_NULL, state) << "i = " << i; } } @@ -306,15 +306,15 @@ TEST(AccelTest, record_incredibly_long_sequence) { state = test_fabricate_1d_state(); int gesture = 0; - EXPECT_EQ(0, accel_start_record_gesture(state, &gesture)); - EXPECT_EQ(0, gesture); + EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture)); + EXPECT_EQ(ACCEL_SUCCESS, gesture); int data[] = {1}; for (int i = 0; i < 10000; ++i) { - EXPECT_EQ(0, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); } - EXPECT_EQ(0, accel_end_record_gesture(state, gesture)); + EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture)); test_burn_state(&state); } @@ -323,16 +323,16 @@ TEST(AccelTest, end_to_end_test_single_recording) { state = test_fabricate_1d_state(); int gesture = 0; - EXPECT_EQ(0, accel_start_record_gesture(state, &gesture)); - EXPECT_EQ(0, gesture); + EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture)); + EXPECT_EQ(ACCEL_SUCCESS, gesture); int data[] = {1}; for (int i = 0; i < 10; ++i) { data[0] = i * 100; - EXPECT_EQ(0, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); } - EXPECT_EQ(0, accel_end_record_gesture(state, gesture)); + EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, gesture)); int prev_affinity = 0; for (int i = 0; i < 10; ++i) { @@ -357,27 +357,27 @@ TEST(AccelTest, end_to_end_test_multiple_recordings) { state = test_fabricate_1d_state(); int first_gesture = 0; - EXPECT_EQ(0, accel_start_record_gesture(state, &first_gesture)); - EXPECT_EQ(0, first_gesture); + EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &first_gesture)); + EXPECT_EQ(ACCEL_SUCCESS, first_gesture); int data[] = {1}; for (int i = 0; i < 10; ++i) { data[0] = i; - EXPECT_EQ(0, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); } - EXPECT_EQ(0, accel_end_record_gesture(state, first_gesture)); + EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, first_gesture)); int second_gesture = 0; - EXPECT_EQ(0, accel_start_record_gesture(state, &second_gesture)); + EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &second_gesture)); EXPECT_NE(first_gesture, second_gesture); for (int i = 0; i < 10; ++i) { data[0] = i * i; - EXPECT_EQ(0, accel_process_timer_tick(state, data)); + EXPECT_EQ(ACCEL_SUCCESS, accel_process_timer_tick(state, data)); } - EXPECT_EQ(0, accel_end_record_gesture(state, second_gesture)); + EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, second_gesture)); int prev_affinity = 0; for (int i = 0; i < 10; ++i) { @@ -472,7 +472,7 @@ TEST(MovingAvgTicker, AllocatesAndFreesCorrectly) { EXPECT_EQ(retval, 0); retval = free_moving_avg(&allocated); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_EQ(VOID_NULL, allocated); } @@ -480,39 +480,39 @@ TEST(MovingAvgTicker, ResetsCorrectly) { moving_avg_values *allocated = NULL; int retval = allocate_moving_avg(1, 1, &allocated); EXPECT_NE(VOID_NULL, allocated); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); retval = reset_moving_avg(NULL); EXPECT_EQ(MOVING_AVG_PARAM_ERROR, retval); retval = reset_moving_avg(allocated); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); } TEST(MovingAvgTicker, AppendsCorrectly1_1) { moving_avg_values *allocated = NULL; int retval = allocate_moving_avg(1, 1, &allocated); EXPECT_NE(VOID_NULL, allocated); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); bool is_at_end = false; retval = append_to_moving_avg(allocated, 1, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_TRUE(is_at_end); int frame = 0; retval = get_latest_frame_moving_avg(allocated, &frame); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_EQ(1, frame); is_at_end = false; retval = append_to_moving_avg(allocated, 2, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_TRUE(is_at_end); frame = 0; retval = get_latest_frame_moving_avg(allocated, &frame); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_EQ(2, frame); } @@ -520,36 +520,36 @@ TEST(MovingAvgTicker, AppendsCorrectly2_1) { moving_avg_values *allocated = NULL; int retval = allocate_moving_avg(2, 1, &allocated); EXPECT_NE(VOID_NULL, allocated); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); bool is_at_end = false; retval = append_to_moving_avg(allocated, 2, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_TRUE(is_at_end); int frame = 0; retval = get_latest_frame_moving_avg(allocated, &frame); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_EQ(1, frame); is_at_end = false; retval = append_to_moving_avg(allocated, 4, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_TRUE(is_at_end); frame = 0; retval = get_latest_frame_moving_avg(allocated, &frame); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_EQ(3, frame); is_at_end = false; retval = append_to_moving_avg(allocated, 2, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_TRUE(is_at_end); frame = 0; retval = get_latest_frame_moving_avg(allocated, &frame); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_EQ(3, frame); } @@ -557,34 +557,34 @@ TEST(MovingAvgTicker, AppendsCorrectly1_2) { moving_avg_values *allocated = NULL; int retval = allocate_moving_avg(1, 2, &allocated); EXPECT_NE(VOID_NULL, allocated); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); bool is_at_end = false; retval = append_to_moving_avg(allocated, 1, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_FALSE(is_at_end); retval = append_to_moving_avg(allocated, 1, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_TRUE(is_at_end); int frame = 0; retval = get_latest_frame_moving_avg(allocated, &frame); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_EQ(2, frame); is_at_end = false; retval = append_to_moving_avg(allocated, 2, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_FALSE(is_at_end); retval = append_to_moving_avg(allocated, 2, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_TRUE(is_at_end); frame = 0; retval = get_latest_frame_moving_avg(allocated, &frame); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_EQ(4, frame); } @@ -592,38 +592,38 @@ TEST(MovingAvgTicker, AppendsCorrectly2_2) { moving_avg_values *allocated = NULL; int retval = allocate_moving_avg(2, 2, &allocated); EXPECT_NE(VOID_NULL, allocated); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); bool is_at_end = false; retval = append_to_moving_avg(allocated, 1, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_FALSE(is_at_end); retval = append_to_moving_avg(allocated, 1, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_TRUE(is_at_end); int frame = 0; retval = get_latest_frame_moving_avg(allocated, &frame); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_EQ(1, frame); is_at_end = false; retval = append_to_moving_avg(allocated, 2, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_FALSE(is_at_end); retval = append_to_moving_avg(allocated, 2, &is_at_end); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_TRUE(is_at_end); frame = 0; retval = get_latest_frame_moving_avg(allocated, &frame); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_EQ(3, frame); retval = free_moving_avg(&allocated); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_EQ(VOID_NULL, allocated); } @@ -638,13 +638,13 @@ TEST(MovingAvgTicker, AppendWithInvalidAtEnd) { int retval = 0; moving_avg_values *allocated = NULL; retval = allocate_moving_avg(2, 2, &allocated); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_NE(VOID_NULL, allocated); retval = append_to_moving_avg(allocated, 1, (bool *)NULL); EXPECT_EQ(MOVING_AVG_PARAM_ERROR, retval); - EXPECT_EQ(0, free_moving_avg(&allocated)); + EXPECT_EQ(MOVING_AVG_SUCCESS, free_moving_avg(&allocated)); } TEST(MovingAvgTicker, InvalidLatestFrameParams) { @@ -655,13 +655,13 @@ TEST(MovingAvgTicker, InvalidLatestFrameParams) { moving_avg_values *allocated = NULL; retval = allocate_moving_avg(2, 2, &allocated); - EXPECT_EQ(0, retval); + EXPECT_EQ(MOVING_AVG_SUCCESS, retval); EXPECT_NE(VOID_NULL, allocated); retval = get_latest_frame_moving_avg(allocated, (int *)NULL); EXPECT_EQ(MOVING_AVG_PARAM_ERROR, retval); - EXPECT_EQ(0, free_moving_avg(&allocated)); + EXPECT_EQ(MOVING_AVG_SUCCESS, free_moving_avg(&allocated)); } TEST(MovingAvgTickerFuzzTest, allocate_moving_avg) { @@ -688,9 +688,9 @@ TEST(MovingAvgTickerFuzzTest, allocate_moving_avg) { // Test with success, to validate that there was only one difference between this and the above tests. allocated = NULL; - EXPECT_EQ(0, allocate_moving_avg(1, 1, &allocated)); + EXPECT_EQ(MOVING_AVG_SUCCESS, allocate_moving_avg(1, 1, &allocated)); EXPECT_NE(VOID_NULL, allocated); - EXPECT_EQ(0, free_moving_avg(&allocated)); + EXPECT_EQ(MOVING_AVG_SUCCESS, free_moving_avg(&allocated)); EXPECT_EQ(VOID_NULL, allocated); } @@ -703,7 +703,7 @@ TEST(MovingAvgTickerFuzzTest, append_to_moving_avg) { // Setup: bool is_at_end = false; moving_avg_values *allocated = NULL; - EXPECT_EQ(0, allocate_moving_avg(1, 1, &allocated)); + EXPECT_EQ(MOVING_AVG_SUCCESS, allocate_moving_avg(1, 1, &allocated)); // Test with null pointer EXPECT_EQ(MOVING_AVG_PARAM_ERROR, append_to_moving_avg(NULL, 1, &is_at_end)); @@ -712,16 +712,16 @@ TEST(MovingAvgTickerFuzzTest, append_to_moving_avg) { EXPECT_EQ(MOVING_AVG_PARAM_ERROR, append_to_moving_avg(allocated, 1, NULL)); // Test with all valid input - EXPECT_EQ(0, append_to_moving_avg(allocated, 1, &is_at_end)); + EXPECT_EQ(MOVING_AVG_SUCCESS, append_to_moving_avg(allocated, 1, &is_at_end)); // Cleanup: - EXPECT_EQ(0, free_moving_avg(&allocated)); + EXPECT_EQ(MOVING_AVG_SUCCESS, free_moving_avg(&allocated)); } TEST(MovingAvgTickerFuzzTest, get_latest_frame_moving_avg) { int frame = 0; moving_avg_values *allocated = NULL; - EXPECT_EQ(0, allocate_moving_avg(1, 1, &allocated)); + EXPECT_EQ(MOVING_AVG_SUCCESS, allocate_moving_avg(1, 1, &allocated)); // Both are NULL EXPECT_EQ(MOVING_AVG_PARAM_ERROR, get_latest_frame_moving_avg(NULL, NULL)); @@ -733,10 +733,10 @@ TEST(MovingAvgTickerFuzzTest, get_latest_frame_moving_avg) { EXPECT_EQ(MOVING_AVG_PARAM_ERROR, get_latest_frame_moving_avg(allocated, NULL)); // Validate it works to justify the above unit tests - EXPECT_EQ(0, get_latest_frame_moving_avg(allocated, &frame)); + EXPECT_EQ(MOVING_AVG_SUCCESS, get_latest_frame_moving_avg(allocated, &frame)); // Cleanup - EXPECT_EQ(0, free_moving_avg(&allocated)); + EXPECT_EQ(MOVING_AVG_SUCCESS, free_moving_avg(&allocated)); } TEST(MovingAvgTickerFuzzTest, free_moving_avg) { @@ -748,18 +748,18 @@ TEST(MovingAvgTickerFuzzTest, free_moving_avg) { EXPECT_EQ(MOVING_AVG_PARAM_ERROR, free_moving_avg(&allocated)); // Test with null wbuf - EXPECT_EQ(0, allocate_moving_avg(1, 1, &allocated)); + EXPECT_EQ(MOVING_AVG_SUCCESS, allocate_moving_avg(1, 1, &allocated)); EXPECT_NE(VOID_NULL, allocated); free(allocated->wbuf); allocated->wbuf = NULL; // TODO: successfully completes, even with invalid input. - EXPECT_EQ(0, free_moving_avg(&allocated)); + EXPECT_EQ(MOVING_AVG_SUCCESS, free_moving_avg(&allocated)); EXPECT_EQ(VOID_NULL, allocated); // Test normal path. - EXPECT_EQ(0, allocate_moving_avg(1, 1, &allocated)); + EXPECT_EQ(MOVING_AVG_SUCCESS, allocate_moving_avg(1, 1, &allocated)); EXPECT_NE(VOID_NULL, allocated); - EXPECT_EQ(0, free_moving_avg(&allocated)); + EXPECT_EQ(MOVING_AVG_SUCCESS, free_moving_avg(&allocated)); EXPECT_EQ(VOID_NULL, allocated); } From 0b597a9a2cb4893ef60f6e68f3a26042a2d5f754 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Mon, 24 Mar 2014 09:58:53 -0700 Subject: [PATCH 55/68] Added a ton of warnings, so everything is known. --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 5b42a62..65138bb 100644 --- a/Makefile +++ b/Makefile @@ -12,9 +12,9 @@ TEST_OBJECTS = $(subst test,bin,$(_TEST_OBJECTS_TMP)) EXEC = tests # removed: -Wl,-z,relro -Wl,-z,now -C_ARGS = -DIS_NOT_PEBBLE -DRELEASE -pipe -m64 -ansi -fPIC -fPIE -g -Os -ffunction-sections -fno-exceptions -fstack-protector-all -fvisibility=hidden -W -Wall -Wextra -Wunused-parameter -Wunused-function -Wunused-label -Wpointer-arith -Wformat -Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral -Winit-self -Wuninitialized -Wdeprecated -Wformat-security -Werror -Wcomment -Wtrigraphs -Wundef -Wunused-macros -pedantic-errors -std=c99 +C_ARGS = -ansi -DIS_NOT_PEBBLE -DRELEASE -ffunction-sections -fno-exceptions -fPIC -fPIE -fstack-protector-all -fstrict-overflow -fvisibility=hidden -g -m64 -Os -pedantic-errors -pipe -std=c99 -W -Wall -Wbad-function-cast -Wc++-compat -Wcast-qual -Wcomment -Wconversion -Wdeprecated -Werror -Wextra -Wfloat-equal -Wformat -Wformat-nonliteral -Wformat-security -Wformat-y2k -Winit-self -Wmissing-include-dirs -Wmultichar -Wnested-externs -Wold-style-definition -Wpacked -Wpadded -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsign-compare -Wstack-protector -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wtrigraphs -Wundef -Wuninitialized -Wuninitialized -Wunused-function -Wunused-label -Wunused-macros -Wunused-parameter -Wvariadic-macros -Wwrite-strings # removed: -lpthread, -Wl,-z,relro -Wl,-z,now -CXX_ARGS = -DIS_NOT_PEBBLE -lpthread -pipe -m64 -ansi -fPIC -g -O3 -fno-exceptions -fstack-protector -fvisibility=hidden -W -Wall -Wno-unused-parameter -Wno-unused-function -Wno-unused-label -Wpointer-arith -Wformat -Wreturn-type -Wsign-compare -Wmultichar -Wformat-nonliteral -Winit-self -Wuninitialized -Wno-deprecated -Wformat-security -Wall +CXX_ARGS = -DIS_NOT_PEBBLE -fno-exceptions -fPIC -fstack-protector -fvisibility=hidden -g -lpthread -m64 -O3 -pipe -W -Wall -Wextra -Wextra-tokens -Wconversion -Wformat -Wformat-nonliteral -Wformat-security -Winit-self -Wmultichar -Wno-deprecated -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wpointer-arith -Wreturn-type -Wsign-compare -Wuninitialized -Wcast-qual -Wsign-compare -Wsign-conversion -Wlogical-op default: @make all From 549aab2c843d8d53be7aed6691be49c6c680bf88 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Mon, 24 Mar 2014 21:07:41 -0700 Subject: [PATCH 56/68] Alpha is a float, as it should be --- sample/simple-accelerometer/src/accel.c | 2 +- src/accel.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index a55e381..cc47680 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -86,7 +86,7 @@ typedef struct internalAccelState { // Decay rate of values we choose to keep. 1.0 is no decay, 2.0 is a doubling every time we keep them. // TODO: should we store the offsets as floats instead? -#define ALPHA 1.0 +#define ALPHA ((float) 1.0) // TODO: include these from a header file? #define MIN(a, b) (((a) < (b)) ? (a) : (b)) diff --git a/src/accel.c b/src/accel.c index a55e381..cc47680 100644 --- a/src/accel.c +++ b/src/accel.c @@ -86,7 +86,7 @@ typedef struct internalAccelState { // Decay rate of values we choose to keep. 1.0 is no decay, 2.0 is a doubling every time we keep them. // TODO: should we store the offsets as floats instead? -#define ALPHA 1.0 +#define ALPHA ((float) 1.0) // TODO: include these from a header file? #define MIN(a, b) (((a) < (b)) ? (a) : (b)) From 9aa0efb4cf4506c32367a8934b94def916920a05 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Mon, 24 Mar 2014 21:43:19 -0700 Subject: [PATCH 57/68] Added brackets to some types, and braces to others --- sample/simple-accelerometer/src/accel.c | 6 +++--- sample/simple-accelerometer/src/pebble_makeup.h | 6 ++++-- src/accel.c | 6 +++--- src/pebble_makeup.h | 6 ++++-- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index cc47680..b9af834 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -8,8 +8,8 @@ #include #include #else -#define my_realloc(a, b, c) realloc(a, b) -#define my_calloc(a, b) calloc(a, b) +#define my_realloc(a, b, c) (realloc(a, b)) +#define my_calloc(a, b) (calloc(a, b)) #endif #ifndef INT16_MAX @@ -86,7 +86,7 @@ typedef struct internalAccelState { // Decay rate of values we choose to keep. 1.0 is no decay, 2.0 is a doubling every time we keep them. // TODO: should we store the offsets as floats instead? -#define ALPHA ((float) 1.0) +#define ALPHA ((float)1.0) // TODO: include these from a header file? #define MIN(a, b) (((a) < (b)) ? (a) : (b)) diff --git a/sample/simple-accelerometer/src/pebble_makeup.h b/sample/simple-accelerometer/src/pebble_makeup.h index ee92618..f09e29b 100644 --- a/sample/simple-accelerometer/src/pebble_makeup.h +++ b/sample/simple-accelerometer/src/pebble_makeup.h @@ -22,8 +22,9 @@ void *my_realloc(void *old_ptr, size_t new_size, size_t old_size) { } void *p = malloc(new_size); - if (!p) + if (!p) { return NULL; + } size_t min_size = new_size < old_size ? new_size : old_size; memcpy(p, old_ptr, min_size); @@ -33,8 +34,9 @@ void *my_realloc(void *old_ptr, size_t new_size, size_t old_size) { void *my_calloc(size_t num, size_t size) { void *allocd = malloc(num * size); - if (allocd == NULL) + if (allocd == NULL) { return allocd; + } memset(allocd, 0, num * size); return allocd; } diff --git a/src/accel.c b/src/accel.c index cc47680..b9af834 100644 --- a/src/accel.c +++ b/src/accel.c @@ -8,8 +8,8 @@ #include #include #else -#define my_realloc(a, b, c) realloc(a, b) -#define my_calloc(a, b) calloc(a, b) +#define my_realloc(a, b, c) (realloc(a, b)) +#define my_calloc(a, b) (calloc(a, b)) #endif #ifndef INT16_MAX @@ -86,7 +86,7 @@ typedef struct internalAccelState { // Decay rate of values we choose to keep. 1.0 is no decay, 2.0 is a doubling every time we keep them. // TODO: should we store the offsets as floats instead? -#define ALPHA ((float) 1.0) +#define ALPHA ((float)1.0) // TODO: include these from a header file? #define MIN(a, b) (((a) < (b)) ? (a) : (b)) diff --git a/src/pebble_makeup.h b/src/pebble_makeup.h index ee92618..f09e29b 100644 --- a/src/pebble_makeup.h +++ b/src/pebble_makeup.h @@ -22,8 +22,9 @@ void *my_realloc(void *old_ptr, size_t new_size, size_t old_size) { } void *p = malloc(new_size); - if (!p) + if (!p) { return NULL; + } size_t min_size = new_size < old_size ? new_size : old_size; memcpy(p, old_ptr, min_size); @@ -33,8 +34,9 @@ void *my_realloc(void *old_ptr, size_t new_size, size_t old_size) { void *my_calloc(size_t num, size_t size) { void *allocd = malloc(num * size); - if (allocd == NULL) + if (allocd == NULL) { return allocd; + } memset(allocd, 0, num * size); return allocd; } From 472915202e32688f660ee87b91bbe66a11c7491e Mon Sep 17 00:00:00 2001 From: shalecraig Date: Mon, 24 Mar 2014 23:42:55 -0700 Subject: [PATCH 58/68] Wow, I cleaned up types everywhere. Next up: adding explicit sizing --- Makefile | 6 +- sample/simple-accelerometer/src/accel.c | 96 ++++++++++++------- sample/simple-accelerometer/src/accel.h | 5 +- .../src/moving_avg_ticker.c | 2 +- .../src/moving_avg_ticker.h | 2 +- src/accel.c | 96 ++++++++++++------- src/accel.h | 5 +- src/moving_avg_ticker.c | 2 +- src/moving_avg_ticker.h | 2 +- test/main.cc | 11 --- 10 files changed, 135 insertions(+), 92 deletions(-) diff --git a/Makefile b/Makefile index 65138bb..3cd9336 100644 --- a/Makefile +++ b/Makefile @@ -12,9 +12,9 @@ TEST_OBJECTS = $(subst test,bin,$(_TEST_OBJECTS_TMP)) EXEC = tests # removed: -Wl,-z,relro -Wl,-z,now -C_ARGS = -ansi -DIS_NOT_PEBBLE -DRELEASE -ffunction-sections -fno-exceptions -fPIC -fPIE -fstack-protector-all -fstrict-overflow -fvisibility=hidden -g -m64 -Os -pedantic-errors -pipe -std=c99 -W -Wall -Wbad-function-cast -Wc++-compat -Wcast-qual -Wcomment -Wconversion -Wdeprecated -Werror -Wextra -Wfloat-equal -Wformat -Wformat-nonliteral -Wformat-security -Wformat-y2k -Winit-self -Wmissing-include-dirs -Wmultichar -Wnested-externs -Wold-style-definition -Wpacked -Wpadded -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsign-compare -Wstack-protector -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wtrigraphs -Wundef -Wuninitialized -Wuninitialized -Wunused-function -Wunused-label -Wunused-macros -Wunused-parameter -Wvariadic-macros -Wwrite-strings -# removed: -lpthread, -Wl,-z,relro -Wl,-z,now -CXX_ARGS = -DIS_NOT_PEBBLE -fno-exceptions -fPIC -fstack-protector -fvisibility=hidden -g -lpthread -m64 -O3 -pipe -W -Wall -Wextra -Wextra-tokens -Wconversion -Wformat -Wformat-nonliteral -Wformat-security -Winit-self -Wmultichar -Wno-deprecated -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wpointer-arith -Wreturn-type -Wsign-compare -Wuninitialized -Wcast-qual -Wsign-compare -Wsign-conversion -Wlogical-op +C_ARGS = -ansi -DIS_NOT_PEBBLE -DRELEASE -ffunction-sections -fno-exceptions -fPIC -fPIE -fstack-protector-all -fstrict-overflow -fvisibility=hidden -g -m64 -Os -pedantic-errors -pipe -std=c99 -W -Wall -Wbad-function-cast -Wc++-compat -Wcast-qual -Wcomment -Wconversion -Wdeprecated -Werror -Wextra -Wfloat-equal -Wformat -Wformat-nonliteral -Wformat-security -Wformat-y2k -Winit-self -Wmissing-include-dirs -Wmultichar -Wnested-externs -Wold-style-definition -Wpacked -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsign-compare -Wstack-protector -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wtrigraphs -Wundef -Wuninitialized -Wuninitialized -Wunused-function -Wunused-label -Wunused-macros -Wunused-parameter -Wvariadic-macros -Wwrite-strings -Wlong-long +# removed: -lpthread, -Wl,-z,relro -Wl,-z,now -Wsign-conversion +CXX_ARGS = -DIS_NOT_PEBBLE -fno-exceptions -fPIC -fstack-protector -fvisibility=hidden -g -lpthread -m64 -O3 -pipe -W -Wall -Wextra -Wextra-tokens -Wconversion -Wformat -Wformat-nonliteral -Wformat-security -Winit-self -Wmultichar -Wno-deprecated -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wpointer-arith -Wreturn-type -Wsign-compare -Wuninitialized -Wcast-qual -Wsign-compare -Wno-sign-conversion -Wno-conversion default: @make all diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index b9af834..2098a2a 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -29,7 +29,8 @@ typedef struct { bool is_recording; bool is_recorded; - int recording_size; + // TODO: uint32_t needed instead? + uint16_t recording_size; int **normalized_recording; moving_avg_values **moving_avg_values; @@ -37,9 +38,10 @@ typedef struct { } accel_gesture; typedef struct internalAccelState { - int window_size; + // TODO: uint32_t needed instead? + uint16_t num_gestures_saved; + uint16_t window_size; int threshold; - int num_gestures_saved; accel_gesture **gestures; } internal_accel_state; @@ -67,15 +69,15 @@ typedef struct internalAccelState { if (INPUT_STATE->state->window_size <= 0) { \ return ACCEL_INTERNAL_ERROR; \ } \ - if (INPUT_STATE->state->num_gestures_saved < 0) { \ - return ACCEL_INTERNAL_ERROR; \ - } \ if (INPUT_STATE->state->gestures == NULL && INPUT_STATE->state->num_gestures_saved != 0) { \ return ACCEL_INTERNAL_ERROR; \ } \ if (INPUT_STATE->state->gestures != NULL && INPUT_STATE->state->num_gestures_saved == 0) { \ return ACCEL_INTERNAL_ERROR; \ } +// if (INPUT_STATE->state->num_gestures_saved < 0) { +// return ACCEL_INTERNAL_ERROR; +// } #define PRECONDITION_TRUE_PARAM(TRUE_COND) \ { \ @@ -88,11 +90,12 @@ typedef struct internalAccelState { // TODO: should we store the offsets as floats instead? #define ALPHA ((float)1.0) -// TODO: include these from a header file? +// TODO: include this from a header file? +// TODO: include as a static inline function? #define MIN(a, b) (((a) < (b)) ? (a) : (b)) // #define MAX(a,b) (((a)>(b))?(a):(b)) -void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { +void accel_destroy_gesture(accel_gesture **gesture, uint32_t dimensions) { if (gesture == NULL || *gesture == NULL) { return; } @@ -100,12 +103,12 @@ void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { accel_gesture *gest = *gesture; if (gest->moving_avg_values != NULL) { - for (int i = 0; i < dimensions; ++i) { + for (uint32_t i = 0; i < dimensions; ++i) { free_moving_avg(&(gest->moving_avg_values[i])); } } if (gest->normalized_recording != NULL) { - for (int i = 0; i < gest->recording_size; ++i) { + for (uint16_t i = 0; i < gest->recording_size; ++i) { if (gest->normalized_recording[i] != NULL) { free(gest->normalized_recording[i]); gest->normalized_recording[i] = NULL; @@ -146,13 +149,13 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { *gesture = NULL; return ACCEL_MALLOC_ERROR; } - for (int i = 0; i < state->dimensions; ++i) { + for (uint32_t i = 0; i < state->dimensions; ++i) { // TODO: these two shouldn't both be the same.... int result = allocate_moving_avg(state->state->window_size, state->state->window_size, &((*gesture)->moving_avg_values[i])); if (result != ACCEL_SUCCESS) { - for (int j = 0; j < i; ++j) { + for (uint32_t j = 0; j < i; ++j) { free_moving_avg(&((*gesture)->moving_avg_values[i])); } accel_destroy_gesture(gesture, state->dimensions); @@ -162,14 +165,14 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { return ACCEL_SUCCESS; } -int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback, +int accel_generate_state(accel_state **state, uint32_t dimensions, uint16_t window_size, accel_callback callback, const int threshold) { PRECONDITION_NOT_NULL(state); // TODO: write a test for this value. PRECONDITION_NULL(*state); - if (dimensions <= 0) { + if (dimensions == 0) { return ACCEL_PARAM_ERROR; } if (window_size <= 0) { @@ -216,12 +219,12 @@ int accel_destroy_state(accel_state **state) { PRECONDITION_NOT_NULL(state); PRECONDITION_NOT_NULL(*state); - int dimensions = (*state)->dimensions; + uint32_t dimensions = (*state)->dimensions; if ((*state)->state != NULL) { internal_accel_state *istate = (*state)->state; if (istate->gestures != NULL) { /* TODO: remove all additional fields inside the accel_state variable */ - for (int i = 0; i < istate->num_gestures_saved; ++i) { + for (uint16_t i = 0; i < istate->num_gestures_saved; ++i) { accel_gesture *gest = (istate->gestures[i]); accel_destroy_gesture(&(gest), dimensions); } @@ -281,17 +284,40 @@ int accel_start_record_gesture(accel_state *state, int *gesture) { return ACCEL_SUCCESS; } +// Taken from hackers delight +// http://www.hackersdelight.org/hdcodetxt/icbrt.c.txt +uint32_t icbrt1(uint32_t x) { + int32_t s; + unsigned y, b; + + y = 0; + for (s = 30; s >= 0; s = s - 3) { + y = 2 * y; + b = (3 * y * (y + 1) + 1) << s; + if (x >= b) { + x = x - b; + y = y + 1; + } + } + return y; +} + // The uWave paper suggests a mapping from [-20, 20]->[-15, 15], but cube root // should to work better for variable ranges. // TODO: revisit this decision. -int normalize(int sum) { return (int)cbrt(sum); } +int32_t normalize(int32_t sum) { + if (sum < 0) { + return -1 * (int32_t)icbrt1((uint32_t)(-sum)); + } + return (int32_t)icbrt1((uint32_t)sum); +} -int reset_gesture(accel_gesture *gest, const int dimensions) { +int reset_gesture(accel_gesture *gest, const uint32_t dimensions) { PRECONDITION_NOT_NULL(gest); - for (int i = 0; i < gest->recording_size; ++i) { + for (uint16_t i = 0; i < gest->recording_size; ++i) { gest->offsets[i] = INT16_MAX; } - for (int d = 0; d < dimensions; ++d) { + for (uint32_t d = 0; d < dimensions; ++d) { reset_moving_avg(gest->moving_avg_values[d]); } return ACCEL_SUCCESS; @@ -337,10 +363,10 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { gesture->is_recorded = true; } - for (int i = 0; i < gesture->recording_size; ++i) { + for (uint16_t i = 0; i < gesture->recording_size; ++i) { gesture->offsets[i] = INT16_MAX; } - for (int d = 0; d < state->dimensions; ++d) { + for (uint32_t d = 0; d < state->dimensions; ++d) { reset_moving_avg(gesture->moving_avg_values[d]); } return ACCEL_SUCCESS; @@ -349,7 +375,7 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { // TODO: gracefully handle malloc failure in this function. // TODO: this should return error types instead of being void. // Follow-up: find usages of this method. -void handle_recording_tick(accel_gesture *gesture, int dimensions) { +void handle_recording_tick(accel_gesture *gesture, uint32_t dimensions) { if (gesture == NULL) { return; } @@ -365,7 +391,7 @@ void handle_recording_tick(accel_gesture *gesture, int dimensions) { gesture->normalized_recording = (int **)malloc(sizeof(int *)); } gesture->normalized_recording[gesture->recording_size] = (int *)malloc(sizeof(int) * dimensions); - for (int i = 0; i < dimensions; ++i) { + for (uint32_t i = 0; i < dimensions; ++i) { // TODO: fix this int/float business. // TODO: complain about invalid return values. get_latest_frame_moving_avg(gesture->moving_avg_values[i], @@ -373,26 +399,26 @@ void handle_recording_tick(accel_gesture *gesture, int dimensions) { gesture->normalized_recording[gesture->recording_size][i] = normalize(gesture->normalized_recording[gesture->recording_size][i]); } - ++gesture->recording_size; + ++(gesture->recording_size); } int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gesture_id) { // TODO: load the input at the beginning instead of gesture->recording_size times. PRECONDITION_NOT_NULL(gesture); - int dimensions = state->dimensions; + uint32_t dimensions = state->dimensions; if (gesture->moving_avg_values == NULL || gesture->offsets == NULL) { return ACCEL_INTERNAL_ERROR; } - int i = gesture->recording_size; + uint16_t i = gesture->recording_size; while (i != 0) { --i; int cost = 0; - for (int d = 0; d < dimensions; ++d) { + for (uint32_t d = 0; d < dimensions; ++d) { int recording_i_d = gesture->normalized_recording[i][d]; - int input_i_d = 0; + int32_t input_i_d = 0; // TODO: complain about invalid return values. get_latest_frame_moving_avg(gesture->moving_avg_values[d], &input_i_d); input_i_d = normalize(input_i_d); @@ -406,14 +432,14 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu if (i == 0) { gesture->offsets[i] = cost; } else { - gesture->offsets[i] = MIN(ALPHA * gesture->offsets[i], cost + gesture->offsets[i - 1]); + gesture->offsets[i] = MIN((int)(ALPHA * gesture->offsets[i]), cost + gesture->offsets[i - 1]); } } for (i = 1; i < gesture->recording_size; ++i) { int cost = 0; - for (int d = 0; d < dimensions; ++d) { + for (uint32_t d = 0; d < dimensions; ++d) { int recording_i_d = gesture->normalized_recording[i][d]; - int input_i_d = 0; + int32_t input_i_d = 0; // TODO: complain about invalid return values. get_latest_frame_moving_avg(gesture->moving_avg_values[d], &input_i_d); if (recording_i_d > input_i_d) { @@ -429,10 +455,10 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu if (state->state->threshold <= 0) { return ACCEL_PARAM_ERROR; } - float avg_affinity = gesture->offsets[gesture->recording_size - 1] * 1.0 / gesture->recording_size; + float avg_affinity = ((float)gesture->offsets[gesture->recording_size - 1]) / gesture->recording_size; if (avg_affinity < state->state->threshold) { bool reset; - int retval = state->callback(state, gesture_id, avg_affinity, &reset); + int retval = state->callback(state, gesture_id, (int32_t)avg_affinity, &reset); if (reset == true) { reset_gesture(gesture, dimensions); } @@ -464,7 +490,7 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { // If the moving average is at a final line. bool avg_line = false; int returned = ACCEL_SUCCESS; - for (int d = 0; d < state->dimensions && returned == 0; ++d) { + for (uint32_t d = 0; d < state->dimensions && returned == 0; ++d) { returned = append_to_moving_avg(gesture->moving_avg_values[d], accel_data[d], &avg_line); } if (returned != ACCEL_SUCCESS) { diff --git a/sample/simple-accelerometer/src/accel.h b/sample/simple-accelerometer/src/accel.h index eda1110..5dd4599 100644 --- a/sample/simple-accelerometer/src/accel.h +++ b/sample/simple-accelerometer/src/accel.h @@ -60,8 +60,9 @@ struct accelState; */ typedef int (*accel_callback)(struct accelState *state, int gesture_id, int offset_found, bool *reset_gesture); +// TODO: define this as a partial-type instead of exposing some fields. typedef struct accelState { - int dimensions; + uint32_t dimensions; accel_callback callback; struct internalAccelState *state; @@ -84,7 +85,7 @@ typedef struct accelState { * gestures must be before the callback is called. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback, +int accel_generate_state(accel_state **state, uint32_t dimensions, uint16_t window_size, accel_callback callback, const int threshold); /** diff --git a/sample/simple-accelerometer/src/moving_avg_ticker.c b/sample/simple-accelerometer/src/moving_avg_ticker.c index 67e05b0..bde4931 100644 --- a/sample/simple-accelerometer/src/moving_avg_ticker.c +++ b/sample/simple-accelerometer/src/moving_avg_ticker.c @@ -33,7 +33,7 @@ int precondition_valid_moving_avg_values(moving_avg_values *input) { return MOVING_AVG_SUCCESS; } -int allocate_moving_avg(int num_wbuf, int subtotal_sizes, moving_avg_values **allocated) { +int allocate_moving_avg(uint16_t num_wbuf, int subtotal_sizes, moving_avg_values **allocated) { PRECONDITION_NOT_NULL(allocated); if (*allocated != NULL) { return MOVING_AVG_PARAM_ERROR; diff --git a/sample/simple-accelerometer/src/moving_avg_ticker.h b/sample/simple-accelerometer/src/moving_avg_ticker.h index 5a4d06d..1eebdab 100644 --- a/sample/simple-accelerometer/src/moving_avg_ticker.h +++ b/sample/simple-accelerometer/src/moving_avg_ticker.h @@ -23,7 +23,7 @@ typedef struct moving_avg_values { int max_subtotal_size; } moving_avg_values; -int allocate_moving_avg(int num_wbuf, int subtotal_sizes, moving_avg_values **allocated); +int allocate_moving_avg(uint16_t num_wbuf, int subtotal_sizes, moving_avg_values **allocated); int reset_moving_avg(moving_avg_values *reset); diff --git a/src/accel.c b/src/accel.c index b9af834..2098a2a 100644 --- a/src/accel.c +++ b/src/accel.c @@ -29,7 +29,8 @@ typedef struct { bool is_recording; bool is_recorded; - int recording_size; + // TODO: uint32_t needed instead? + uint16_t recording_size; int **normalized_recording; moving_avg_values **moving_avg_values; @@ -37,9 +38,10 @@ typedef struct { } accel_gesture; typedef struct internalAccelState { - int window_size; + // TODO: uint32_t needed instead? + uint16_t num_gestures_saved; + uint16_t window_size; int threshold; - int num_gestures_saved; accel_gesture **gestures; } internal_accel_state; @@ -67,15 +69,15 @@ typedef struct internalAccelState { if (INPUT_STATE->state->window_size <= 0) { \ return ACCEL_INTERNAL_ERROR; \ } \ - if (INPUT_STATE->state->num_gestures_saved < 0) { \ - return ACCEL_INTERNAL_ERROR; \ - } \ if (INPUT_STATE->state->gestures == NULL && INPUT_STATE->state->num_gestures_saved != 0) { \ return ACCEL_INTERNAL_ERROR; \ } \ if (INPUT_STATE->state->gestures != NULL && INPUT_STATE->state->num_gestures_saved == 0) { \ return ACCEL_INTERNAL_ERROR; \ } +// if (INPUT_STATE->state->num_gestures_saved < 0) { +// return ACCEL_INTERNAL_ERROR; +// } #define PRECONDITION_TRUE_PARAM(TRUE_COND) \ { \ @@ -88,11 +90,12 @@ typedef struct internalAccelState { // TODO: should we store the offsets as floats instead? #define ALPHA ((float)1.0) -// TODO: include these from a header file? +// TODO: include this from a header file? +// TODO: include as a static inline function? #define MIN(a, b) (((a) < (b)) ? (a) : (b)) // #define MAX(a,b) (((a)>(b))?(a):(b)) -void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { +void accel_destroy_gesture(accel_gesture **gesture, uint32_t dimensions) { if (gesture == NULL || *gesture == NULL) { return; } @@ -100,12 +103,12 @@ void accel_destroy_gesture(accel_gesture **gesture, int dimensions) { accel_gesture *gest = *gesture; if (gest->moving_avg_values != NULL) { - for (int i = 0; i < dimensions; ++i) { + for (uint32_t i = 0; i < dimensions; ++i) { free_moving_avg(&(gest->moving_avg_values[i])); } } if (gest->normalized_recording != NULL) { - for (int i = 0; i < gest->recording_size; ++i) { + for (uint16_t i = 0; i < gest->recording_size; ++i) { if (gest->normalized_recording[i] != NULL) { free(gest->normalized_recording[i]); gest->normalized_recording[i] = NULL; @@ -146,13 +149,13 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { *gesture = NULL; return ACCEL_MALLOC_ERROR; } - for (int i = 0; i < state->dimensions; ++i) { + for (uint32_t i = 0; i < state->dimensions; ++i) { // TODO: these two shouldn't both be the same.... int result = allocate_moving_avg(state->state->window_size, state->state->window_size, &((*gesture)->moving_avg_values[i])); if (result != ACCEL_SUCCESS) { - for (int j = 0; j < i; ++j) { + for (uint32_t j = 0; j < i; ++j) { free_moving_avg(&((*gesture)->moving_avg_values[i])); } accel_destroy_gesture(gesture, state->dimensions); @@ -162,14 +165,14 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { return ACCEL_SUCCESS; } -int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback, +int accel_generate_state(accel_state **state, uint32_t dimensions, uint16_t window_size, accel_callback callback, const int threshold) { PRECONDITION_NOT_NULL(state); // TODO: write a test for this value. PRECONDITION_NULL(*state); - if (dimensions <= 0) { + if (dimensions == 0) { return ACCEL_PARAM_ERROR; } if (window_size <= 0) { @@ -216,12 +219,12 @@ int accel_destroy_state(accel_state **state) { PRECONDITION_NOT_NULL(state); PRECONDITION_NOT_NULL(*state); - int dimensions = (*state)->dimensions; + uint32_t dimensions = (*state)->dimensions; if ((*state)->state != NULL) { internal_accel_state *istate = (*state)->state; if (istate->gestures != NULL) { /* TODO: remove all additional fields inside the accel_state variable */ - for (int i = 0; i < istate->num_gestures_saved; ++i) { + for (uint16_t i = 0; i < istate->num_gestures_saved; ++i) { accel_gesture *gest = (istate->gestures[i]); accel_destroy_gesture(&(gest), dimensions); } @@ -281,17 +284,40 @@ int accel_start_record_gesture(accel_state *state, int *gesture) { return ACCEL_SUCCESS; } +// Taken from hackers delight +// http://www.hackersdelight.org/hdcodetxt/icbrt.c.txt +uint32_t icbrt1(uint32_t x) { + int32_t s; + unsigned y, b; + + y = 0; + for (s = 30; s >= 0; s = s - 3) { + y = 2 * y; + b = (3 * y * (y + 1) + 1) << s; + if (x >= b) { + x = x - b; + y = y + 1; + } + } + return y; +} + // The uWave paper suggests a mapping from [-20, 20]->[-15, 15], but cube root // should to work better for variable ranges. // TODO: revisit this decision. -int normalize(int sum) { return (int)cbrt(sum); } +int32_t normalize(int32_t sum) { + if (sum < 0) { + return -1 * (int32_t)icbrt1((uint32_t)(-sum)); + } + return (int32_t)icbrt1((uint32_t)sum); +} -int reset_gesture(accel_gesture *gest, const int dimensions) { +int reset_gesture(accel_gesture *gest, const uint32_t dimensions) { PRECONDITION_NOT_NULL(gest); - for (int i = 0; i < gest->recording_size; ++i) { + for (uint16_t i = 0; i < gest->recording_size; ++i) { gest->offsets[i] = INT16_MAX; } - for (int d = 0; d < dimensions; ++d) { + for (uint32_t d = 0; d < dimensions; ++d) { reset_moving_avg(gest->moving_avg_values[d]); } return ACCEL_SUCCESS; @@ -337,10 +363,10 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { gesture->is_recorded = true; } - for (int i = 0; i < gesture->recording_size; ++i) { + for (uint16_t i = 0; i < gesture->recording_size; ++i) { gesture->offsets[i] = INT16_MAX; } - for (int d = 0; d < state->dimensions; ++d) { + for (uint32_t d = 0; d < state->dimensions; ++d) { reset_moving_avg(gesture->moving_avg_values[d]); } return ACCEL_SUCCESS; @@ -349,7 +375,7 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { // TODO: gracefully handle malloc failure in this function. // TODO: this should return error types instead of being void. // Follow-up: find usages of this method. -void handle_recording_tick(accel_gesture *gesture, int dimensions) { +void handle_recording_tick(accel_gesture *gesture, uint32_t dimensions) { if (gesture == NULL) { return; } @@ -365,7 +391,7 @@ void handle_recording_tick(accel_gesture *gesture, int dimensions) { gesture->normalized_recording = (int **)malloc(sizeof(int *)); } gesture->normalized_recording[gesture->recording_size] = (int *)malloc(sizeof(int) * dimensions); - for (int i = 0; i < dimensions; ++i) { + for (uint32_t i = 0; i < dimensions; ++i) { // TODO: fix this int/float business. // TODO: complain about invalid return values. get_latest_frame_moving_avg(gesture->moving_avg_values[i], @@ -373,26 +399,26 @@ void handle_recording_tick(accel_gesture *gesture, int dimensions) { gesture->normalized_recording[gesture->recording_size][i] = normalize(gesture->normalized_recording[gesture->recording_size][i]); } - ++gesture->recording_size; + ++(gesture->recording_size); } int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gesture_id) { // TODO: load the input at the beginning instead of gesture->recording_size times. PRECONDITION_NOT_NULL(gesture); - int dimensions = state->dimensions; + uint32_t dimensions = state->dimensions; if (gesture->moving_avg_values == NULL || gesture->offsets == NULL) { return ACCEL_INTERNAL_ERROR; } - int i = gesture->recording_size; + uint16_t i = gesture->recording_size; while (i != 0) { --i; int cost = 0; - for (int d = 0; d < dimensions; ++d) { + for (uint32_t d = 0; d < dimensions; ++d) { int recording_i_d = gesture->normalized_recording[i][d]; - int input_i_d = 0; + int32_t input_i_d = 0; // TODO: complain about invalid return values. get_latest_frame_moving_avg(gesture->moving_avg_values[d], &input_i_d); input_i_d = normalize(input_i_d); @@ -406,14 +432,14 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu if (i == 0) { gesture->offsets[i] = cost; } else { - gesture->offsets[i] = MIN(ALPHA * gesture->offsets[i], cost + gesture->offsets[i - 1]); + gesture->offsets[i] = MIN((int)(ALPHA * gesture->offsets[i]), cost + gesture->offsets[i - 1]); } } for (i = 1; i < gesture->recording_size; ++i) { int cost = 0; - for (int d = 0; d < dimensions; ++d) { + for (uint32_t d = 0; d < dimensions; ++d) { int recording_i_d = gesture->normalized_recording[i][d]; - int input_i_d = 0; + int32_t input_i_d = 0; // TODO: complain about invalid return values. get_latest_frame_moving_avg(gesture->moving_avg_values[d], &input_i_d); if (recording_i_d > input_i_d) { @@ -429,10 +455,10 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu if (state->state->threshold <= 0) { return ACCEL_PARAM_ERROR; } - float avg_affinity = gesture->offsets[gesture->recording_size - 1] * 1.0 / gesture->recording_size; + float avg_affinity = ((float)gesture->offsets[gesture->recording_size - 1]) / gesture->recording_size; if (avg_affinity < state->state->threshold) { bool reset; - int retval = state->callback(state, gesture_id, avg_affinity, &reset); + int retval = state->callback(state, gesture_id, (int32_t)avg_affinity, &reset); if (reset == true) { reset_gesture(gesture, dimensions); } @@ -464,7 +490,7 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { // If the moving average is at a final line. bool avg_line = false; int returned = ACCEL_SUCCESS; - for (int d = 0; d < state->dimensions && returned == 0; ++d) { + for (uint32_t d = 0; d < state->dimensions && returned == 0; ++d) { returned = append_to_moving_avg(gesture->moving_avg_values[d], accel_data[d], &avg_line); } if (returned != ACCEL_SUCCESS) { diff --git a/src/accel.h b/src/accel.h index eda1110..5dd4599 100644 --- a/src/accel.h +++ b/src/accel.h @@ -60,8 +60,9 @@ struct accelState; */ typedef int (*accel_callback)(struct accelState *state, int gesture_id, int offset_found, bool *reset_gesture); +// TODO: define this as a partial-type instead of exposing some fields. typedef struct accelState { - int dimensions; + uint32_t dimensions; accel_callback callback; struct internalAccelState *state; @@ -84,7 +85,7 @@ typedef struct accelState { * gestures must be before the callback is called. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_generate_state(accel_state **state, int dimensions, int window_size, accel_callback callback, +int accel_generate_state(accel_state **state, uint32_t dimensions, uint16_t window_size, accel_callback callback, const int threshold); /** diff --git a/src/moving_avg_ticker.c b/src/moving_avg_ticker.c index 67e05b0..bde4931 100644 --- a/src/moving_avg_ticker.c +++ b/src/moving_avg_ticker.c @@ -33,7 +33,7 @@ int precondition_valid_moving_avg_values(moving_avg_values *input) { return MOVING_AVG_SUCCESS; } -int allocate_moving_avg(int num_wbuf, int subtotal_sizes, moving_avg_values **allocated) { +int allocate_moving_avg(uint16_t num_wbuf, int subtotal_sizes, moving_avg_values **allocated) { PRECONDITION_NOT_NULL(allocated); if (*allocated != NULL) { return MOVING_AVG_PARAM_ERROR; diff --git a/src/moving_avg_ticker.h b/src/moving_avg_ticker.h index 5a4d06d..1eebdab 100644 --- a/src/moving_avg_ticker.h +++ b/src/moving_avg_ticker.h @@ -23,7 +23,7 @@ typedef struct moving_avg_values { int max_subtotal_size; } moving_avg_values; -int allocate_moving_avg(int num_wbuf, int subtotal_sizes, moving_avg_values **allocated); +int allocate_moving_avg(uint16_t num_wbuf, int subtotal_sizes, moving_avg_values **allocated); int reset_moving_avg(moving_avg_values *reset); diff --git a/test/main.cc b/test/main.cc index bb3edda..f96d000 100644 --- a/test/main.cc +++ b/test/main.cc @@ -54,10 +54,6 @@ TEST(AccelFuzzTest, generate_state_negative_or_zero_dimensions) { int result = accel_generate_state(&state, 0, 1, NULL, 0); EXPECT_EQ(ACCEL_PARAM_ERROR, result); - // -1 dimension must fail - result = accel_generate_state(&state, -1, 1, NULL, 0); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - // 1 dimension must succeed. state = NULL; result = accel_generate_state(&state, 1, 1, NULL, 0); @@ -90,10 +86,6 @@ TEST(AccelFuzzTest, generate_state_invalid_window_size) { result = accel_generate_state(&state, 1, 0, NULL, 0); EXPECT_EQ(ACCEL_PARAM_ERROR, result); - // Size -1 must fail - result = accel_generate_state(&state, 1, -1, NULL, 0); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - // Size 1 must succeed result = accel_generate_state(&state, 1, 1, NULL, 0); EXPECT_EQ(ACCEL_SUCCESS, result); @@ -667,9 +659,6 @@ TEST(MovingAvgTicker, InvalidLatestFrameParams) { TEST(MovingAvgTickerFuzzTest, allocate_moving_avg) { moving_avg_values *allocated = NULL; - // Test with negative num_wbuf - EXPECT_EQ(MOVING_AVG_PARAM_ERROR, allocate_moving_avg(-1, 1, &allocated)); - // Test with zero num_wbuf EXPECT_EQ(MOVING_AVG_PARAM_ERROR, allocate_moving_avg(0, 1, &allocated)); From 5544f45462efbce5079b1ae3e53d657565048030 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Mon, 24 Mar 2014 23:50:28 -0700 Subject: [PATCH 59/68] Hmm, this can go too. --- src/accel.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/accel.c b/src/accel.c index 2098a2a..b5e8a08 100644 --- a/src/accel.c +++ b/src/accel.c @@ -526,10 +526,6 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off *gesture_id = ACCEL_NO_VALID_GESTURE; *offset = ACCEL_NO_VALID_GESTURE; - if (state->state->num_gestures_saved < 0) { - return ACCEL_INTERNAL_ERROR; - } - if (state->state->num_gestures_saved == 0) { return ACCEL_NO_VALID_GESTURE; } From cbc1a179386bd0f22a8089771c432184e3a480a1 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Tue, 25 Mar 2014 00:02:14 -0700 Subject: [PATCH 60/68] Updated a number of types so pebble build works --- sample/simple-accelerometer/src/accel.c | 14 +++++--------- sample/simple-accelerometer/src/accel.h | 1 + .../simple-accelerometer/src/moving_avg_ticker.c | 4 ++-- .../simple-accelerometer/src/moving_avg_ticker.h | 4 +++- src/accel.c | 10 +++++----- src/accel.h | 1 + src/moving_avg_ticker.c | 4 ++-- src/moving_avg_ticker.h | 4 +++- 8 files changed, 22 insertions(+), 20 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index 2098a2a..746fc61 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -31,7 +31,7 @@ typedef struct { // TODO: uint32_t needed instead? uint16_t recording_size; - int **normalized_recording; + int32_t **normalized_recording; moving_avg_values **moving_avg_values; int *offsets; @@ -382,15 +382,15 @@ void handle_recording_tick(accel_gesture *gesture, uint32_t dimensions) { // TODO: grow exponentially, not linearly. Linear growth allocates too frequently. if (gesture->recording_size != 0) { gesture->normalized_recording = - (int **)my_realloc(gesture->normalized_recording, (gesture->recording_size + 1) * sizeof(int *), - gesture->recording_size * sizeof(int *)); + (int32_t **)my_realloc(gesture->normalized_recording, (gesture->recording_size + 1) * sizeof(int32_t *), + gesture->recording_size * sizeof(int32_t *)); if (gesture->normalized_recording == NULL) { return; } } else { - gesture->normalized_recording = (int **)malloc(sizeof(int *)); + gesture->normalized_recording = (int32_t **)malloc(sizeof(int32_t *)); } - gesture->normalized_recording[gesture->recording_size] = (int *)malloc(sizeof(int) * dimensions); + gesture->normalized_recording[gesture->recording_size] = (int32_t *)malloc(sizeof(int32_t) * dimensions); for (uint32_t i = 0; i < dimensions; ++i) { // TODO: fix this int/float business. // TODO: complain about invalid return values. @@ -526,10 +526,6 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off *gesture_id = ACCEL_NO_VALID_GESTURE; *offset = ACCEL_NO_VALID_GESTURE; - if (state->state->num_gestures_saved < 0) { - return ACCEL_INTERNAL_ERROR; - } - if (state->state->num_gestures_saved == 0) { return ACCEL_NO_VALID_GESTURE; } diff --git a/sample/simple-accelerometer/src/accel.h b/sample/simple-accelerometer/src/accel.h index 5dd4599..f57d2b0 100644 --- a/sample/simple-accelerometer/src/accel.h +++ b/sample/simple-accelerometer/src/accel.h @@ -6,6 +6,7 @@ extern "C" { #endif #include +#include #define ACCEL_SUCCESS 0 #define ACCEL_PARAM_ERROR -1 diff --git a/sample/simple-accelerometer/src/moving_avg_ticker.c b/sample/simple-accelerometer/src/moving_avg_ticker.c index bde4931..050eed3 100644 --- a/sample/simple-accelerometer/src/moving_avg_ticker.c +++ b/sample/simple-accelerometer/src/moving_avg_ticker.c @@ -103,7 +103,7 @@ int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end return MOVING_AVG_SUCCESS; } -int get_latest_frame_moving_avg(moving_avg_values *value, int *frame) { +int get_latest_frame_moving_avg(moving_avg_values *value, int32_t *frame) { int is_valid_return_value = precondition_valid_moving_avg_values(value); if (is_valid_return_value != MOVING_AVG_SUCCESS) { return is_valid_return_value; @@ -115,7 +115,7 @@ int get_latest_frame_moving_avg(moving_avg_values *value, int *frame) { for (int i = 0; i < value->wbuf_len; ++i) { sum += value->wbuf[i] * 1.0 / value->wbuf_len; } - *frame = (int)sum; + *frame = (int32_t)sum; return MOVING_AVG_SUCCESS; } diff --git a/sample/simple-accelerometer/src/moving_avg_ticker.h b/sample/simple-accelerometer/src/moving_avg_ticker.h index 1eebdab..82ecba9 100644 --- a/sample/simple-accelerometer/src/moving_avg_ticker.h +++ b/sample/simple-accelerometer/src/moving_avg_ticker.h @@ -5,6 +5,8 @@ extern "C" { #endif +#include + #include "accel.h" #define MOVING_AVG_SUCCESS ACCEL_SUCCESS @@ -29,7 +31,7 @@ int reset_moving_avg(moving_avg_values *reset); int append_to_moving_avg(moving_avg_values *value, int appended, bool *isAtEnd); -int get_latest_frame_moving_avg(moving_avg_values *value, int *frame); +int get_latest_frame_moving_avg(moving_avg_values *value, int32_t *frame); int free_moving_avg(moving_avg_values **value); diff --git a/src/accel.c b/src/accel.c index b5e8a08..746fc61 100644 --- a/src/accel.c +++ b/src/accel.c @@ -31,7 +31,7 @@ typedef struct { // TODO: uint32_t needed instead? uint16_t recording_size; - int **normalized_recording; + int32_t **normalized_recording; moving_avg_values **moving_avg_values; int *offsets; @@ -382,15 +382,15 @@ void handle_recording_tick(accel_gesture *gesture, uint32_t dimensions) { // TODO: grow exponentially, not linearly. Linear growth allocates too frequently. if (gesture->recording_size != 0) { gesture->normalized_recording = - (int **)my_realloc(gesture->normalized_recording, (gesture->recording_size + 1) * sizeof(int *), - gesture->recording_size * sizeof(int *)); + (int32_t **)my_realloc(gesture->normalized_recording, (gesture->recording_size + 1) * sizeof(int32_t *), + gesture->recording_size * sizeof(int32_t *)); if (gesture->normalized_recording == NULL) { return; } } else { - gesture->normalized_recording = (int **)malloc(sizeof(int *)); + gesture->normalized_recording = (int32_t **)malloc(sizeof(int32_t *)); } - gesture->normalized_recording[gesture->recording_size] = (int *)malloc(sizeof(int) * dimensions); + gesture->normalized_recording[gesture->recording_size] = (int32_t *)malloc(sizeof(int32_t) * dimensions); for (uint32_t i = 0; i < dimensions; ++i) { // TODO: fix this int/float business. // TODO: complain about invalid return values. diff --git a/src/accel.h b/src/accel.h index 5dd4599..f57d2b0 100644 --- a/src/accel.h +++ b/src/accel.h @@ -6,6 +6,7 @@ extern "C" { #endif #include +#include #define ACCEL_SUCCESS 0 #define ACCEL_PARAM_ERROR -1 diff --git a/src/moving_avg_ticker.c b/src/moving_avg_ticker.c index bde4931..050eed3 100644 --- a/src/moving_avg_ticker.c +++ b/src/moving_avg_ticker.c @@ -103,7 +103,7 @@ int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end return MOVING_AVG_SUCCESS; } -int get_latest_frame_moving_avg(moving_avg_values *value, int *frame) { +int get_latest_frame_moving_avg(moving_avg_values *value, int32_t *frame) { int is_valid_return_value = precondition_valid_moving_avg_values(value); if (is_valid_return_value != MOVING_AVG_SUCCESS) { return is_valid_return_value; @@ -115,7 +115,7 @@ int get_latest_frame_moving_avg(moving_avg_values *value, int *frame) { for (int i = 0; i < value->wbuf_len; ++i) { sum += value->wbuf[i] * 1.0 / value->wbuf_len; } - *frame = (int)sum; + *frame = (int32_t)sum; return MOVING_AVG_SUCCESS; } diff --git a/src/moving_avg_ticker.h b/src/moving_avg_ticker.h index 1eebdab..82ecba9 100644 --- a/src/moving_avg_ticker.h +++ b/src/moving_avg_ticker.h @@ -5,6 +5,8 @@ extern "C" { #endif +#include + #include "accel.h" #define MOVING_AVG_SUCCESS ACCEL_SUCCESS @@ -29,7 +31,7 @@ int reset_moving_avg(moving_avg_values *reset); int append_to_moving_avg(moving_avg_values *value, int appended, bool *isAtEnd); -int get_latest_frame_moving_avg(moving_avg_values *value, int *frame); +int get_latest_frame_moving_avg(moving_avg_values *value, int32_t *frame); int free_moving_avg(moving_avg_values **value); From 368a791dcad954ec8048ec960c0d4eb93e065910 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Tue, 25 Mar 2014 00:03:50 -0700 Subject: [PATCH 61/68] Remove useless definiition --- sample/simple-accelerometer/src/accel.c | 4 ---- src/accel.c | 4 ---- 2 files changed, 8 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index 746fc61..e004d59 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -12,10 +12,6 @@ #define my_calloc(a, b) (calloc(a, b)) #endif -#ifndef INT16_MAX -#define INT16_MAX 0x7fff -#endif - // cbrt is defined and importable for everybody! #include #include diff --git a/src/accel.c b/src/accel.c index 746fc61..e004d59 100644 --- a/src/accel.c +++ b/src/accel.c @@ -12,10 +12,6 @@ #define my_calloc(a, b) (calloc(a, b)) #endif -#ifndef INT16_MAX -#define INT16_MAX 0x7fff -#endif - // cbrt is defined and importable for everybody! #include #include From eccae3550f6a029f0b8b8657979da18d110e8f80 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Tue, 25 Mar 2014 00:19:07 -0700 Subject: [PATCH 62/68] Made offsets explicitly int32_t --- sample/simple-accelerometer/src/accel.c | 4 ++-- src/accel.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index e004d59..330b84a 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -30,7 +30,7 @@ typedef struct { int32_t **normalized_recording; moving_avg_values **moving_avg_values; - int *offsets; + int32_t *offsets; } accel_gesture; typedef struct internalAccelState { @@ -345,7 +345,7 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { return ACCEL_PARAM_ERROR; } - gesture->offsets = (int *)malloc(gesture->recording_size * sizeof(int)); + gesture->offsets = (int32_t *)malloc(gesture->recording_size * sizeof(int32_t)); if (gesture->offsets == NULL) { return ACCEL_MALLOC_ERROR; } diff --git a/src/accel.c b/src/accel.c index e004d59..330b84a 100644 --- a/src/accel.c +++ b/src/accel.c @@ -30,7 +30,7 @@ typedef struct { int32_t **normalized_recording; moving_avg_values **moving_avg_values; - int *offsets; + int32_t *offsets; } accel_gesture; typedef struct internalAccelState { @@ -345,7 +345,7 @@ int accel_end_record_gesture(accel_state *state, int gesture_id) { return ACCEL_PARAM_ERROR; } - gesture->offsets = (int *)malloc(gesture->recording_size * sizeof(int)); + gesture->offsets = (int32_t *)malloc(gesture->recording_size * sizeof(int32_t)); if (gesture->offsets == NULL) { return ACCEL_MALLOC_ERROR; } From 3e5a2f172b486030d4b30d3ef6f27be0644f006f Mon Sep 17 00:00:00 2001 From: shalecraig Date: Tue, 25 Mar 2014 00:54:43 -0700 Subject: [PATCH 63/68] Threshold is now unsigned. Updated tests accordingly. --- sample/simple-accelerometer/src/accel.c | 10 +++++----- sample/simple-accelerometer/src/accel.h | 2 +- src/accel.c | 10 +++++----- src/accel.h | 2 +- test/main.cc | 6 +----- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index 330b84a..8598776 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -37,7 +37,7 @@ typedef struct internalAccelState { // TODO: uint32_t needed instead? uint16_t num_gestures_saved; uint16_t window_size; - int threshold; + uint32_t threshold; accel_gesture **gestures; } internal_accel_state; @@ -162,7 +162,7 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { } int accel_generate_state(accel_state **state, uint32_t dimensions, uint16_t window_size, accel_callback callback, - const int threshold) { + const uint32_t threshold) { PRECONDITION_NOT_NULL(state); // TODO: write a test for this value. @@ -174,10 +174,10 @@ int accel_generate_state(accel_state **state, uint32_t dimensions, uint16_t wind if (window_size <= 0) { return ACCEL_PARAM_ERROR; } - if (threshold <= 0 && callback != NULL) { + if (threshold == 0 && callback != NULL) { return ACCEL_PARAM_ERROR; } - if (threshold > 0 && callback == NULL) { + if (threshold != 0 && callback == NULL) { return ACCEL_PARAM_ERROR; } @@ -448,7 +448,7 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu gesture->offsets[i] = MIN(gesture->offsets[i], gesture->offsets[i - 1] + cost); } if (state->callback != NULL) { - if (state->state->threshold <= 0) { + if (state->state->threshold == 0) { return ACCEL_PARAM_ERROR; } float avg_affinity = ((float)gesture->offsets[gesture->recording_size - 1]) / gesture->recording_size; diff --git a/sample/simple-accelerometer/src/accel.h b/sample/simple-accelerometer/src/accel.h index f57d2b0..73106f6 100644 --- a/sample/simple-accelerometer/src/accel.h +++ b/sample/simple-accelerometer/src/accel.h @@ -87,7 +87,7 @@ typedef struct accelState { * @return ACCEL_SUCCESS if successful, an error code otherwise. */ int accel_generate_state(accel_state **state, uint32_t dimensions, uint16_t window_size, accel_callback callback, - const int threshold); + const uint32_t threshold); /** * Destroys the state object at the pointer pointed to by the state pointer. diff --git a/src/accel.c b/src/accel.c index 330b84a..8598776 100644 --- a/src/accel.c +++ b/src/accel.c @@ -37,7 +37,7 @@ typedef struct internalAccelState { // TODO: uint32_t needed instead? uint16_t num_gestures_saved; uint16_t window_size; - int threshold; + uint32_t threshold; accel_gesture **gestures; } internal_accel_state; @@ -162,7 +162,7 @@ int accel_generate_gesture(accel_state *state, accel_gesture **gesture) { } int accel_generate_state(accel_state **state, uint32_t dimensions, uint16_t window_size, accel_callback callback, - const int threshold) { + const uint32_t threshold) { PRECONDITION_NOT_NULL(state); // TODO: write a test for this value. @@ -174,10 +174,10 @@ int accel_generate_state(accel_state **state, uint32_t dimensions, uint16_t wind if (window_size <= 0) { return ACCEL_PARAM_ERROR; } - if (threshold <= 0 && callback != NULL) { + if (threshold == 0 && callback != NULL) { return ACCEL_PARAM_ERROR; } - if (threshold > 0 && callback == NULL) { + if (threshold != 0 && callback == NULL) { return ACCEL_PARAM_ERROR; } @@ -448,7 +448,7 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu gesture->offsets[i] = MIN(gesture->offsets[i], gesture->offsets[i - 1] + cost); } if (state->callback != NULL) { - if (state->state->threshold <= 0) { + if (state->state->threshold == 0) { return ACCEL_PARAM_ERROR; } float avg_affinity = ((float)gesture->offsets[gesture->recording_size - 1]) / gesture->recording_size; diff --git a/src/accel.h b/src/accel.h index f57d2b0..73106f6 100644 --- a/src/accel.h +++ b/src/accel.h @@ -87,7 +87,7 @@ typedef struct accelState { * @return ACCEL_SUCCESS if successful, an error code otherwise. */ int accel_generate_state(accel_state **state, uint32_t dimensions, uint16_t window_size, accel_callback callback, - const int threshold); + const uint32_t threshold); /** * Destroys the state object at the pointer pointed to by the state pointer. diff --git a/test/main.cc b/test/main.cc index f96d000..af6d3ee 100644 --- a/test/main.cc +++ b/test/main.cc @@ -65,12 +65,8 @@ TEST(AccelFuzzTest, generate_state_invalid_threshold_with_callback_params) { accel_state *state = NULL; accel_callback nonNullCallback = (accel_callback)1; - // Fails for negatives. - int result = accel_generate_state(&state, 1, 1, nonNullCallback, -1); - EXPECT_EQ(ACCEL_PARAM_ERROR, result); - // Fails for zero. - result = accel_generate_state(&state, 1, 1, nonNullCallback, 0); + int result = accel_generate_state(&state, 1, 1, nonNullCallback, 0); EXPECT_EQ(ACCEL_PARAM_ERROR, result); // Succeeds for 1. From ef502c09105eb8a71136d17eb34aee3c15f15b6b Mon Sep 17 00:00:00 2001 From: shalecraig Date: Wed, 26 Mar 2014 22:21:10 -0700 Subject: [PATCH 64/68] Made tons of types more explicitly signed/sized Accel: - accel_start_record_gesture takes a uint16_t *gesture - accel_end_record_gesture takes a uint16_t *gesture Moving_avg: - wbuf_len is now a uint16_t Updated pebble sample accordingly --- sample/simple-accelerometer/src/accel.c | 28 +++++++++---------- sample/simple-accelerometer/src/accel.h | 16 +++++------ .../src/moving_avg_ticker.h | 2 +- .../src/simple-accelerometer.c | 5 ++-- src/accel.c | 28 +++++++++---------- src/accel.h | 16 +++++------ src/moving_avg_ticker.h | 2 +- test/main.cc | 28 +++++++++---------- 8 files changed, 61 insertions(+), 64 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index 8598776..8e7cdaa 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -237,7 +237,7 @@ int accel_destroy_state(accel_state **state) { return ACCEL_SUCCESS; } -int accel_start_record_gesture(accel_state *state, int *gesture) { +int accel_start_record_gesture(accel_state *state, uint16_t *gesture) { PRECONDITION_VALID_STATE(state); PRECONDITION_NOT_NULL(gesture); @@ -261,7 +261,6 @@ int accel_start_record_gesture(accel_state *state, int *gesture) { int result = accel_generate_gesture(state, &(state->state->gestures[*gesture])); if (result != ACCEL_SUCCESS) { - *gesture = -1; if (state->state->num_gestures_saved == 1) { free(state->state->gestures); state->state->gestures = NULL; @@ -284,7 +283,7 @@ int accel_start_record_gesture(accel_state *state, int *gesture) { // http://www.hackersdelight.org/hdcodetxt/icbrt.c.txt uint32_t icbrt1(uint32_t x) { int32_t s; - unsigned y, b; + uint32_t y, b; y = 0; for (s = 30; s >= 0; s = s - 3) { @@ -303,9 +302,9 @@ uint32_t icbrt1(uint32_t x) { // TODO: revisit this decision. int32_t normalize(int32_t sum) { if (sum < 0) { - return -1 * (int32_t)icbrt1((uint32_t)(-sum)); + return (int)-cbrtf(-((float)sum)); } - return (int32_t)icbrt1((uint32_t)sum); + return -(int)-cbrtf(((float)sum)); } int reset_gesture(accel_gesture *gest, const uint32_t dimensions) { @@ -320,14 +319,12 @@ int reset_gesture(accel_gesture *gest, const uint32_t dimensions) { } // TODO: does this work for zero recorded timestamps? -int accel_end_record_gesture(accel_state *state, int gesture_id) { +int accel_end_record_gesture(accel_state *state, uint16_t gesture_id) { PRECONDITION_VALID_STATE(state); - // TODO: use an unsigned int instead so we don't need to check for this type of error. - if (gesture_id < 0) { - return ACCEL_PARAM_ERROR; - } internal_accel_state *istate = state->state; + // TODO: gesture_id == istate->num_gestures_saved is incorrect. + // Write a test for it and fix it. if (gesture_id > istate->num_gestures_saved) { return ACCEL_PARAM_ERROR; } @@ -514,12 +511,12 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { return retcode; } -int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *offset) { +int accel_find_most_likely_gesture(accel_state *state, uint16_t *gesture_id, int *offset) { PRECONDITION_VALID_STATE(state); PRECONDITION_NOT_NULL(gesture_id); PRECONDITION_NOT_NULL(offset); - *gesture_id = ACCEL_NO_VALID_GESTURE; + *gesture_id = UINT16_MAX; *offset = ACCEL_NO_VALID_GESTURE; if (state->state->num_gestures_saved == 0) { @@ -530,7 +527,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off return ACCEL_INTERNAL_ERROR; } - for (int i = 0; i < state->state->num_gestures_saved; ++i) { + for (uint16_t i = 0; i < state->state->num_gestures_saved; ++i) { accel_gesture *gesture = state->state->gestures[i]; // TODO: Should this be tested? @@ -538,7 +535,8 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off return ACCEL_INTERNAL_ERROR; } - if ((*gesture_id == ACCEL_NO_VALID_GESTURE || *offset == ACCEL_NO_VALID_GESTURE) && *gesture_id != *offset) { + // Both should be the default or changed at the same time. We have a programming error otherwise. + if ((*gesture_id == UINT16_MAX) != (*offset == ACCEL_NO_VALID_GESTURE)) { return ACCEL_INTERNAL_ERROR; } @@ -557,7 +555,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off *gesture_id = i; } } - if (*gesture_id == ACCEL_NO_VALID_GESTURE || *offset == ACCEL_NO_VALID_GESTURE) { + if (*gesture_id == UINT16_MAX || *offset == ACCEL_NO_VALID_GESTURE) { return ACCEL_NO_VALID_GESTURE; } return ACCEL_SUCCESS; diff --git a/sample/simple-accelerometer/src/accel.h b/sample/simple-accelerometer/src/accel.h index 73106f6..3e79768 100644 --- a/sample/simple-accelerometer/src/accel.h +++ b/sample/simple-accelerometer/src/accel.h @@ -98,30 +98,30 @@ int accel_generate_state(accel_state **state, uint32_t dimensions, uint16_t wind int accel_destroy_state(accel_state **state); /** - * Starts recording a accel gesture + * Starts recording an accel gesture * @param state A pointer to a non-NULL state variable that holds recording * metadata. * @param gesture Non-NULL pointer that will be populated with the gesture id. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_start_record_gesture(accel_state *state, int *gesture); +int accel_start_record_gesture(accel_state *state, uint16_t *gesture); /** - * Ends recording a accel gesture + * Ends recording an accel gesture * @param state A pointer to a non-NULL state variable that holds recording * metadata. * @param gesture_id Value that corresponds to a gesture currently being * recorded. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_end_record_gesture(accel_state *state, int gesture_id); +int accel_end_record_gesture(accel_state *state, uint16_t gesture_id); /** - * Updates the state variable's current state based on the accel data array - * passed in. + * Updates the state variable's current state based on the raw accelerometer + * data array passed in. * @param state A pointer to a non-NULL state variable that holds * recording metadata. - * @param accel_data An with accelerometer data. + * @param accel_data A state->dimensions array with accelerometer data. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ int accel_process_timer_tick(accel_state *state, int *accel_data); @@ -137,7 +137,7 @@ int accel_process_timer_tick(accel_state *state, int *accel_data); * distance corresponding to the returned gesture. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *distance); +int accel_find_most_likely_gesture(accel_state *state, uint16_t *gesture_id, int *distance); /** * For a given state and recorded gesture, resets the gesture's offset state diff --git a/sample/simple-accelerometer/src/moving_avg_ticker.h b/sample/simple-accelerometer/src/moving_avg_ticker.h index 82ecba9..b533415 100644 --- a/sample/simple-accelerometer/src/moving_avg_ticker.h +++ b/sample/simple-accelerometer/src/moving_avg_ticker.h @@ -18,7 +18,7 @@ typedef struct moving_avg_values { // Circular buffer int *wbuf; int wbuf_end; - int wbuf_len; + uint16_t wbuf_len; int subtotal; int subtotal_size; diff --git a/sample/simple-accelerometer/src/simple-accelerometer.c b/sample/simple-accelerometer/src/simple-accelerometer.c index d968f01..1a162e6 100644 --- a/sample/simple-accelerometer/src/simple-accelerometer.c +++ b/sample/simple-accelerometer/src/simple-accelerometer.c @@ -1,4 +1,5 @@ #include +#include #include "accel.h" @@ -6,7 +7,7 @@ static Window *window; static TextLayer *text_layer; static accel_state *state = NULL; -static int recording_gesture = 0; +static uint16_t recording_gesture = 0; static bool recording = false; static void select_click_handler(ClickRecognizerRef recognizer, void *context) { @@ -59,7 +60,7 @@ static void down_click_handler(ClickRecognizerRef recognizer, void *context) { if (result == ACCEL_SUCCESS) { text_layer_set_text(text_layer, "Successful tick"); - int gesture_id = 0; + uint16_t gesture_id = 0; int distance = 0; accel_find_most_likely_gesture(state, &gesture_id, &distance); APP_LOG(APP_LOG_LEVEL_INFO, "Distance: %i, gesture: %i", distance, gesture_id); diff --git a/src/accel.c b/src/accel.c index 8598776..8e7cdaa 100644 --- a/src/accel.c +++ b/src/accel.c @@ -237,7 +237,7 @@ int accel_destroy_state(accel_state **state) { return ACCEL_SUCCESS; } -int accel_start_record_gesture(accel_state *state, int *gesture) { +int accel_start_record_gesture(accel_state *state, uint16_t *gesture) { PRECONDITION_VALID_STATE(state); PRECONDITION_NOT_NULL(gesture); @@ -261,7 +261,6 @@ int accel_start_record_gesture(accel_state *state, int *gesture) { int result = accel_generate_gesture(state, &(state->state->gestures[*gesture])); if (result != ACCEL_SUCCESS) { - *gesture = -1; if (state->state->num_gestures_saved == 1) { free(state->state->gestures); state->state->gestures = NULL; @@ -284,7 +283,7 @@ int accel_start_record_gesture(accel_state *state, int *gesture) { // http://www.hackersdelight.org/hdcodetxt/icbrt.c.txt uint32_t icbrt1(uint32_t x) { int32_t s; - unsigned y, b; + uint32_t y, b; y = 0; for (s = 30; s >= 0; s = s - 3) { @@ -303,9 +302,9 @@ uint32_t icbrt1(uint32_t x) { // TODO: revisit this decision. int32_t normalize(int32_t sum) { if (sum < 0) { - return -1 * (int32_t)icbrt1((uint32_t)(-sum)); + return (int)-cbrtf(-((float)sum)); } - return (int32_t)icbrt1((uint32_t)sum); + return -(int)-cbrtf(((float)sum)); } int reset_gesture(accel_gesture *gest, const uint32_t dimensions) { @@ -320,14 +319,12 @@ int reset_gesture(accel_gesture *gest, const uint32_t dimensions) { } // TODO: does this work for zero recorded timestamps? -int accel_end_record_gesture(accel_state *state, int gesture_id) { +int accel_end_record_gesture(accel_state *state, uint16_t gesture_id) { PRECONDITION_VALID_STATE(state); - // TODO: use an unsigned int instead so we don't need to check for this type of error. - if (gesture_id < 0) { - return ACCEL_PARAM_ERROR; - } internal_accel_state *istate = state->state; + // TODO: gesture_id == istate->num_gestures_saved is incorrect. + // Write a test for it and fix it. if (gesture_id > istate->num_gestures_saved) { return ACCEL_PARAM_ERROR; } @@ -514,12 +511,12 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { return retcode; } -int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *offset) { +int accel_find_most_likely_gesture(accel_state *state, uint16_t *gesture_id, int *offset) { PRECONDITION_VALID_STATE(state); PRECONDITION_NOT_NULL(gesture_id); PRECONDITION_NOT_NULL(offset); - *gesture_id = ACCEL_NO_VALID_GESTURE; + *gesture_id = UINT16_MAX; *offset = ACCEL_NO_VALID_GESTURE; if (state->state->num_gestures_saved == 0) { @@ -530,7 +527,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off return ACCEL_INTERNAL_ERROR; } - for (int i = 0; i < state->state->num_gestures_saved; ++i) { + for (uint16_t i = 0; i < state->state->num_gestures_saved; ++i) { accel_gesture *gesture = state->state->gestures[i]; // TODO: Should this be tested? @@ -538,7 +535,8 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off return ACCEL_INTERNAL_ERROR; } - if ((*gesture_id == ACCEL_NO_VALID_GESTURE || *offset == ACCEL_NO_VALID_GESTURE) && *gesture_id != *offset) { + // Both should be the default or changed at the same time. We have a programming error otherwise. + if ((*gesture_id == UINT16_MAX) != (*offset == ACCEL_NO_VALID_GESTURE)) { return ACCEL_INTERNAL_ERROR; } @@ -557,7 +555,7 @@ int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *off *gesture_id = i; } } - if (*gesture_id == ACCEL_NO_VALID_GESTURE || *offset == ACCEL_NO_VALID_GESTURE) { + if (*gesture_id == UINT16_MAX || *offset == ACCEL_NO_VALID_GESTURE) { return ACCEL_NO_VALID_GESTURE; } return ACCEL_SUCCESS; diff --git a/src/accel.h b/src/accel.h index 73106f6..3e79768 100644 --- a/src/accel.h +++ b/src/accel.h @@ -98,30 +98,30 @@ int accel_generate_state(accel_state **state, uint32_t dimensions, uint16_t wind int accel_destroy_state(accel_state **state); /** - * Starts recording a accel gesture + * Starts recording an accel gesture * @param state A pointer to a non-NULL state variable that holds recording * metadata. * @param gesture Non-NULL pointer that will be populated with the gesture id. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_start_record_gesture(accel_state *state, int *gesture); +int accel_start_record_gesture(accel_state *state, uint16_t *gesture); /** - * Ends recording a accel gesture + * Ends recording an accel gesture * @param state A pointer to a non-NULL state variable that holds recording * metadata. * @param gesture_id Value that corresponds to a gesture currently being * recorded. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_end_record_gesture(accel_state *state, int gesture_id); +int accel_end_record_gesture(accel_state *state, uint16_t gesture_id); /** - * Updates the state variable's current state based on the accel data array - * passed in. + * Updates the state variable's current state based on the raw accelerometer + * data array passed in. * @param state A pointer to a non-NULL state variable that holds * recording metadata. - * @param accel_data An with accelerometer data. + * @param accel_data A state->dimensions array with accelerometer data. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ int accel_process_timer_tick(accel_state *state, int *accel_data); @@ -137,7 +137,7 @@ int accel_process_timer_tick(accel_state *state, int *accel_data); * distance corresponding to the returned gesture. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_find_most_likely_gesture(accel_state *state, int *gesture_id, int *distance); +int accel_find_most_likely_gesture(accel_state *state, uint16_t *gesture_id, int *distance); /** * For a given state and recorded gesture, resets the gesture's offset state diff --git a/src/moving_avg_ticker.h b/src/moving_avg_ticker.h index 82ecba9..b533415 100644 --- a/src/moving_avg_ticker.h +++ b/src/moving_avg_ticker.h @@ -18,7 +18,7 @@ typedef struct moving_avg_values { // Circular buffer int *wbuf; int wbuf_end; - int wbuf_len; + uint16_t wbuf_len; int subtotal; int subtotal_size; diff --git a/test/main.cc b/test/main.cc index af6d3ee..b7c99f0 100644 --- a/test/main.cc +++ b/test/main.cc @@ -125,7 +125,7 @@ return ACCEL_SUCCESS; } TEST(AccelFuzzTest, accel_generate_state_valid_callback) { - int gesture_id = 0; + uint16_t gesture_id = 0; accel_state *state = NULL; // Non-null callback, watch it iterate over this stuff. @@ -170,7 +170,7 @@ TEST(AccelFuzzTest, accel_destroy_state_invalid_input) { TEST(AccelFuzzTest, accel_start_record_gesture_invalid_input) { int result = 0; - int gesture_id = 0; + uint16_t gesture_id = 0; result = accel_start_record_gesture(NULL, &gesture_id); EXPECT_EQ(ACCEL_PARAM_ERROR, result); @@ -204,7 +204,7 @@ TEST(AccelFuzzTest, accel_end_record_gesture_invalid_input) { // Verify it works for valid indexes. state = test_fabricate_1d_state(); - int gesture = 1234; + uint16_t gesture = 1234; result = accel_start_record_gesture(state, &gesture); EXPECT_NE(1234, gesture); EXPECT_EQ(ACCEL_SUCCESS, result); @@ -234,7 +234,7 @@ TEST(AccelFuzzTest, accel_process_timer_tick_invalid_input) { TEST(AccelFuzzTest, accel_find_most_likely_gesture_invalid_input) { int result = 0; - int gesture_id = 0; + uint16_t gesture_id = 0; int affinity = 0; accel_state *state = NULL; @@ -257,7 +257,7 @@ TEST(AccelFuzzTest, accel_find_most_likely_gesture_invalid_input) { // No tests exist, but otherwise valid parameters. state = test_fabricate_1d_state(); result = accel_find_most_likely_gesture(state, &gesture_id, &affinity); - EXPECT_EQ(ACCEL_NO_VALID_GESTURE, gesture_id); + EXPECT_EQ(UINT16_MAX, gesture_id); EXPECT_EQ(ACCEL_NO_VALID_GESTURE, affinity); EXPECT_EQ(ACCEL_NO_VALID_GESTURE, result); } @@ -278,7 +278,7 @@ TEST(AccelTest, start_recording_and_close_many_gestures) { int data[1] = {0}; for (int i = 0; i < 10; ++i) { - int gesture = 0; + uint16_t gesture = 0; ASSERT_EQ(0, accel_start_record_gesture(state, &gesture)); ASSERT_EQ(i, gesture); ASSERT_EQ(0, accel_process_timer_tick(state, data)); @@ -293,7 +293,7 @@ TEST(AccelTest, record_incredibly_long_sequence) { accel_state *state = NULL; state = test_fabricate_1d_state(); - int gesture = 0; + uint16_t gesture = 0; EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture)); EXPECT_EQ(ACCEL_SUCCESS, gesture); @@ -310,7 +310,7 @@ TEST(AccelTest, end_to_end_test_single_recording) { accel_state *state = NULL; state = test_fabricate_1d_state(); - int gesture = 0; + uint16_t gesture = 0; EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture)); EXPECT_EQ(ACCEL_SUCCESS, gesture); @@ -325,7 +325,7 @@ TEST(AccelTest, end_to_end_test_single_recording) { int prev_affinity = 0; for (int i = 0; i < 10; ++i) { data[0] = i * 100; - int gesture_found = 1; + uint16_t gesture_found = 1; int affinity_of_gesture = 1; ASSERT_EQ(0, accel_process_timer_tick(state, data)); ASSERT_EQ(0, accel_find_most_likely_gesture(state, &gesture_found, &affinity_of_gesture)); @@ -344,7 +344,7 @@ TEST(AccelTest, end_to_end_test_multiple_recordings) { accel_state *state = NULL; state = test_fabricate_1d_state(); - int first_gesture = 0; + uint16_t first_gesture = 0; EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &first_gesture)); EXPECT_EQ(ACCEL_SUCCESS, first_gesture); @@ -356,7 +356,7 @@ TEST(AccelTest, end_to_end_test_multiple_recordings) { EXPECT_EQ(ACCEL_SUCCESS, accel_end_record_gesture(state, first_gesture)); - int second_gesture = 0; + uint16_t second_gesture = 0; EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &second_gesture)); EXPECT_NE(first_gesture, second_gesture); @@ -370,7 +370,7 @@ TEST(AccelTest, end_to_end_test_multiple_recordings) { int prev_affinity = 0; for (int i = 0; i < 10; ++i) { data[0] = i * 2; - int gesture_found = 1; + uint16_t gesture_found = 1; int affinity_of_gesture = 1; ASSERT_EQ(0, accel_process_timer_tick(state, data)); ASSERT_EQ(0, accel_find_most_likely_gesture(state, &gesture_found, &affinity_of_gesture)); @@ -391,7 +391,7 @@ TEST(AccelTest, test_fuzz_reset_affinities) { state = test_fabricate_1d_state(); EXPECT_EQ(ACCEL_PARAM_ERROR, accel_reset_affinities_for_gesture(state, 0)); - int gesture_id = 0; + uint16_t gesture_id = 0; EXPECT_EQ(ACCEL_SUCCESS, accel_start_record_gesture(state, &gesture_id)); // A recording gesture with no data. @@ -410,7 +410,7 @@ TEST(AccelTest, test_fuzz_reset_affinities) { // No ticks have been recorded. EXPECT_EQ(ACCEL_SUCCESS, accel_reset_affinities_for_gesture(state, gesture_id)); - int gesture = 1; + uint16_t gesture = 1; int initial_distance = 1; int after_run_distance = 1; int after_reset_distance = 1; From 79ad48c280b09f378884c0a809652668d44170e2 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Wed, 26 Mar 2014 22:23:12 -0700 Subject: [PATCH 65/68] updated phony targets --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 3cd9336..8460dc0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ # TODO: update this. -PHONY = default echo/objects echo/sources list tests bin/tests run bin clean +.PHONY = default echo/objects echo/sources list tests run bin clean SOURCES_C = $(wildcard src/*.c) TEST_CC = $(wildcard test/*.cc) @@ -28,7 +28,7 @@ echo/sources: @echo ${TEST_CC} list: - @echo $(PHONY) + @echo $(.PHONY) tests: bin/tests From 147b371f7539a999d7c1124e5c15bde46df95a3f Mon Sep 17 00:00:00 2001 From: shalecraig Date: Wed, 26 Mar 2014 22:23:53 -0700 Subject: [PATCH 66/68] Added more compilation flags -Wincompatible-pointer-types -Wtype-limits -Werror=type-limits --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8460dc0..97d0aa7 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ TEST_OBJECTS = $(subst test,bin,$(_TEST_OBJECTS_TMP)) EXEC = tests # removed: -Wl,-z,relro -Wl,-z,now -C_ARGS = -ansi -DIS_NOT_PEBBLE -DRELEASE -ffunction-sections -fno-exceptions -fPIC -fPIE -fstack-protector-all -fstrict-overflow -fvisibility=hidden -g -m64 -Os -pedantic-errors -pipe -std=c99 -W -Wall -Wbad-function-cast -Wc++-compat -Wcast-qual -Wcomment -Wconversion -Wdeprecated -Werror -Wextra -Wfloat-equal -Wformat -Wformat-nonliteral -Wformat-security -Wformat-y2k -Winit-self -Wmissing-include-dirs -Wmultichar -Wnested-externs -Wold-style-definition -Wpacked -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsign-compare -Wstack-protector -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wtrigraphs -Wundef -Wuninitialized -Wuninitialized -Wunused-function -Wunused-label -Wunused-macros -Wunused-parameter -Wvariadic-macros -Wwrite-strings -Wlong-long +C_ARGS = -ansi -DIS_NOT_PEBBLE -DRELEASE -ffunction-sections -fno-exceptions -fPIC -fPIE -fstack-protector-all -fstrict-overflow -fvisibility=hidden -g -m64 -Os -pedantic-errors -pipe -std=c99 -W -Wall -Wbad-function-cast -Wc++-compat -Wcast-qual -Wcomment -Wconversion -Wdeprecated -Werror -Wextra -Wfloat-equal -Wformat -Wformat-nonliteral -Wformat-security -Wformat-y2k -Winit-self -Wmissing-include-dirs -Wmultichar -Wnested-externs -Wold-style-definition -Wpacked -Wpointer-arith -Wredundant-decls -Wreturn-type -Wsign-compare -Wstack-protector -Wstrict-prototypes -Wswitch-default -Wswitch-enum -Wtrigraphs -Wundef -Wuninitialized -Wuninitialized -Wunused-function -Wunused-label -Wunused-macros -Wunused-parameter -Wvariadic-macros -Wwrite-strings -Wlong-long -Wincompatible-pointer-types -Wtype-limits -Werror=type-limits # removed: -lpthread, -Wl,-z,relro -Wl,-z,now -Wsign-conversion CXX_ARGS = -DIS_NOT_PEBBLE -fno-exceptions -fPIC -fstack-protector -fvisibility=hidden -g -lpthread -m64 -O3 -pipe -W -Wall -Wextra -Wextra-tokens -Wconversion -Wformat -Wformat-nonliteral -Wformat-security -Winit-self -Wmultichar -Wno-deprecated -Wno-unused-function -Wno-unused-label -Wno-unused-parameter -Wpointer-arith -Wreturn-type -Wsign-compare -Wuninitialized -Wcast-qual -Wsign-compare -Wno-sign-conversion -Wno-conversion From 8405f3d1c8158e8922442a56e827bc1087cf6c36 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Wed, 26 Mar 2014 22:46:00 -0700 Subject: [PATCH 67/68] Specific types are best types: - accel_callback takes a uint16_t gesture_id - accel_callback takes a uint32_t offset_found --- sample/simple-accelerometer/src/accel.c | 13 ++++++++----- sample/simple-accelerometer/src/accel.h | 6 ++++-- src/accel.c | 13 ++++++++----- src/accel.h | 6 ++++-- test/main.cc | 4 ++-- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/sample/simple-accelerometer/src/accel.c b/sample/simple-accelerometer/src/accel.c index 8e7cdaa..2880ba5 100644 --- a/sample/simple-accelerometer/src/accel.c +++ b/sample/simple-accelerometer/src/accel.c @@ -12,7 +12,7 @@ #define my_calloc(a, b) (calloc(a, b)) #endif -// cbrt is defined and importable for everybody! +// cbrt/abs is defined and importable for everybody! #include #include #include @@ -395,7 +395,7 @@ void handle_recording_tick(accel_gesture *gesture, uint32_t dimensions) { ++(gesture->recording_size); } -int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gesture_id) { +int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, uint16_t gesture_id) { // TODO: load the input at the beginning instead of gesture->recording_size times. PRECONDITION_NOT_NULL(gesture); uint32_t dimensions = state->dimensions; @@ -449,9 +449,12 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu return ACCEL_PARAM_ERROR; } float avg_affinity = ((float)gesture->offsets[gesture->recording_size - 1]) / gesture->recording_size; + if (avg_affinity < 0) { + return ACCEL_INTERNAL_ERROR; + } if (avg_affinity < state->state->threshold) { bool reset; - int retval = state->callback(state, gesture_id, (int32_t)avg_affinity, &reset); + int retval = state->callback(state, gesture_id, (uint32_t)(avg_affinity), &reset); if (reset == true) { reset_gesture(gesture, dimensions); } @@ -466,7 +469,7 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { PRECONDITION_NOT_NULL(accel_data); int retcode = ACCEL_SUCCESS; - for (int gesture_id = 0; gesture_id < state->state->num_gestures_saved; ++gesture_id) { + for (uint16_t gesture_id = 0; gesture_id < state->state->num_gestures_saved; ++gesture_id) { accel_gesture *gesture = state->state->gestures[gesture_id]; if (gesture == NULL) { retcode = ACCEL_INTERNAL_ERROR; @@ -561,7 +564,7 @@ int accel_find_most_likely_gesture(accel_state *state, uint16_t *gesture_id, int return ACCEL_SUCCESS; } -int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id) { +int accel_reset_affinities_for_gesture(accel_state *state, uint16_t gesture_id) { PRECONDITION_VALID_STATE(state); PRECONDITION_NOT_NULL(state->state); PRECONDITION_TRUE_PARAM((state->state->num_gestures_saved > gesture_id)); diff --git a/sample/simple-accelerometer/src/accel.h b/sample/simple-accelerometer/src/accel.h index 3e79768..47ffdaa 100644 --- a/sample/simple-accelerometer/src/accel.h +++ b/sample/simple-accelerometer/src/accel.h @@ -59,7 +59,8 @@ struct accelState; * refer to the ACCEL_MIN_RESERVED definition inside * their implementations. */ -typedef int (*accel_callback)(struct accelState *state, int gesture_id, int offset_found, bool *reset_gesture); +typedef int (*accel_callback)(struct accelState *state, uint16_t gesture_id, uint32_t offset_found, + bool *reset_gesture); // TODO: define this as a partial-type instead of exposing some fields. typedef struct accelState { @@ -137,6 +138,7 @@ int accel_process_timer_tick(accel_state *state, int *accel_data); * distance corresponding to the returned gesture. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ +// TODO: rename distance to offset int accel_find_most_likely_gesture(accel_state *state, uint16_t *gesture_id, int *distance); /** @@ -147,7 +149,7 @@ int accel_find_most_likely_gesture(accel_state *state, uint16_t *gesture_id, int * @param gesture_id Value that corresponds to a gesture currently being reset. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); +int accel_reset_affinities_for_gesture(accel_state *state, uint16_t gesture_id); #ifdef __cplusplus } diff --git a/src/accel.c b/src/accel.c index 8e7cdaa..2880ba5 100644 --- a/src/accel.c +++ b/src/accel.c @@ -12,7 +12,7 @@ #define my_calloc(a, b) (calloc(a, b)) #endif -// cbrt is defined and importable for everybody! +// cbrt/abs is defined and importable for everybody! #include #include #include @@ -395,7 +395,7 @@ void handle_recording_tick(accel_gesture *gesture, uint32_t dimensions) { ++(gesture->recording_size); } -int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gesture_id) { +int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, uint16_t gesture_id) { // TODO: load the input at the beginning instead of gesture->recording_size times. PRECONDITION_NOT_NULL(gesture); uint32_t dimensions = state->dimensions; @@ -449,9 +449,12 @@ int handle_evaluation_tick(accel_state *state, accel_gesture *gesture, int gestu return ACCEL_PARAM_ERROR; } float avg_affinity = ((float)gesture->offsets[gesture->recording_size - 1]) / gesture->recording_size; + if (avg_affinity < 0) { + return ACCEL_INTERNAL_ERROR; + } if (avg_affinity < state->state->threshold) { bool reset; - int retval = state->callback(state, gesture_id, (int32_t)avg_affinity, &reset); + int retval = state->callback(state, gesture_id, (uint32_t)(avg_affinity), &reset); if (reset == true) { reset_gesture(gesture, dimensions); } @@ -466,7 +469,7 @@ int accel_process_timer_tick(accel_state *state, int *accel_data) { PRECONDITION_NOT_NULL(accel_data); int retcode = ACCEL_SUCCESS; - for (int gesture_id = 0; gesture_id < state->state->num_gestures_saved; ++gesture_id) { + for (uint16_t gesture_id = 0; gesture_id < state->state->num_gestures_saved; ++gesture_id) { accel_gesture *gesture = state->state->gestures[gesture_id]; if (gesture == NULL) { retcode = ACCEL_INTERNAL_ERROR; @@ -561,7 +564,7 @@ int accel_find_most_likely_gesture(accel_state *state, uint16_t *gesture_id, int return ACCEL_SUCCESS; } -int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id) { +int accel_reset_affinities_for_gesture(accel_state *state, uint16_t gesture_id) { PRECONDITION_VALID_STATE(state); PRECONDITION_NOT_NULL(state->state); PRECONDITION_TRUE_PARAM((state->state->num_gestures_saved > gesture_id)); diff --git a/src/accel.h b/src/accel.h index 3e79768..47ffdaa 100644 --- a/src/accel.h +++ b/src/accel.h @@ -59,7 +59,8 @@ struct accelState; * refer to the ACCEL_MIN_RESERVED definition inside * their implementations. */ -typedef int (*accel_callback)(struct accelState *state, int gesture_id, int offset_found, bool *reset_gesture); +typedef int (*accel_callback)(struct accelState *state, uint16_t gesture_id, uint32_t offset_found, + bool *reset_gesture); // TODO: define this as a partial-type instead of exposing some fields. typedef struct accelState { @@ -137,6 +138,7 @@ int accel_process_timer_tick(accel_state *state, int *accel_data); * distance corresponding to the returned gesture. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ +// TODO: rename distance to offset int accel_find_most_likely_gesture(accel_state *state, uint16_t *gesture_id, int *distance); /** @@ -147,7 +149,7 @@ int accel_find_most_likely_gesture(accel_state *state, uint16_t *gesture_id, int * @param gesture_id Value that corresponds to a gesture currently being reset. * @return ACCEL_SUCCESS if successful, an error code otherwise. */ -int accel_reset_affinities_for_gesture(accel_state *state, int gesture_id); +int accel_reset_affinities_for_gesture(accel_state *state, uint16_t gesture_id); #ifdef __cplusplus } diff --git a/test/main.cc b/test/main.cc index b7c99f0..41b9b32 100644 --- a/test/main.cc +++ b/test/main.cc @@ -118,8 +118,8 @@ TEST(AccelFuzzTest, accel_generate_state_null_callback) { result = accel_generate_state(&state, 1, 1, NULL, 0); } -TEST_CALLBACK(int, AccelFuzzTest, accel_generate_state_valid_callback, myTest, accel_state *state, int gesture_id, - int offset_found, bool *reset_gesture) +TEST_CALLBACK(int, AccelFuzzTest, accel_generate_state_valid_callback, myTest, accel_state *state, uint16_t gesture_id, + uint32_t offset_found, bool *reset_gesture) *reset_gesture = true; return ACCEL_SUCCESS; } From b1580473fee1f0998d31fe470662902c1392ed61 Mon Sep 17 00:00:00 2001 From: shalecraig Date: Wed, 26 Mar 2014 23:07:54 -0700 Subject: [PATCH 68/68] Moved the last few major types over to explicit types: - allocate_moving_avg takes an uint16_t subtotal_sizes - Created a "data type" for the type stored in the moving_avg --- .../src/moving_avg_ticker.c | 16 +++++--------- .../src/moving_avg_ticker.h | 22 ++++++++++++------- src/moving_avg_ticker.c | 16 +++++--------- src/moving_avg_ticker.h | 22 ++++++++++++------- test/main.cc | 3 --- 5 files changed, 38 insertions(+), 41 deletions(-) diff --git a/sample/simple-accelerometer/src/moving_avg_ticker.c b/sample/simple-accelerometer/src/moving_avg_ticker.c index 050eed3..63a1ed0 100644 --- a/sample/simple-accelerometer/src/moving_avg_ticker.c +++ b/sample/simple-accelerometer/src/moving_avg_ticker.c @@ -15,15 +15,9 @@ int precondition_valid_moving_avg_values(moving_avg_values *input) { if (input->wbuf == NULL) { return MOVING_AVG_INTERNAL_ERROR; } - if (input->wbuf_end < 0) { - return MOVING_AVG_INTERNAL_ERROR; - } if (input->wbuf_len <= 0) { return MOVING_AVG_INTERNAL_ERROR; } - if (input->subtotal_size < 0) { - return MOVING_AVG_INTERNAL_ERROR; - } if (input->subtotal_size >= input->max_subtotal_size) { return MOVING_AVG_INTERNAL_ERROR; } @@ -33,7 +27,7 @@ int precondition_valid_moving_avg_values(moving_avg_values *input) { return MOVING_AVG_SUCCESS; } -int allocate_moving_avg(uint16_t num_wbuf, int subtotal_sizes, moving_avg_values **allocated) { +int allocate_moving_avg(uint16_t num_wbuf, uint16_t subtotal_sizes, moving_avg_values **allocated) { PRECONDITION_NOT_NULL(allocated); if (*allocated != NULL) { return MOVING_AVG_PARAM_ERROR; @@ -54,7 +48,7 @@ int allocate_moving_avg(uint16_t num_wbuf, int subtotal_sizes, moving_avg_values memset(*allocated, 0, size); (*allocated)->max_subtotal_size = subtotal_sizes; - int *wbuf = (int *)calloc(num_wbuf, sizeof(int)); + moving_avg_data_type *wbuf = (moving_avg_data_type *)calloc(num_wbuf, sizeof(moving_avg_data_type)); if (wbuf == NULL) { // Run away, fast! free(allocated); @@ -79,7 +73,7 @@ int reset_moving_avg(moving_avg_values *reset) { return MOVING_AVG_SUCCESS; } -int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end) { +int append_to_moving_avg(moving_avg_values *value, moving_avg_data_type appended, bool *is_at_end) { int is_valid_return_value = precondition_valid_moving_avg_values(value); if (is_valid_return_value != MOVING_AVG_SUCCESS) { return is_valid_return_value; @@ -103,7 +97,7 @@ int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end return MOVING_AVG_SUCCESS; } -int get_latest_frame_moving_avg(moving_avg_values *value, int32_t *frame) { +int get_latest_frame_moving_avg(moving_avg_values *value, moving_avg_data_type *frame) { int is_valid_return_value = precondition_valid_moving_avg_values(value); if (is_valid_return_value != MOVING_AVG_SUCCESS) { return is_valid_return_value; @@ -112,7 +106,7 @@ int get_latest_frame_moving_avg(moving_avg_values *value, int32_t *frame) { PRECONDITION_NOT_NULL(frame); float sum = 0; - for (int i = 0; i < value->wbuf_len; ++i) { + for (uint16_t i = 0; i < value->wbuf_len; ++i) { sum += value->wbuf[i] * 1.0 / value->wbuf_len; } *frame = (int32_t)sum; diff --git a/sample/simple-accelerometer/src/moving_avg_ticker.h b/sample/simple-accelerometer/src/moving_avg_ticker.h index b533415..4d081b0 100644 --- a/sample/simple-accelerometer/src/moving_avg_ticker.h +++ b/sample/simple-accelerometer/src/moving_avg_ticker.h @@ -14,24 +14,30 @@ extern "C" { #define MOVING_AVG_INTERNAL_ERROR ACCEL_INTERNAL_ERROR #define MOVING_AVG_MALLOC_ERROR ACCEL_MALLOC_ERROR +#ifndef MOVING_AVG_DATA_TYPE +#define MOVING_AVG_DATA_TYPE int32_t +#endif + +typedef MOVING_AVG_DATA_TYPE moving_avg_data_type; + typedef struct moving_avg_values { // Circular buffer - int *wbuf; - int wbuf_end; + moving_avg_data_type *wbuf; + uint16_t wbuf_end; uint16_t wbuf_len; - int subtotal; - int subtotal_size; - int max_subtotal_size; + moving_avg_data_type subtotal; + uint16_t subtotal_size; + uint16_t max_subtotal_size; } moving_avg_values; -int allocate_moving_avg(uint16_t num_wbuf, int subtotal_sizes, moving_avg_values **allocated); +int allocate_moving_avg(uint16_t num_wbuf, uint16_t subtotal_sizes, moving_avg_values **allocated); int reset_moving_avg(moving_avg_values *reset); -int append_to_moving_avg(moving_avg_values *value, int appended, bool *isAtEnd); +int append_to_moving_avg(moving_avg_values *value, moving_avg_data_type appended, bool *is_at_end); -int get_latest_frame_moving_avg(moving_avg_values *value, int32_t *frame); +int get_latest_frame_moving_avg(moving_avg_values *value, moving_avg_data_type *frame); int free_moving_avg(moving_avg_values **value); diff --git a/src/moving_avg_ticker.c b/src/moving_avg_ticker.c index 050eed3..63a1ed0 100644 --- a/src/moving_avg_ticker.c +++ b/src/moving_avg_ticker.c @@ -15,15 +15,9 @@ int precondition_valid_moving_avg_values(moving_avg_values *input) { if (input->wbuf == NULL) { return MOVING_AVG_INTERNAL_ERROR; } - if (input->wbuf_end < 0) { - return MOVING_AVG_INTERNAL_ERROR; - } if (input->wbuf_len <= 0) { return MOVING_AVG_INTERNAL_ERROR; } - if (input->subtotal_size < 0) { - return MOVING_AVG_INTERNAL_ERROR; - } if (input->subtotal_size >= input->max_subtotal_size) { return MOVING_AVG_INTERNAL_ERROR; } @@ -33,7 +27,7 @@ int precondition_valid_moving_avg_values(moving_avg_values *input) { return MOVING_AVG_SUCCESS; } -int allocate_moving_avg(uint16_t num_wbuf, int subtotal_sizes, moving_avg_values **allocated) { +int allocate_moving_avg(uint16_t num_wbuf, uint16_t subtotal_sizes, moving_avg_values **allocated) { PRECONDITION_NOT_NULL(allocated); if (*allocated != NULL) { return MOVING_AVG_PARAM_ERROR; @@ -54,7 +48,7 @@ int allocate_moving_avg(uint16_t num_wbuf, int subtotal_sizes, moving_avg_values memset(*allocated, 0, size); (*allocated)->max_subtotal_size = subtotal_sizes; - int *wbuf = (int *)calloc(num_wbuf, sizeof(int)); + moving_avg_data_type *wbuf = (moving_avg_data_type *)calloc(num_wbuf, sizeof(moving_avg_data_type)); if (wbuf == NULL) { // Run away, fast! free(allocated); @@ -79,7 +73,7 @@ int reset_moving_avg(moving_avg_values *reset) { return MOVING_AVG_SUCCESS; } -int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end) { +int append_to_moving_avg(moving_avg_values *value, moving_avg_data_type appended, bool *is_at_end) { int is_valid_return_value = precondition_valid_moving_avg_values(value); if (is_valid_return_value != MOVING_AVG_SUCCESS) { return is_valid_return_value; @@ -103,7 +97,7 @@ int append_to_moving_avg(moving_avg_values *value, int appended, bool *is_at_end return MOVING_AVG_SUCCESS; } -int get_latest_frame_moving_avg(moving_avg_values *value, int32_t *frame) { +int get_latest_frame_moving_avg(moving_avg_values *value, moving_avg_data_type *frame) { int is_valid_return_value = precondition_valid_moving_avg_values(value); if (is_valid_return_value != MOVING_AVG_SUCCESS) { return is_valid_return_value; @@ -112,7 +106,7 @@ int get_latest_frame_moving_avg(moving_avg_values *value, int32_t *frame) { PRECONDITION_NOT_NULL(frame); float sum = 0; - for (int i = 0; i < value->wbuf_len; ++i) { + for (uint16_t i = 0; i < value->wbuf_len; ++i) { sum += value->wbuf[i] * 1.0 / value->wbuf_len; } *frame = (int32_t)sum; diff --git a/src/moving_avg_ticker.h b/src/moving_avg_ticker.h index b533415..4d081b0 100644 --- a/src/moving_avg_ticker.h +++ b/src/moving_avg_ticker.h @@ -14,24 +14,30 @@ extern "C" { #define MOVING_AVG_INTERNAL_ERROR ACCEL_INTERNAL_ERROR #define MOVING_AVG_MALLOC_ERROR ACCEL_MALLOC_ERROR +#ifndef MOVING_AVG_DATA_TYPE +#define MOVING_AVG_DATA_TYPE int32_t +#endif + +typedef MOVING_AVG_DATA_TYPE moving_avg_data_type; + typedef struct moving_avg_values { // Circular buffer - int *wbuf; - int wbuf_end; + moving_avg_data_type *wbuf; + uint16_t wbuf_end; uint16_t wbuf_len; - int subtotal; - int subtotal_size; - int max_subtotal_size; + moving_avg_data_type subtotal; + uint16_t subtotal_size; + uint16_t max_subtotal_size; } moving_avg_values; -int allocate_moving_avg(uint16_t num_wbuf, int subtotal_sizes, moving_avg_values **allocated); +int allocate_moving_avg(uint16_t num_wbuf, uint16_t subtotal_sizes, moving_avg_values **allocated); int reset_moving_avg(moving_avg_values *reset); -int append_to_moving_avg(moving_avg_values *value, int appended, bool *isAtEnd); +int append_to_moving_avg(moving_avg_values *value, moving_avg_data_type appended, bool *is_at_end); -int get_latest_frame_moving_avg(moving_avg_values *value, int32_t *frame); +int get_latest_frame_moving_avg(moving_avg_values *value, moving_avg_data_type *frame); int free_moving_avg(moving_avg_values **value); diff --git a/test/main.cc b/test/main.cc index 41b9b32..bfadb96 100644 --- a/test/main.cc +++ b/test/main.cc @@ -658,9 +658,6 @@ TEST(MovingAvgTickerFuzzTest, allocate_moving_avg) { // Test with zero num_wbuf EXPECT_EQ(MOVING_AVG_PARAM_ERROR, allocate_moving_avg(0, 1, &allocated)); - // Test with negative subtotal_size - EXPECT_EQ(MOVING_AVG_PARAM_ERROR, allocate_moving_avg(1, -1, &allocated)); - // Test with zero subtotal_size EXPECT_EQ(MOVING_AVG_PARAM_ERROR, allocate_moving_avg(1, 0, &allocated));