Guest User

Untitled

a guest
Oct 22nd, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. /* uuid_test.c
  2. * Exercising functions from libuuid (uuid(3))
  3. */
  4.  
  5. #include <assert.h>
  6. #include <stdio.h>
  7. #include <sys/time.h>
  8. #include <time.h>
  9. #include <uuid/uuid.h>
  10.  
  11. #define BUFFSIZE 100
  12.  
  13. int main() {
  14. uuid_t my_uuid;
  15. uuid_t my_uuid_copy;
  16. char my_uuid_string[37];
  17. time_t uuid_ctime;
  18. char ctime_string[BUFFSIZE] = {0};
  19. struct timeval uuid_ctimeval;
  20.  
  21. /* Generate uuid */
  22. uuid_generate_time(my_uuid);
  23. assert(!uuid_is_null(my_uuid));
  24.  
  25. /* Unparse to string */
  26. uuid_unparse(my_uuid, my_uuid_string);
  27. printf("Got UUID: %s\n", my_uuid_string);
  28. uuid_unparse_upper(my_uuid, my_uuid_string);
  29. printf("uppercase %s\n", my_uuid_string);
  30.  
  31. /* Check creation time */
  32. uuid_ctime = uuid_time(my_uuid, &uuid_ctimeval);
  33. assert(strftime(ctime_string, BUFFSIZE,
  34. "%I:%M:%S %p on %A %B %e, %Y",
  35. gmtime(&uuid_ctime)));
  36. printf("\nUUID created at %s\n", ctime_string);
  37. printf("More precisely, calendar time: %ld.%06ld\n",
  38. uuid_ctimeval.tv_sec, uuid_ctimeval.tv_usec);
  39.  
  40. /* Parse and compare */
  41. uuid_parse(my_uuid_string, my_uuid_copy);
  42. assert(!uuid_compare(my_uuid, my_uuid_copy));
  43.  
  44. /* Clear */
  45. uuid_clear(my_uuid_copy);
  46. assert(uuid_is_null(my_uuid_copy));
  47.  
  48. /* Copy */
  49. uuid_copy(my_uuid_copy, my_uuid);
  50. assert(!uuid_is_null(my_uuid_copy));
  51. assert(!uuid_compare(my_uuid, my_uuid_copy));
  52.  
  53. /* Clear */
  54. uuid_clear(my_uuid);
  55. uuid_clear(my_uuid_copy);
  56. assert(uuid_is_null(my_uuid) && uuid_is_null(my_uuid_copy));
  57.  
  58. return 0;
  59. }
Add Comment
Please, Sign In to add comment