LCOV - code coverage report
Current view: top level - clients/examples/C - bincopyuuid.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 38 38 100.0 %
Date: 2024-10-04 20:04:04 Functions: 3 3 100.0 %

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

Generated by: LCOV version 1.14