From 22740815460da81f29607ed3b24f65fcfc1c6a8b Mon Sep 17 00:00:00 2001
From: Pedro Gonnet <gonnet@google.com>
Date: Wed, 4 Jan 2017 13:33:55 +0100
Subject: [PATCH] added a test for reading/writing timestamps.

---
 tests/testLogger.c | 55 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)

diff --git a/tests/testLogger.c b/tests/testLogger.c
index 5a65b36870..943e1d8b40 100644
--- a/tests/testLogger.c
+++ b/tests/testLogger.c
@@ -169,6 +169,56 @@ void test_log_gparts(struct dump *d) {
   }
 }
 
+void test_log_timestamps(struct dump *d) {
+
+  /* The timestamp to log. */
+  unsigned long long int t = 10;
+
+  /* Start with an offset at the end of the dump. */
+  size_t offset = d->count;
+
+  /* Log three consecutive timestamps. */
+  logger_log_timestamp(t, &offset, d);
+  printf("Logged timestamp %020llu at offset %#016zx.\n", t, offset);
+  t += 10;
+  logger_log_timestamp(t, &offset, d);
+  printf("Logged timestamp %020llu at offset %#016zx.\n", t, offset);
+  t += 10;
+  logger_log_timestamp(t, &offset, d);
+  printf("Logged timestamp %020llu at offset %#016zx.\n", t, offset);
+
+  /* Recover the three timestamps. */
+  size_t offset_old = offset;
+  t = 0;
+  int mask = logger_read_timestamp(&t, &offset, d->data);
+  printf("Recovered timestamp %020llu at offset %#016zx with mask %#04x.\n", t,
+         offset_old, mask);
+  if (t != 30) {
+    printf("FAIL: could not recover correct timestamp.\n");
+    abort();
+  }
+
+  offset_old = offset;
+  t = 0;
+  mask = logger_read_timestamp(&t, &offset, d->data);
+  printf("Recovered timestamp %020llu at offset %#016zx with mask %#04x.\n", t,
+         offset_old, mask);
+  if (t != 20) {
+    printf("FAIL: could not recover correct timestamp.\n");
+    abort();
+  }
+
+  offset_old = offset;
+  t = 0;
+  mask = logger_read_timestamp(&t, &offset, d->data);
+  printf("Recovered timestamp %020llu at offset %#016zx with mask %#04x.\n", t,
+         offset_old, mask);
+  if (t != 10) {
+    printf("FAIL: could not recover correct timestamp.\n");
+    abort();
+  }
+}
+
 int main(int argc, char *argv[]) {
 
   /* Some constants. */
@@ -181,9 +231,12 @@ int main(int argc, char *argv[]) {
   /* Test writing/reading parts. */
   test_log_parts(&d);
 
-  /* Test writing/reading parts. */
+  /* Test writing/reading gparts. */
   test_log_gparts(&d);
 
+  /* Test writing/reading timestamps. */
+  test_log_timestamps(&d);
+
   /* Finalize the dump. */
   dump_close(&d);
 
-- 
GitLab