LCOV - code coverage report
Current view: top level - clients/examples/C - bincopyuuid.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 34 34 100.0 %
Date: 2021-09-14 19:48:19 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : #include "bincopydata.h"
      10             : 
      11             : typedef union {
      12             :         uint8_t u[16];
      13             :         uint32_t w[4];
      14             : } my_uuid;
      15             : 
      16             : static void
      17     2000000 : random_uuid(struct rng *rng, my_uuid *u)
      18             : {
      19     2000000 :         u->w[0] = rng_next(rng);
      20     2000000 :         u->w[1] = rng_next(rng);
      21     2000000 :         u->w[2] = rng_next(rng);
      22     2000000 :         u->w[3] = rng_next(rng);
      23             : 
      24             :         // See https://en.wikipedia.org/wiki/Universally_unique_identifier
      25             :         //
      26             :         // _0_1_2_3 _4_5 _6_7 _8_9
      27             :         // 123e4567-e89b-12d3-a456-426614174000
      28             :     // xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
      29             :         //
      30             :         // M: uuid version, located in byte 6
      31             :         // N: uuid variant, located in byte 8
      32             : 
      33             :         // Force variant 1: most common, fully big-endian encoding.
      34             :         // Variant 1 means 0b10xx.
      35     2000000 :         u->u[8] &= 0x3F; // now it's 0b00xx,
      36     2000000 :         u->u[8] |= 0x80; // now it's 0b10xx.
      37             : 
      38             :         // Force version 4: randomly generated
      39     2000000 :         u->u[6] &= 0x0F;
      40     2000000 :         u->u[6] |= 0x40;
      41     2000000 : }
      42             : 
      43             : void
      44           1 : gen_bin_uuids(FILE *f, bool byteswap, long nrecs)
      45             : {
      46           1 :         struct rng rng = my_favorite_rng();
      47           1 :         my_uuid uu = { .u = { 0 }};
      48     1000001 :         for (long i = 0; i < nrecs; i++) {
      49     1000000 :                 random_uuid(&rng, &uu);
      50     1000000 :                 if (i % 100 == 99)
      51       10000 :                         uu = (my_uuid) { .u = { 0 }};
      52     1000000 :                 fwrite(&uu, sizeof(uu), 1, f);
      53             :         }
      54             :         (void)byteswap;
      55           1 : }
      56             : 
      57             : void
      58           1 : gen_text_uuids(FILE *f, bool byteswap, long nrecs)
      59             : {
      60           1 :         struct rng rng = my_favorite_rng();
      61           1 :         my_uuid uu = { .u = { 0 }};
      62     1000001 :         for (long i = 0; i < nrecs; i++) {
      63     1000000 :                 random_uuid(&rng, &uu);
      64     1000000 :                 if (i % 100 == 99)
      65       10000 :                         uu = (my_uuid) { .u = { 0 }};
      66     1000000 :                 fprintf(
      67             :                         f,
      68             :                         "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
      69     1000000 :                         uu.u[0], uu.u[1], uu.u[2], uu.u[3],
      70     1000000 :                         uu.u[4], uu.u[5],
      71     1000000 :                         uu.u[6], uu.u[7],
      72     1000000 :                         uu.u[8], uu.u[9],
      73     1000000 :                         uu.u[10], uu.u[11], uu.u[12], uu.u[13], uu.u[14], uu.u[15]
      74             :                         );
      75     1000000 :                 fputc('\0', f);
      76             :         }
      77             :         (void)byteswap;
      78           1 : }
      79             : 

Generated by: LCOV version 1.14