summaryrefslogtreecommitdiff
path: root/test/testtime.c
diff options
context:
space:
mode:
authorRyan Bloom <rbb@apache.org>2002-09-15 22:51:28 +0000
committerRyan Bloom <rbb@apache.org>2002-09-15 22:51:28 +0000
commit3b11ef38cb97dfca62e40f33f159c81902425eda (patch)
tree53d7f9ba265a2c058f7a7e5dddcd48c7debd7855 /test/testtime.c
parent1f3739440f8f75a07173ac67b336bb75c2200fee (diff)
downloadapr-3b11ef38cb97dfca62e40f33f159c81902425eda.tar.gz
Begin to migrate the APR test suite to the CuTest framework. This
basically moves testtime and teststr to a single binary, testall. The testall binary will run all of the tests that it knows about, and print the results. A document will be added later today that describes how to write tests, and how the test suite works. This is just an initial port for these test programs. There have been no new tests added. git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@63870 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'test/testtime.c')
-rw-r--r--test/testtime.c332
1 files changed, 210 insertions, 122 deletions
diff --git a/test/testtime.c b/test/testtime.c
index a1ee2ce7b..2266890cf 100644
--- a/test/testtime.c
+++ b/test/testtime.c
@@ -57,12 +57,21 @@
#include "apr_general.h"
#include "apr_lib.h"
#include <errno.h>
+#include <time.h>
#include <stdio.h>
#include "test_apr.h"
#define STR_SIZE 45
-static const char* print_time (apr_pool_t *pool, const apr_time_exp_t *xt)
+/* The time value is used throughout the tests, so just make this a global.
+ * Also, we need a single value that we can test for the positive tests, so
+ * I chose the number below, it corresponds to:
+ * 2002-08-14 12:05:36.186711 -25200 [257 Sat].
+ * Which happens to be when I wrote the new tests.
+ */
+apr_time_t now = 1032030336186711;
+
+static char* print_time (apr_pool_t *pool, const apr_time_exp_t *xt)
{
return apr_psprintf (pool,
"%04d-%02d-%02d %02d:%02d:%02d.%06d %+05d [%d %s]%s",
@@ -79,145 +88,224 @@ static const char* print_time (apr_pool_t *pool, const apr_time_exp_t *xt)
(xt->tm_isdst ? " DST" : ""));
}
-int main(void)
+
+static void test_now(CuTest *tc)
{
- apr_time_t now;
- apr_time_exp_t xt, xt2;
- apr_time_t imp;
- apr_pool_t *p;
- char *str, *str2;
- apr_size_t sz;
- apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
- apr_int64_t hr_off_64;
+ apr_time_t timediff;
+ apr_time_t current;
+ time_t os_now;
- apr_initialize();
+ current = apr_time_now();
+ time(&os_now);
- printf("APR Time Functions\n==================\n\n");
+ timediff = os_now - (current / APR_USEC_PER_SEC);
+ /* Even though these are called so close together, there is the chance
+ * that the time will be slightly off, so accept anything between -1 and
+ * 1 second.
+ */
+ CuAssert(tc, "apr_time and OS time do not agree",
+ (timediff > -1) && (timediff < 1));
+}
- STD_TEST_NEQ("Creating a pool to use", apr_pool_create(&p, NULL))
+static void test_gmtstr(CuTest *tc)
+{
+ apr_status_t rv;
+ apr_time_exp_t xt;
+ time_t os_now;
- printf("%-60s", " apr_time_now()");
- now = apr_time_now();
- printf("OK\n");
+ rv = apr_time_exp_gmt(&xt, now);
+ os_now = now / APR_USEC_PER_SEC;
+ if (rv == APR_ENOTIMPL) {
+ CuNotImpl(tc, "apr_time_exp_gmt");
+ }
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ CuAssertStrEquals(tc, "2002-08-14 19:05:36.186711 +0000 [257 Sat]",
+ print_time(p, &xt));
+}
- STD_TEST_NEQ(" apr_time_exp_gmt", apr_time_exp_gmt(&xt, now))
- printf(" (%s)\n", print_time(p, &xt));
+static void test_localstr(CuTest *tc)
+{
+ apr_status_t rv;
+ apr_time_exp_t xt;
+ time_t os_now;
- STD_TEST_NEQ(" apr_time_exp_lt", apr_time_exp_lt(&xt2, now))
- printf(" (%s)\n", print_time(p, &xt2));
+ rv = apr_time_exp_lt(&xt, now);
+ os_now = now / APR_USEC_PER_SEC;
+ if (rv == APR_ENOTIMPL) {
+ CuNotImpl(tc, "apr_time_exp_lt");
+ }
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ CuAssertStrEquals(tc, "2002-08-14 12:05:36.186711 -25200 [257 Sat] DST",
+ print_time(p, &xt));
+}
- STD_TEST_NEQ(" apr_time_exp_get (GMT)", apr_time_exp_get(&imp, &xt))
+static void test_exp_get_gmt(CuTest *tc)
+{
+ apr_status_t rv;
+ apr_time_exp_t xt;
+ apr_time_t imp;
+ apr_int64_t hr_off_64;
- printf("%-60s", " checking GMT explode == implode");
+ rv = apr_time_exp_gmt(&xt, now);
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ rv = apr_time_exp_get(&imp, &xt);
+ if (rv == APR_ENOTIMPL) {
+ CuNotImpl(tc, "apr_time_exp_get");
+ }
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ hr_off_64 = (apr_int64_t) xt.tm_gmtoff * APR_USEC_PER_SEC;
+ CuAssertTrue(tc, now + hr_off_64 == imp);
+}
+
+static void test_exp_get_lt(CuTest *tc)
+{
+ apr_status_t rv;
+ apr_time_exp_t xt;
+ apr_time_t imp;
+ apr_int64_t hr_off_64;
+
+ rv = apr_time_exp_lt(&xt, now);
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ rv = apr_time_exp_get(&imp, &xt);
+ if (rv == APR_ENOTIMPL) {
+ CuNotImpl(tc, "apr_time_exp_get");
+ }
+ CuAssertTrue(tc, rv == APR_SUCCESS);
hr_off_64 = (apr_int64_t) xt.tm_gmtoff * APR_USEC_PER_SEC;
- if (imp != now + hr_off_64) {
- printf("mismatch\n"
- "\t\tapr_now() %" APR_INT64_T_FMT "\n"
- "\t\tapr_implode() returned %" APR_INT64_T_FMT "\n"
- "\t\terror delta was %" APR_TIME_T_FMT "\n"
- "\t\tshould have been %" APR_INT64_T_FMT "\n",
- now, imp, imp-now, hr_off_64);
- exit(-1);
+ CuAssertTrue(tc, now + hr_off_64 == imp);
+}
+
+static void test_imp_gmt(CuTest *tc)
+{
+ apr_status_t rv;
+ apr_time_exp_t xt;
+ apr_time_t imp;
+
+ rv = apr_time_exp_gmt(&xt, now);
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ rv = apr_implode_gmt(&imp, &xt);
+ if (rv == APR_ENOTIMPL) {
+ CuNotImpl(tc, "apr_implode_gmt");
+ }
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ CuAssertTrue(tc, now == imp);
+}
+
+static void test_rfcstr(CuTest *tc)
+{
+ apr_status_t rv;
+ char str[STR_SIZE];
+
+ rv = apr_rfc822_date(str, now);
+ if (rv == APR_ENOTIMPL) {
+ CuNotImpl(tc, "apr_rfc822_date");
}
- printf("OK\n");
-
- STD_TEST_NEQ(" apr_time_exp_get (localtime)",
- apr_time_exp_get(&imp, &xt2))
-
- printf("%-60s", " checking localtime explode == implode");
- hr_off_64 = (apr_int64_t) xt2.tm_gmtoff * APR_USEC_PER_SEC;
- if (imp != now + hr_off_64) {
- printf("mismatch\n"
- "\t\tapr_now() %" APR_INT64_T_FMT "\n"
- "\t\tapr_implode() returned %" APR_INT64_T_FMT "\n"
- "\t\terror delta was %" APR_TIME_T_FMT "\n"
- "\t\tshould have been %" APR_INT64_T_FMT "\n",
- now, imp, imp-now, hr_off_64);
- exit(-1);
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ CuAssertStrEquals(tc, "Sat, 14 Sep 2002 19:05:36 GMT", str);
+}
+
+static void test_ctime(CuTest *tc)
+{
+ apr_status_t rv;
+ char str[STR_SIZE];
+
+ rv = apr_ctime(str, now);
+ if (rv == APR_ENOTIMPL) {
+ CuNotImpl(tc, "apr_ctime");
}
- printf("OK\n");
-
- STD_TEST_NEQ(" apr_implode_gmt (GMT)",
- apr_implode_gmt(&imp, &xt))
-
- printf("%-60s", " checking GMT explode == GMT implode");
- if (imp != now) {
- printf("mismatch\n"
- "\t\tapr_now() %" APR_INT64_T_FMT "\n"
- "\t\tapr_implode() returned %" APR_INT64_T_FMT "\n"
- "\t\terror delta was %" APR_TIME_T_FMT "\n"
- "\t\tshould have been 0\n",
- now, imp, imp-now);
- exit(-1);
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ CuAssertStrEquals(tc, "Sat Sep 14 12:05:36 2002", str);
+}
+
+static void test_strftime(CuTest *tc)
+{
+ apr_status_t rv;
+ apr_time_exp_t xt;
+ char *str = NULL;
+ apr_size_t sz;
+
+ rv = apr_time_exp_gmt(&xt, now);
+ str = apr_palloc(p, STR_SIZE + 1);
+ rv = apr_strftime(str, &sz, STR_SIZE, "%R %A %d %B %Y", &xt);
+ if (rv == APR_ENOTIMPL) {
+ CuNotImpl(tc, "apr_strftime");
}
- printf("OK\n");
-
- STD_TEST_NEQ(" apr_implode_gmt (localtime)",
- apr_implode_gmt(&imp, &xt2))
-
- printf("%-60s", " checking localtime explode == GMT implode");
- if (imp != now) {
- printf("mismatch\n"
- "\t\tapr_now() %" APR_INT64_T_FMT "\n"
- "\t\tapr_implode() returned %" APR_INT64_T_FMT "\n"
- "\t\terror delta was %" APR_TIME_T_FMT "\n"
- "\t\tshould have been 0\n",
- now, imp, imp-now);
- exit(-1);
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ CuAssertStrEquals(tc, "19:05 Saturday 14 September 2002", str);
+}
+
+static void test_strftimesmall(CuTest *tc)
+{
+ apr_status_t rv;
+ apr_time_exp_t xt;
+ char str[STR_SIZE];
+ apr_size_t sz;
+
+ rv = apr_time_exp_gmt(&xt, now);
+ rv = apr_strftime(str, &sz, STR_SIZE, "%T", &xt);
+ if (rv == APR_ENOTIMPL) {
+ CuNotImpl(tc, "apr_strftime");
}
- printf("OK\n");
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ CuAssertStrEquals(tc, "19:05:36", str);
+}
- str = apr_pcalloc(p, sizeof(char) * STR_SIZE);
- str2 = apr_pcalloc(p, sizeof(char) * STR_SIZE);
- imp = 0;
+static void test_exp_tz(CuTest *tc)
+{
+ apr_status_t rv;
+ apr_time_exp_t xt;
+ char str[STR_SIZE];
+ apr_size_t sz;
+ apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
- if (!str || !str2) {
- printf("Failure!\n");
- fprintf(stderr,"Failed to allocate memory!\n");
- exit(-1);
+ rv = apr_time_exp_tz(&xt, now, hr_off);
+ if (rv == APR_ENOTIMPL) {
+ CuNotImpl(tc, "apr_time_exp_tz");
}
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ CuAssertStrEquals(tc, "19:05:36", str);
+}
+
+static void test_strftimeoffset(CuTest *tc)
+{
+ apr_status_t rv;
+ apr_time_exp_t xt;
+ char str[STR_SIZE];
+ apr_size_t sz;
+ apr_int32_t hr_off = -5 * 3600; /* 5 hours in seconds */
- STD_TEST_NEQ(" apr_rfc822_date", apr_rfc822_date(str, now))
- printf(" ( %s )\n", str);
-
- STD_TEST_NEQ(" apr_ctime (local time)", apr_ctime(str, now))
- printf(" ( %s )\n", str);
-
- STD_TEST_NEQ(" apr_strftime (GMT) (24H day date month year)",
- apr_strftime(str, &sz, STR_SIZE, "%R %A %d %B %Y", &xt))
- printf(" ( %s )\n", str);
-
- STD_TEST_NEQ(" apr_strftime (GMT) (HH:MM:SS)",
- apr_strftime(str, &sz, STR_SIZE, "%T", &xt))
- printf (" ( %s )\n", str);
-
- STD_TEST_NEQ(" apr_time_exp_tz (GMT -5 hours)",
- apr_time_exp_tz(&xt2, now, hr_off))
-
- STD_TEST_NEQ(" apr_strftime (offset) (HH:MM:SS)",
- apr_strftime(str2, &sz, STR_SIZE, "%T", &xt2))
- printf(" ( %s )\n", str2);
-
- TEST_EQ(" Comparing the GMT and offset time strings",
- strcmp(str, str2), 0, "OK", "Failed")
- printf(" ( %s != %s )\n", str, str2);
-
- STD_TEST_NEQ(" apr_time_exp_get (offset)",
- apr_time_exp_get(&imp, &xt2))
-
- hr_off_64 = (apr_int64_t) hr_off * APR_USEC_PER_SEC; /* microseconds */
- printf("%-60s"," Checking offset is correct");
- if (imp != now + hr_off_64){
- printf("Failed! :(\n");
- printf("Difference is %" APR_INT64_T_FMT " (should be %"
- APR_INT64_T_FMT")\n", imp - now, hr_off_64);
- exit(-1);
+ apr_time_exp_tz(&xt, now, hr_off);
+ rv = apr_strftime(str, &sz, STR_SIZE, "%T", &xt);
+ if (rv == APR_ENOTIMPL) {
+ CuNotImpl(tc, "apr_strftime");
}
- printf("OK\n");
- printf(" ( %" APR_TIME_T_FMT " - %" APR_TIME_T_FMT
- " = %" APR_INT64_T_FMT " )\n", imp, now, imp - now);
+ CuAssertTrue(tc, rv == APR_SUCCESS);
+ CuAssertStrEquals(tc, "14:05:36", str);
+}
+
+CuSuite *testtime(void)
+{
+ CuSuite *suite = CuSuiteNew("Test Time");
- printf("\nTest Complete.\n");
- return 0;
-}
+ SUITE_ADD_TEST(suite, test_now);
+ SUITE_ADD_TEST(suite, test_gmtstr);
+ SUITE_ADD_TEST(suite, test_localstr);
+ SUITE_ADD_TEST(suite, test_exp_get_gmt);
+ SUITE_ADD_TEST(suite, test_exp_get_lt);
+ SUITE_ADD_TEST(suite, test_imp_gmt);
+ SUITE_ADD_TEST(suite, test_rfcstr);
+ SUITE_ADD_TEST(suite, test_ctime);
+ SUITE_ADD_TEST(suite, test_strftime);
+ SUITE_ADD_TEST(suite, test_strftimesmall);
+ SUITE_ADD_TEST(suite, test_exp_tz);
+ SUITE_ADD_TEST(suite, test_strftimeoffset);
+ return suite;
+}
+
+#ifdef SINGLE_PROG
+CuSuite *getsuite(void)
+{
+ return testtime();
+}
+#endif