Mercurial > hg > octave-kai > gnulib-hg
annotate tests/test-strftime.c @ 14444:4d990235f694
tests: add strftime-tests module
* tests/test-strftime.c: New file.
* modules/strftime-tests: New module.
author | Jim Meyering <meyering@redhat.com> |
---|---|
date | Mon, 21 Mar 2011 15:09:10 +0100 |
parents | |
children | c155aeb8c42e |
rev | line source |
---|---|
14444
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
1 /* Test that posixtime works as required. |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
2 Copyright (C) 2011 Free Software Foundation, Inc. |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
3 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
4 This program is free software: you can redistribute it and/or modify |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
5 it under the terms of the GNU General Public License as published by |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
6 the Free Software Foundation; either version 3 of the License, or |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
7 (at your option) any later version. |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
8 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
9 This program is distributed in the hope that it will be useful, |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
12 GNU General Public License for more details. |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
13 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
14 You should have received a copy of the GNU General Public License |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
16 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
17 /* Written by Jim Meyering. */ |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
18 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
19 #include <config.h> |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
20 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
21 #include "strftime.h" |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
22 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
23 #include <stdio.h> |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
24 #include <time.h> |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
25 #include <string.h> |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
26 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
27 #include "macros.h" |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
28 #define STREQ(a, b) (strcmp (a, b) == 0) |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
29 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
30 struct posixtm_test |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
31 { |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
32 time_t in; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
33 int in_ns; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
34 char const *fmt; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
35 char const *exp; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
36 }; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
37 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
38 static struct posixtm_test const T[] = |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
39 { |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
40 { 1300000000, 0, "%F", "2011-03-13" }, |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
41 { 0, 0, NULL, NULL } |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
42 }; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
43 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
44 int |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
45 main (void) |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
46 { |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
47 int fail = 0; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
48 unsigned int i; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
49 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
50 for (i = 0; T[i].fmt; i++) |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
51 { |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
52 char buf[1000]; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
53 time_t t = T[i].in; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
54 struct tm *tm = gmtime (&t); |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
55 size_t n; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
56 int utc = 1; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
57 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
58 ASSERT (tm); |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
59 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
60 n = nstrftime (buf, sizeof buf, T[i].fmt, tm, utc, T[i].in_ns); |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
61 if (n == 0) |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
62 { |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
63 fail = 1; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
64 printf ("nstrftime failed with format %s\n", T[i].fmt); |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
65 } |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
66 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
67 if (! STREQ (buf, T[i].exp)) |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
68 { |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
69 fail = 1; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
70 printf ("%s: result mismatch: got %s, expected %s\n", |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
71 T[i].fmt, buf, T[i].exp); |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
72 } |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
73 } |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
74 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
75 return fail; |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
76 } |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
77 |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
78 /* |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
79 Local Variables: |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
80 indent-tabs-mode: nil |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
81 End: |
4d990235f694
tests: add strftime-tests module
Jim Meyering <meyering@redhat.com>
parents:
diff
changeset
|
82 */ |