[libsoup] Add more tests to tests/date.c



commit 16d186fc9ff0ca018416256a92ca36617a45a922
Author: Dan Winship <danw gnome org>
Date:   Sat Apr 18 11:40:10 2009 -0400

    Add more tests to tests/date.c
    
    Conversion between formats
    non-UTC timestamps
    floating timestamps
---
 tests/date.c |  256 ++++++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 214 insertions(+), 42 deletions(-)

diff --git a/tests/date.c b/tests/date.c
index 49afa9f..4e97d8a 100644
--- a/tests/date.c
+++ b/tests/date.c
@@ -11,6 +11,8 @@
 
 #include "test-utils.h"
 
+static gboolean check_ok (const char *strdate, SoupDate *date);
+
 static const struct {
 	SoupDateFormat format;
 	const char *date;
@@ -23,6 +25,26 @@ static const struct {
 	{ SOUP_DATE_ISO8601_XMLRPC,  "20041106T08:09:07" }
 };
 
+static void
+check_good (SoupDateFormat format, const char *strdate)
+{
+	SoupDate *date;
+	char *strdate2;
+
+	date = soup_date_new_from_string (strdate);
+	if (date)
+		strdate2 = soup_date_to_string (date, format);
+	if (!check_ok (strdate, date))
+		return;
+
+	if (strcmp (strdate, strdate2) != 0) {
+		debug_printf (1, "  restringification failed: '%s' -> '%s'\n",
+			      strdate, strdate2);
+		errors++;
+	}
+	g_free (strdate2);
+}
+
 static const char *ok_dates[] = {
 	/* rfc1123-date, and broken variants */
 	"Sat, 06 Nov 2004 08:09:07 GMT",
@@ -80,6 +102,32 @@ static const char *ok_dates[] = {
 	"Sat, 06 Nov 2004 08:09:7 GMT"
 };
 
+#define TIME_T 1099728547L
+#define TIME_T_STRING "1099728547"
+
+static gboolean
+check_ok (const char *strdate, SoupDate *date)
+{
+	debug_printf (2, "%s\n", strdate);
+
+	if (date &&
+	    date->year == 2004 && date->month == 11 && date->day == 6 &&
+	    date->hour == 8 && date->minute == 9 && date->second == 7) {
+		soup_date_free (date);
+		return TRUE;
+	}
+
+	debug_printf (1, "  date parsing failed for '%s'.\n", strdate);
+	if (date) {
+		debug_printf (1, "    got: %d %d %d - %d %d %d\n\n",
+			      date->year, date->month, date->day,
+			      date->hour, date->minute, date->second);
+		soup_date_free (date);
+	}
+	errors++;
+	return FALSE;
+}
+
 static const char *bad_dates[] = {
 	/* broken rfc1123-date */
 	", 06 Nov 2004 08:09:07 GMT",
@@ -116,65 +164,185 @@ static const char *bad_dates[] = {
 	"Sat Nov  6 08:09:07 GMT 2004"
 };
 
-#define TIME_T 1099728547L
-#define TIME_T_STRING "1099728547"
-
-static gboolean
-check_ok (const char *strdate, SoupDate *date)
+static void
+check_bad (const char *strdate, SoupDate *date)
 {
 	debug_printf (2, "%s\n", strdate);
 
-	if (date &&
-	    date->year == 2004 && date->month == 11 && date->day == 6 &&
-	    date->hour == 8 && date->minute == 9 && date->second == 7) {
-		soup_date_free (date);
-		return TRUE;
-	}
-
-	debug_printf (1, "  date parsing failed for '%s'.\n", strdate);
-	if (date) {
-		debug_printf (1, "    got: %d %d %d - %d %d %d\n\n",
-			      date->year, date->month, date->day,
-			      date->hour, date->minute, date->second);
-		soup_date_free (date);
-	}
+	if (!date)
+		return;
 	errors++;
-	return FALSE;
+
+	debug_printf (1, "  date parsing succeeded for '%s'!\n", strdate);
+	debug_printf (1, "    got: %d %d %d - %d %d %d\n\n",
+		      date->year, date->month, date->day,
+		      date->hour, date->minute, date->second);
+	soup_date_free (date);
 }
 
+static const struct conversion {
+	const char *source;
+	const char *http, *cookie, *rfc2822, *compact, *full, *xmlrpc;
+} conversions[] = {
+	/* SOUP_DATE_HTTP */
+	{ "Sat, 06 Nov 2004 08:09:07 GMT",
+
+	  "Sat, 06 Nov 2004 08:09:07 GMT",
+	  "Sat, 06-Nov-2004 08:09:07 GMT",
+	  "Sat, 6 Nov 2004 08:09:07 +0000",
+	  "20041106T080907Z",
+	  "2004-11-06T08:09:07Z",
+	  "20041106T08:09:07" },
+
+	/* RFC2822 GMT */
+	{ "Sat, 6 Nov 2004 08:09:07 +0000",
+
+	  "Sat, 06 Nov 2004 08:09:07 GMT",
+	  "Sat, 06-Nov-2004 08:09:07 GMT",
+	  "Sat, 6 Nov 2004 08:09:07 +0000",
+	  "20041106T080907Z",
+	  "2004-11-06T08:09:07Z",
+	  "20041106T08:09:07" },
+
+	/* RFC2822 with positive offset */
+	{ "Sat, 6 Nov 2004 08:09:07 +0430",
+
+	  "Sat, 06 Nov 2004 04:39:07 GMT",
+	  "Sat, 06-Nov-2004 04:39:07 GMT",
+	  "Sat, 6 Nov 2004 08:09:07 +0430",
+	  "20041106T080907+0430",
+	  "2004-11-06T08:09:07+04:30",
+	  "20041106T08:09:07" },
+
+	/* RFC2822 with negative offset */
+	{ "Sat, 6 Nov 2004 08:09:07 -0430",
+
+	  "Sat, 06 Nov 2004 12:39:07 GMT",
+	  "Sat, 06-Nov-2004 12:39:07 GMT",
+	  "Sat, 6 Nov 2004 08:09:07 -0430",
+	  "20041106T080907-0430",
+	  "2004-11-06T08:09:07-04:30",
+	  "20041106T08:09:07" },
+
+	/* RFC2822 floating */
+	{ "Sat, 6 Nov 2004 08:09:07 -0000",
+
+	  "Sat, 06 Nov 2004 08:09:07 GMT",
+	  "Sat, 06-Nov-2004 08:09:07 GMT",
+	  "Sat, 6 Nov 2004 08:09:07 -0000",
+	  "20041106T080907",
+	  "2004-11-06T08:09:07",
+	  "20041106T08:09:07" },
+
+	/* ISO GMT */
+	{ "2004-11-06T08:09:07Z",
+
+	  "Sat, 06 Nov 2004 08:09:07 GMT",
+	  "Sat, 06-Nov-2004 08:09:07 GMT",
+	  "Sat, 6 Nov 2004 08:09:07 +0000",
+	  "20041106T080907Z",
+	  "2004-11-06T08:09:07Z",
+	  "20041106T08:09:07" },
+
+	/* ISO with positive offset */
+	{ "2004-11-06T08:09:07+04:30",
+
+	  "Sat, 06 Nov 2004 04:39:07 GMT",
+	  "Sat, 06-Nov-2004 04:39:07 GMT",
+	  "Sat, 6 Nov 2004 08:09:07 +0430",
+	  "20041106T080907+0430",
+	  "2004-11-06T08:09:07+04:30",
+	  "20041106T08:09:07" },
+
+	/* ISO with negative offset */
+	{ "2004-11-06T08:09:07-04:30",
+
+	  "Sat, 06 Nov 2004 12:39:07 GMT",
+	  "Sat, 06-Nov-2004 12:39:07 GMT",
+	  "Sat, 6 Nov 2004 08:09:07 -0430",
+	  "20041106T080907-0430",
+	  "2004-11-06T08:09:07-04:30",
+	  "20041106T08:09:07" },
+
+	/* ISO floating */
+	{ "2004-11-06T08:09:07",
+
+	  "Sat, 06 Nov 2004 08:09:07 GMT",
+	  "Sat, 06-Nov-2004 08:09:07 GMT",
+	  "Sat, 6 Nov 2004 08:09:07 -0000",
+	  "20041106T080907",
+	  "2004-11-06T08:09:07",
+	  "20041106T08:09:07" }
+};
+
 static void
-check_good (SoupDateFormat format, const char *strdate)
+check_conversion (const struct conversion *conv)
 {
 	SoupDate *date;
-	char *strdate2;
+	char *str;
 
-	date = soup_date_new_from_string (strdate);
-	if (date)
-		strdate2 = soup_date_to_string (date, format);
-	if (!check_ok (strdate, date))
+	debug_printf (2, "%s\n", conv->source);
+	date = soup_date_new_from_string (conv->source);
+	if (!date) {
+		debug_printf (1, "  date parsing failed for '%s'.\n", conv->source);
+		errors++;
 		return;
+	}
 
-	if (strcmp (strdate, strdate2) != 0) {
-		debug_printf (1, "  restringification failed: '%s' -> '%s'\n",
-			      strdate, strdate2);
+	str = soup_date_to_string (date, SOUP_DATE_HTTP);
+	if (!str || strcmp (str, conv->http) != 0) {
+		debug_printf (1, "  conversion of '%s' to HTTP failed:\n"
+			      "    wanted: %s\n    got:    %s\n",
+			      conv->source, conv->http, str ? str : "(null)");
 		errors++;
 	}
-	g_free (strdate2);
-}
+	g_free (str);
 
-static void
-check_bad (const char *strdate, SoupDate *date)
-{
-	debug_printf (2, "%s\n", strdate);
+	str = soup_date_to_string (date, SOUP_DATE_COOKIE);
+	if (!str || strcmp (str, conv->cookie) != 0) {
+		debug_printf (1, "  conversion of '%s' to COOKIE failed:\n"
+			      "    wanted: %s\n    got:    %s\n",
+			      conv->source, conv->cookie, str ? str : "(null)");
+		errors++;
+	}
+	g_free (str);
 
-	if (!date)
-		return;
-	errors++;
+	str = soup_date_to_string (date, SOUP_DATE_RFC2822);
+	if (!str || strcmp (str, conv->rfc2822) != 0) {
+		debug_printf (1, "  conversion of '%s' to RFC2822 failed:\n"
+			      "    wanted: %s\n    got:    %s\n",
+			      conv->source, conv->rfc2822, str ? str : "(null)");
+		errors++;
+	}
+	g_free (str);
+
+	str = soup_date_to_string (date, SOUP_DATE_ISO8601_COMPACT);
+	if (!str || strcmp (str, conv->compact) != 0) {
+		debug_printf (1, "  conversion of '%s' to COMPACT failed:\n"
+			      "    wanted: %s\n    got:    %s\n",
+			      conv->source, conv->compact, str ? str : "(null)");
+		errors++;
+	}
+	g_free (str);
+
+	str = soup_date_to_string (date, SOUP_DATE_ISO8601_FULL);
+	if (!str || strcmp (str, conv->full) != 0) {
+		debug_printf (1, "  conversion of '%s' to FULL failed:\n"
+			      "    wanted: %s\n    got:    %s\n",
+			      conv->source, conv->full, str ? str : "(null)");
+		errors++;
+	}
+	g_free (str);
+
+	str = soup_date_to_string (date, SOUP_DATE_ISO8601_XMLRPC);
+	if (!str || strcmp (str, conv->xmlrpc) != 0) {
+		debug_printf (1, "  conversion of '%s' to XMLRPC failed:\n"
+			      "    wanted: %s\n    got:    %s\n",
+			      conv->source, conv->xmlrpc, str ? str : "(null)");
+		errors++;
+	}
+	g_free (str);
 
-	debug_printf (1, "  date parsing succeeded for '%s'!\n", strdate);
-	debug_printf (1, "    got: %d %d %d - %d %d %d\n\n",
-		      date->year, date->month, date->day,
-		      date->hour, date->minute, date->second);
 	soup_date_free (date);
 }
 
@@ -198,6 +366,10 @@ main (int argc, char **argv)
 	for (i = 0; i < G_N_ELEMENTS (bad_dates); i++)
 		check_bad (bad_dates[i], soup_date_new_from_string (bad_dates[i]));
 
+	debug_printf (1, "\nConversions:\n");
+	for (i = 0; i < G_N_ELEMENTS (conversions); i++)
+		check_conversion (&conversions[i] );
+
 	test_cleanup ();
 	return errors != 0;
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]