File: test_month_adjust.c

package info (click to toggle)
cdo 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 49,836 kB
  • sloc: cpp: 185,271; ansic: 95,766; sh: 7,192; f90: 6,147; makefile: 1,977; ruby: 1,078; csh: 1,028; python: 995; fortran: 319; pascal: 219; perl: 9
file content (69 lines) | stat: -rw-r--r-- 2,170 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* DO NOT REMOVE the config.h include file under any circumstances,
 * it's very much needed on some platforms */
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
/* DO NOT REMOVE the above config.h include file under any
 * circumstances as long as it's the autoconf configuration header
 * used to build this package. When it's missing on some platforms,
 * some poor person has to do long, tedious debugging sessions, where
 * struct offsets almost imperceptibly change from one file to the
 * next to find out what happened */

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#include "normalize_month.h"

int
main(void)
{
  static const int vals[] = { -12, -17, 17, 11, -769652651 };
  enum
  {
    numVals = sizeof(vals) / sizeof(vals[0])
  };
  for (size_t i = 0; i < numVals; ++i)
  {
    enum
    {
      startYear = 1900
    };
    int month = vals[i];
    struct YearMonth ym = normalize_month(startYear, month);
    if ((long) ym.year * 12 + ym.month != (long) startYear * 12 + month)
    {
      fprintf(stderr,
              "problem: month=%d, ym.month=%d, ym.year=%d\n"
              "(long)ym.year * 12 + ym.month = %ld\n"
              "(long)startYear * 12 + month = %ld\n",
              month, ym.month, ym.year, (long) ym.year * 12 + ym.month, (long) startYear * 12 + month);
      abort();
    }
  }
  {
    struct timeval tv;
    if (gettimeofday(&tv, NULL))
    {
      perror("failed to get time for random number generator initialization");
      exit(EXIT_FAILURE);
    }
    srandom((unsigned) (tv.tv_sec ^ tv.tv_usec));
  }
  for (size_t j = 0; j < 1000000; ++j)
  {
    int year = (int) (random() - RAND_MAX / 2), month = (int) (random() - RAND_MAX / 2);
    struct YearMonth ym = normalize_month(year, month);
    if ((long) ym.year * 12 + ym.month != (long) year * 12 + month)
    {
      fprintf(stderr,
              "problem: month=%d, ym.month=%d, ym.year=%d\n"
              "(long)ym.year * 12 + ym.month = %ld\n"
              "(long)year * 12 + month = %ld\n",
              month, ym.month, ym.year, (long) ym.year * 12 + ym.month, (long) year * 12 + month);
      abort();
    }
  }
  return EXIT_SUCCESS;
}