LCOV - code coverage report
Current view: top level - clients/examples/C - bincopytemporaldata.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 34 34 100.0 %
Date: 2021-09-14 19:48:19 Functions: 11 11 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             : static copy_binary_timestamp
      12    10000000 : random_timestamp(struct rng *rng)
      13             : {
      14             :         // the % trick gives a little skew but we don't care
      15    10000000 :         copy_binary_timestamp ts = {
      16             :                 .time = {
      17    10000000 :                         .ms = rng_next(rng) % 1000000,
      18    10000000 :                         .seconds = rng_next(rng) % 60, // 61 ??
      19    10000000 :                         .minutes = rng_next(rng) % 60,
      20    10000000 :                         .hours = rng_next(rng) % 24,
      21             :                         .padding = 0,
      22             :                 },
      23             :                 .date = {
      24             :                         .day = 0, // determine later
      25    10000000 :                         .month = 1 + rng_next(rng) % 12,
      26    10000000 :                         .year = 2030 - (int16_t)rng_next(rng),
      27             :                 },
      28             :         };
      29             : 
      30    10000000 :         const uint32_t durations[] = { 0,
      31             :                 31, 28, 31, 30,
      32             :                 31, 30, 31, 31,
      33             :                 30, 31, 30, 31,
      34             :         };
      35    10000000 :         uint32_t year = ts.date.year;
      36    10000000 :         uint32_t days = durations[ts.date.month];
      37    10000000 :         bool leap_year = (
      38      829090 :                 days == 28 &&
      39    10000000 :                 year % 4 == 0 &&
      40      208870 :                 (year % 100 != 0 || year % 400 == 0)
      41             :         );
      42    10000000 :         if (leap_year)
      43             :                 days = 29;
      44    10000000 :         ts.date.day = 1 + rng_next(rng) % days;
      45             : 
      46    10000000 :         return ts;
      47             : }
      48             : 
      49             : void
      50           1 : gen_timestamps(FILE *f, bool byteswap, long nrecs)
      51             : {
      52           1 :         struct rng rng = my_favorite_rng();
      53             : 
      54     1000001 :         for (long i = 0; i < nrecs; i++) {
      55     1000000 :                 copy_binary_timestamp ts = random_timestamp(&rng);
      56     1000000 :                 if (byteswap) {
      57             :                         copy_binary_convert_timestamp(&ts);
      58             :                 }
      59     1000000 :                 fwrite(&ts, sizeof(ts), 1, f);
      60             :         }
      61           1 : }
      62             : 
      63             : #define GEN_TIMESTAMP_FIELD(name, fld) \
      64             :         void name \
      65             :                 (FILE *f, bool byteswap, long nrecs) \
      66             :         { \
      67             :                 struct rng rng = my_favorite_rng(); \
      68             :         \
      69             :                 for (long i = 0; i < nrecs; i++) { \
      70             :                         copy_binary_timestamp ts = random_timestamp(&rng); \
      71             :                         if (byteswap) { \
      72             :                                 copy_binary_convert_timestamp(&ts); \
      73             :                         } \
      74             :                         fwrite(&ts.fld, sizeof(ts.fld), 1, f); \
      75             :                 } \
      76             :         }
      77             : 
      78     1000001 : GEN_TIMESTAMP_FIELD(gen_timestamp_times, time)
      79     1000001 : GEN_TIMESTAMP_FIELD(gen_timestamp_dates, date)
      80             : 
      81     1000001 : GEN_TIMESTAMP_FIELD(gen_timestamp_ms, time.ms)
      82     1000001 : GEN_TIMESTAMP_FIELD(gen_timestamp_seconds, time.seconds)
      83     1000001 : GEN_TIMESTAMP_FIELD(gen_timestamp_minutes, time.minutes)
      84     1000001 : GEN_TIMESTAMP_FIELD(gen_timestamp_hours, time.hours)
      85     1000001 : GEN_TIMESTAMP_FIELD(gen_timestamp_days, date.day)
      86     1000001 : GEN_TIMESTAMP_FIELD(gen_timestamp_months, date.month)
      87     1000001 : GEN_TIMESTAMP_FIELD(gen_timestamp_years, date.year)

Generated by: LCOV version 1.14