summaryrefslogtreecommitdiffstats
path: root/tests/tests.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tests.c')
-rw-r--r--tests/tests.c77
1 files changed, 76 insertions, 1 deletions
diff --git a/tests/tests.c b/tests/tests.c
index e91e290..cbf7ff8 100644
--- a/tests/tests.c
+++ b/tests/tests.c
@@ -229,11 +229,83 @@ dmnsn_display_flush(dmnsn_display *display)
/* Print a progress bar of the progress of `progress' */
void
-progressbar(const char *str, const dmnsn_progress *progress)
+dmnsn_progressbar(const char *str, const dmnsn_progress *progress)
+{
+ dmnsn_progress *barprogress = dmnsn_progressbar_async(str, progress);
+ if (dmnsn_finish_progress(barprogress) != 0) {
+ dmnsn_error(DMNSN_SEVERITY_HIGH,
+ "Progress bar background thread failed.");
+ }
+}
+
+typedef struct {
+ dmnsn_progress *barprogress;
+ const char *str;
+ const dmnsn_progress *progress;
+} dmnsn_progressbar_payload;
+
+/* Progress bar thread callback */
+static void *dmnsn_progressbar_thread(void *ptr);
+
+/* Print a progress bar in the background */
+dmnsn_progress *
+dmnsn_progressbar_async(const char *str, const dmnsn_progress *progress)
+{
+ dmnsn_progress *barprogress = dmnsn_new_progress();
+ dmnsn_progressbar_payload *payload;
+
+ if (barprogress) {
+ payload = malloc(sizeof(dmnsn_progressbar_payload));
+ if (!payload) {
+ dmnsn_error(DMNSN_SEVERITY_HIGH,
+ "Couldn't allocate progress bar payload.");
+ }
+
+ payload->barprogress = barprogress;
+ payload->str = str;
+ payload->progress = progress;
+
+ /* Create the worker thread */
+ if (pthread_create(&barprogress->thread, NULL, &dmnsn_progressbar_thread,
+ payload)
+ != 0) {
+ dmnsn_error(DMNSN_SEVERITY_HIGH,
+ "Couldn't start progress bar background thread.");
+ }
+ }
+
+ return barprogress;
+}
+
+/* Actual progress bar implementation */
+static int dmnsn_progressbar_impl(dmnsn_progress *barprogress, const char *str,
+ const dmnsn_progress *progress);
+
+/* Progress bar thread callback */
+static void *
+dmnsn_progressbar_thread(void *ptr)
+{
+ dmnsn_progressbar_payload *payload = ptr;
+ int *retval = malloc(sizeof(int));
+ if (retval) {
+ *retval = dmnsn_progressbar_impl(payload->barprogress,
+ payload->str, payload->progress);
+ }
+ dmnsn_done_progress(payload->barprogress);
+ free(payload);
+ return retval;
+}
+
+/* Actual progress bar implementation */
+static int
+dmnsn_progressbar_impl(dmnsn_progress *barprogress,
+ const char *str, const dmnsn_progress *progress)
{
const unsigned int increments = 32;
unsigned int i;
+ dmnsn_new_progress_element(barprogress, increments);
+
printf("%s|", str);
fflush(stdout);
for (i = 0; i < increments; ++i) {
@@ -241,7 +313,10 @@ progressbar(const char *str, const dmnsn_progress *progress)
printf("=");
fflush(stdout);
+ dmnsn_increment_progress(barprogress);
}
printf("|\n");
fflush(stdout);
+
+ return 0;
}