test json-time conversion, add support for forever/never time

This commit is contained in:
Christian Grothoff 2015-05-17 17:01:25 +02:00
parent 3817f83dd5
commit b1401f93da
2 changed files with 52 additions and 0 deletions

View File

@ -83,6 +83,9 @@ TALER_json_from_abs (struct GNUNET_TIME_Absolute stamp)
json_t *j; json_t *j;
char *mystr; char *mystr;
int ret; int ret;
if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
return json_string ("never");
ret = GNUNET_asprintf (&mystr, ret = GNUNET_asprintf (&mystr,
"%llu", "%llu",
(long long) (stamp.abs_value_us / (1000 * 1000))); (long long) (stamp.abs_value_us / (1000 * 1000)));
@ -259,6 +262,12 @@ TALER_json_to_abs (json_t *json,
GNUNET_assert (NULL != abs); GNUNET_assert (NULL != abs);
EXITIF (NULL == (str = json_string_value (json))); EXITIF (NULL == (str = json_string_value (json)));
if (0 == strcasecmp (str,
"never"))
{
*abs = GNUNET_TIME_UNIT_FOREVER_ABS;
return GNUNET_OK;
}
EXITIF (1 > sscanf (str, "%llu", &abs_value_s)); EXITIF (1 > sscanf (str, "%llu", &abs_value_s));
abs->abs_value_us = abs_value_s * 1000 * 1000; abs->abs_value_us = abs_value_s * 1000 * 1000;
return GNUNET_OK; return GNUNET_OK;

View File

@ -24,6 +24,11 @@
#include "taler_json_lib.h" #include "taler_json_lib.h"
/**
* Test amount conversion from/to JSON.
*
* @return 0 on success
*/
static int static int
test_amount () test_amount ()
{ {
@ -47,6 +52,42 @@ test_amount ()
} }
/**
* Test time conversion from/to JSON.
*
* @return 0 on success
*/
static int
test_time ()
{
json_t *j;
struct GNUNET_TIME_Absolute a1;
struct GNUNET_TIME_Absolute a2;
a1 = GNUNET_TIME_absolute_get ();
a1.abs_value_us -= a1.abs_value_us % 1000000; /* round! */
j = TALER_json_from_abs (a1);
GNUNET_assert (NULL != j);
GNUNET_assert (GNUNET_OK ==
TALER_json_to_abs (j,
&a2));
GNUNET_assert (a1.abs_value_us ==
a2.abs_value_us);
json_decref (j);
a1 = GNUNET_TIME_UNIT_FOREVER_ABS;
j = TALER_json_from_abs (a1);
GNUNET_assert (NULL != j);
GNUNET_assert (GNUNET_OK ==
TALER_json_to_abs (j,
&a2));
GNUNET_assert (a1.abs_value_us ==
a2.abs_value_us);
json_decref (j);
return 0;
}
int int
main(int argc, main(int argc,
const char *const argv[]) const char *const argv[])
@ -56,6 +97,8 @@ main(int argc,
NULL); NULL);
if (0 != test_amount ()) if (0 != test_amount ())
return 1; return 1;
if (0 != test_time ())
return 1;
/* FIXME: implement test... */ /* FIXME: implement test... */
return 0; return 0;
} }