Skip to content

Commit 957cc1e

Browse files
authored
Merge pull request #167 from saxbophone/josh/107-track-time-spent
Josh/107 track time spent
2 parents ed1bc1d + 8e00445 commit 957cc1e

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

sxbp/initialise.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ sxbp_direction_t sxbp_change_direction(
2727
}
2828

2929
sxbp_spiral_t sxbp_blank_spiral(void) {
30-
return (sxbp_spiral_t){0, NULL, {{NULL, 0}, 0}, false, 0, 0, 0};
30+
return (sxbp_spiral_t){0, NULL, {{NULL, 0}, 0}, false, 0, 0, 0, 0};
3131
}
3232

3333
sxbp_status_t sxbp_init_spiral(sxbp_buffer_t buffer, sxbp_spiral_t* spiral) {

sxbp/saxbospiral.h

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ typedef struct sxbp_co_ord_cache_t {
184184
* spiral object may be produced from binary data, and a fully-complete spiral
185185
* may be produced from the partially complete one via the routines in the
186186
* library.
187-
* @note The solved_count and seconds_spent fields are currently unused by the
188-
* solver/generator code, but they are read to and written from files. They will
189-
* be used by the rest of the code in future versions.
190187
*/
191188
typedef struct sxbp_spiral_t {
192189
/** @brief count of lines in the spiral */
@@ -205,16 +202,15 @@ typedef struct sxbp_spiral_t {
205202
* @private
206203
*/
207204
uint32_t collider;
208-
/**
209-
* @brief the count of lines solved so far (index of next line to solve)
210-
* @note Currently not used by solver/generator
211-
*/
205+
/** @brief the count of lines solved so far (index of next line to solve) */
212206
uint32_t solved_count;
207+
/** @brief count of seconds spent solving the spiral */
208+
uint32_t seconds_spent;
213209
/**
214-
* @brief count of seconds spent solving the spiral
215-
* @note Currently not used by solver/generator
210+
* @brief stores the number of seconds' accuracy of the `seconds_spent`
211+
* field
216212
*/
217-
uint32_t seconds_spent;
213+
uint32_t seconds_accuracy;
218214
} sxbp_spiral_t;
219215

220216
/** @brief A simple buffer type for storing arrays of bytes. */

sxbp/serialise.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ sxbp_serialise_result_t sxbp_load_spiral(
151151
spiral->size = spiral_size;
152152
spiral->solved_count = load_uint32_t(&buffer, 14);
153153
spiral->seconds_spent = load_uint32_t(&buffer, 18);
154+
spiral->seconds_accuracy = load_uint32_t(&buffer, 22);
154155
// allocate memory
155156
spiral->lines = calloc(sizeof(sxbp_line_t), spiral->size);
156157
// catch allocation error
@@ -211,6 +212,7 @@ sxbp_serialise_result_t sxbp_dump_spiral(
211212
dump_uint32_t(spiral.size, buffer, 10);
212213
dump_uint32_t(spiral.solved_count, buffer, 14);
213214
dump_uint32_t(spiral.seconds_spent, buffer, 18);
215+
dump_uint32_t(spiral.seconds_accuracy, buffer, 22);
214216
// now write the data section
215217
for(size_t i = 0; i < spiral.size; i++) {
216218
/*

sxbp/solve.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <stdbool.h>
1313
#include <stddef.h>
1414
#include <stdint.h>
15+
#include <time.h>
1516

1617
#include "saxbospiral.h"
1718
#include "plot.h"
@@ -264,6 +265,15 @@ sxbp_status_t sxbp_plot_spiral(
264265
) {
265266
// preconditional assertions
266267
assert(spiral->lines != NULL);
268+
// get current time
269+
time_t start_time = time(NULL);
270+
// get original number of seconds spent
271+
uint32_t seconds_spent_so_far = spiral->seconds_spent;
272+
/*
273+
* update accuracy of the seconds spent field
274+
* (every run time makes it one second less accurate).
275+
*/
276+
spiral->seconds_accuracy++;
267277
// set up result status
268278
sxbp_status_t result;
269279
// get index of highest line to plot
@@ -275,11 +285,21 @@ sxbp_status_t sxbp_plot_spiral(
275285
if(result != SXBP_OPERATION_OK) {
276286
return result;
277287
}
288+
// update time spent solving
289+
spiral->seconds_spent = (
290+
// amount of time spent previously + time spent during this run time
291+
seconds_spent_so_far + (uint32_t)difftime(time(NULL), start_time)
292+
);
278293
// call callback if given
279294
if(progress_callback != NULL) {
280295
progress_callback(spiral, i, max_index, progress_callback_user_data);
281296
}
282297
}
298+
// update time spent solving
299+
spiral->seconds_spent = (
300+
// amount of time spent previously + time spent during this run time
301+
seconds_spent_so_far + (uint32_t)difftime(time(NULL), start_time)
302+
);
283303
// all ok
284304
result = SXBP_OPERATION_OK;
285305
return result;

tests.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,10 @@ static bool test_sxbp_load_spiral(void) {
393393
}
394394
// build expected output struct
395395
sxbp_spiral_t expected = {
396-
.size = 16, .solved_count = 5, .seconds_spent = 3125,
396+
.size = 16,
397+
.solved_count = 5,
398+
.seconds_spent = 3125,
399+
.seconds_accuracy = 1,
397400
};
398401
expected.lines = calloc(sizeof(sxbp_line_t), 16);
399402
sxbp_direction_t directions[16] = {
@@ -419,6 +422,8 @@ static bool test_sxbp_load_spiral(void) {
419422
result = false;
420423
} else if(output.seconds_spent != expected.seconds_spent) {
421424
result = false;
425+
} else if(output.seconds_accuracy != expected.seconds_accuracy) {
426+
result = false;
422427
} else {
423428
// compare with expected struct
424429
for(uint8_t i = 0; i < 16; i++) {
@@ -593,6 +598,7 @@ static bool test_sxbp_dump_spiral(void) {
593598
.size = 16,
594599
.solved_count = 5,
595600
.seconds_spent = 3125,
601+
.seconds_accuracy = 1,
596602
};
597603
input.lines = calloc(sizeof(sxbp_line_t), 16);
598604
sxbp_direction_t directions[16] = {
@@ -629,7 +635,7 @@ static bool test_sxbp_dump_spiral(void) {
629635
0, 0, 0, 16, // size
630636
0, 0, 0, 5, // solved count
631637
0, 0, 12, 53, // seconds spent
632-
0, 0, 0, 0 // seconds accuracy - TODO: Change to something to test
638+
0, 0, 0, 1 // seconds accuracy - TODO: Change to something to test
633639
);
634640
// construct expected data section
635641
uint8_t data[64] = {

0 commit comments

Comments
 (0)