~ubuntu-branches/ubuntu/saucy/emscripten/saucy-proposed

« back to all changes in this revision

Viewing changes to tests/time/src.c

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <time.h>
 
4
#include <string.h>
 
5
 
 
6
int main() {
 
7
  time_t xmas2002 = 1040786563ll;
 
8
  struct tm* tm_ptr;
 
9
 
 
10
  // Make sure stime() always fails.
 
11
  printf("stime: %d\n", stime(&xmas2002));
 
12
 
 
13
  // Verify that tzname sets *something*.
 
14
  tzset();
 
15
  printf("tzname[0] set: %d\n", strlen(tzname[0]) >= 3);
 
16
  printf("tzname[1] set: %d\n", strlen(tzname[1]) >= 3);
 
17
  
 
18
  // Verify gmtime() creates correct struct.
 
19
  tm_ptr = gmtime(&xmas2002);
 
20
  printf("sec: %d\n", tm_ptr->tm_sec);
 
21
  printf("min: %d\n", tm_ptr->tm_min);
 
22
  printf("hour: %d\n", tm_ptr->tm_hour);
 
23
  printf("day: %d\n", tm_ptr->tm_mday);
 
24
  printf("mon: %d\n", tm_ptr->tm_mon);
 
25
  printf("year: %d\n", tm_ptr->tm_year);
 
26
  printf("wday: %d\n", tm_ptr->tm_wday);
 
27
  printf("yday: %d\n", tm_ptr->tm_yday);
 
28
  printf("dst: %d\n", tm_ptr->tm_isdst);
 
29
  printf("off: %d\n", tm_ptr->tm_gmtoff);
 
30
  printf("zone: %s\n", tm_ptr->tm_zone);
 
31
  
 
32
  // Verify timegm() reverses gmtime.
 
33
  printf("timegm <-> gmtime: %d\n", timegm(tm_ptr) == xmas2002);
 
34
  
 
35
  // Verify gmtime_r() doesn't clobber static data.
 
36
  time_t t1 = 0;
 
37
  struct tm tm1;
 
38
  gmtime_r(&t1, &tm1);
 
39
  printf("old year: %d\n", tm_ptr->tm_year);
 
40
  printf("new year: %d\n", tm1.tm_year);
 
41
  gmtime(&xmas2002);
 
42
  printf("old year again: %d\n", tm_ptr->tm_year);
 
43
  
 
44
  // Verify localtime() picks up timezone data.
 
45
  time_t t2 = xmas2002 - 60 * 60 * 24 * 30 * 6;
 
46
  tm_ptr = localtime(&t2);
 
47
  time_t dst_diff = (tm_ptr->tm_isdst == 1) ? tm_ptr->tm_isdst * 60 * 60 : 0;
 
48
  printf("localtime timezone: %d\n", (_timezone + tm_ptr->tm_gmtoff == dst_diff)); // glibc needs
 
49
  printf("localtime daylight: %d\n", _daylight == tm_ptr->tm_isdst);               // no prefix "_"s
 
50
  printf("localtime tzname: %d\n", (!strcmp(tzname[0], tm_ptr->tm_zone) ||
 
51
                                    !strcmp(tzname[1], tm_ptr->tm_zone)));
 
52
 
 
53
  // Verify localtime() and mktime() reverse each other.
 
54
  printf("localtime <-> mktime: %d\n", mktime(localtime(&xmas2002)) == xmas2002);
 
55
  
 
56
  // Verify localtime_r() doesn't clobber static data.
 
57
  time_t t3 = 0;
 
58
  struct tm tm2;
 
59
  localtime_r(&t3, &tm2);
 
60
  printf("localtime_r(1): %d\n", tm2.tm_year != tm_ptr->tm_year);
 
61
  localtime(&xmas2002);
 
62
  printf("localtime_r(2): %d\n", tm2.tm_year != tm_ptr->tm_year);
 
63
 
 
64
  // Verify time() returns reasonable value (between 2011 and 2030).
 
65
  time_t t4 = 0;
 
66
  time(&t4);
 
67
  printf("time: %d\n", t4 > 1309635200ll && t4 < 1893362400ll);
 
68
 
 
69
  // Verify difftime() calculates accurate time difference.
 
70
  time_t t5 = 1309635200ll;
 
71
  printf("difftime+: %lf\n", difftime(t5, xmas2002));
 
72
  printf("difftime-: %lf\n", difftime(xmas2002, t5));
 
73
 
 
74
  // Verify dysize() knows its leap years.
 
75
  printf("1854 days: %d\n", dysize(1854));
 
76
  printf("2000 days: %d\n", dysize(2000));
 
77
  printf("2001 days: %d\n", dysize(2001));
 
78
  printf("2004 days: %d\n", dysize(2004));
 
79
 
 
80
  // Verify asctime() formatting().
 
81
  printf("asctime: %s", asctime(gmtime(&xmas2002)));
 
82
 
 
83
  // Verify asctime_r() doesn't clobber static data.
 
84
  time_t t6 = 1309635200ll;
 
85
  tm_ptr = gmtime(&xmas2002);
 
86
  char* formatted = asctime(tm_ptr);
 
87
  char buffer[32];
 
88
  asctime_r(gmtime(&t6), buffer);
 
89
  printf("old asctime: %s", formatted);
 
90
  printf("new asctime_r: %s", buffer);
 
91
  asctime_r(tm_ptr, buffer);
 
92
  printf("old asctime again: %s", formatted);
 
93
 
 
94
  // Verify that clock() advances.
 
95
  time_t start_t = time(NULL);
 
96
  clock_t start = clock();
 
97
  printf("clock(start): %d\n", start >= 0);
 
98
  while (clock() - start < 2 * CLOCKS_PER_SEC); // Poor man's sleep().
 
99
  clock_t diff = time(NULL) - start_t;
 
100
  printf("clock(end): %d\n", diff >= 2 && diff < 30);
 
101
 
 
102
  // Verify that ctime_r(x, buf) is equivalent to asctime_r(localtime(x), buf).
 
103
  time_t t7 = time(0);
 
104
  char buffer2[30];
 
105
  char buffer3[30];
 
106
  printf("ctime: %d\n", strcmp(ctime_r(&t7, buffer2),
 
107
                               asctime_r(localtime(&t7), buffer3)));
 
108
 
 
109
  return 0;
 
110
}