r1703 - in trunk/src/target/OM-2007/applications/openmoko-rssreader: src tests
zecke at sita.openmoko.org
zecke at sita.openmoko.org
Sat Apr 7 23:59:47 CEST 2007
Author: zecke
Date: 2007-04-07 23:59:47 +0200 (Sat, 07 Apr 2007)
New Revision: 1703
Modified:
trunk/src/target/OM-2007/applications/openmoko-rssreader/src/rfcdate.c
trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/date_test.c
Log:
openmoko-rssreader: Add more tests and besides the memory leaks of Glib we are doing fine
Modified: trunk/src/target/OM-2007/applications/openmoko-rssreader/src/rfcdate.c
===================================================================
--- trunk/src/target/OM-2007/applications/openmoko-rssreader/src/rfcdate.c 2007-04-07 19:36:25 UTC (rev 1702)
+++ trunk/src/target/OM-2007/applications/openmoko-rssreader/src/rfcdate.c 2007-04-07 21:59:47 UTC (rev 1703)
@@ -177,9 +177,13 @@
* %a but for the %b we need to look it up properly
*/
int day, year, hour, minute, second;
+ day = year = hour = minute = second = 0;
gchar month_name[4];
sscanf (rfc822date, "%*3s, %d %3s %d %d:%d:%d", &day, month_name, &year, &hour, &minute, &second );
+ if ( year < 100 )
+ year += 1900;
+
self->timeval.tv_sec = hour*60*60 + minute*60 + second;
self->timeval.tv_usec = 0;
g_date_set_dmy ( self->date, day, rss_month_number (month_name), year);
@@ -216,13 +220,13 @@
g_date_set_time_val (date, &now);
if ( g_date_compare( date, self->date ) == 0 ) {
- date_string = g_string_new (_("Today"));
+ date_string = g_string_new (_("Today,"));
goto exit;
}
g_date_subtract_days( date, 1 );
if ( g_date_compare( date, self->date ) == 0 ) {
- date_string = g_string_new (_("Yesterday"));
+ date_string = g_string_new (_("Yesterday,"));
goto exit;
}
@@ -230,16 +234,19 @@
* copy the date using the current locale. And retry
* until the buffer is big enough
*/
- date_string = g_string_sized_new( 10 );
- while ( g_date_strftime( date_string->str, date_string->allocated_len-1, "%a, %d %b %Y", self->date ) == 0 ) {
+ date_string = g_string_sized_new( 40 );
+ gsize result;
+ while ( (result = g_date_strftime( date_string->str, date_string->allocated_len-1, "%a, %e %b %Y", self->date )) == 0 ) {
g_string_set_size( date_string, date_string->allocated_len + 10 );
}
+ g_string_set_size (date_string, result);
+
exit:
/*
* append the time
*/
- g_string_append_printf ( date_string, ", %ld:%ld:%ld",
+ g_string_append_printf ( date_string, " %ld:%ld:%ld",
self->timeval.tv_sec/60/60,
self->timeval.tv_sec/60%60,
self->timeval.tv_sec%60);
Modified: trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/date_test.c
===================================================================
--- trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/date_test.c 2007-04-07 19:36:25 UTC (rev 1702)
+++ trunk/src/target/OM-2007/applications/openmoko-rssreader/tests/date_test.c 2007-04-07 21:59:47 UTC (rev 1703)
@@ -1,5 +1,7 @@
#include <check.h>
#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
#include <glib.h>
#include "rfcdate.h"
@@ -16,13 +18,88 @@
RSSRFCDate *date = RSS_RFC_DATE(rss_rfc_date_new ());
rss_rfc_date_set (date, "Wed, 26 Jan 83 07:57");
+ fail_unless (g_date_valid (date->date), "Date is valid");
fail_unless (g_date_get_month (date->date) == G_DATE_JANUARY, "Seldom year number, seldom time");
fail_unless (g_date_get_day (date->date) == 26, "Parsed my birthday");
fail_unless (g_date_get_year (date->date) == 1983, "Parsed my birthyear");
- fail_unless (date->timeval.tv_sec == (8*60*60+57*60), "Parsed my birthtime properly");
+ fail_unless (date->timeval.tv_sec == (7*60*60+57*60), "Parsed my birthtime properly");
+
+ g_object_unref (date);
}
END_TEST
+START_TEST(test_month_names)
+{
+ gchar date_buffer[40];
+ gchar *month_names[] = {
+ "Jan",
+ "Feb",
+ "Mar",
+ "Apr",
+ "May",
+ "Jun",
+ "Jul",
+ "Aug",
+ "Sep",
+ "Oct",
+ "Nov",
+ "Dec" };
+
+ RSSRFCDate *date = RSS_RFC_DATE(rss_rfc_date_new ());
+ fail_unless( date != NULL, "Allocated date" );
+
+ for ( int i = 0; i < 12; ++i ) {
+ snprintf (date_buffer, 40, "Mon, 9 %s 2041 23:23:23", month_names[i] );
+ rss_rfc_date_set ( date, date_buffer);
+ fail_unless (g_date_valid(date->date), "Parsing the date failed" );
+ fail_unless (g_date_get_month(date->date) == i+1, "Parsing the month named failed" );
+ fail_unless (date->timeval.tv_sec == 23*60*60+23*60+23 );
+ }
+
+ g_object_unref (date);
+}
+END_TEST
+
+/*
+ * make sure to run this with LC_ALL=C
+ */
+START_TEST(test_string_representation)
+{
+
+ static gchar * month_names[] = { "", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
+
+ RSSRFCDate *date = RSS_RFC_DATE(rss_rfc_date_new ());
+ rss_rfc_date_set( date, "Mon, 6 Mar 1848 13:24:43" );
+ g_print ("Date: %s\n", rss_rfc_date_as_string (date));
+ fail_unless( strcmp(rss_rfc_date_as_string (date), "Mon, 6 Mar 1848 13:24:43") == 0, "formatted string 1" );
+ fail_unless( strcmp(rss_rfc_date_as_string (date), "Mon, 6 Mar 1848 13:24:43") == 0, "formatted stringc cached?" );
+
+
+ GTimeVal now;
+ g_get_current_time (&now);
+ GDate *gdate = g_date_new ();
+ g_date_set_time_val (gdate, &now);
+ g_print( "Day: %d Month: %d Year: %d\n", g_date_day (gdate), g_date_month (gdate), g_date_year (gdate) );
+
+ gchar date_string[40];
+ snprintf( date_string, 40, "Mon, %d %s %d 23:10:53", g_date_day (gdate), month_names[g_date_month (gdate)], g_date_year (gdate) );
+ rss_rfc_date_set (date, date_string);
+ g_print ("Date string '%s' '%s'\n", date_string, rss_rfc_date_as_string (date));
+ fail_unless ( strcmp( "Today, 23:10:53", rss_rfc_date_as_string (date) ) == 0, "Correct today's formatting");
+ fail_unless ( strcmp( "Today, 23:10:53", rss_rfc_date_as_string (date) ) == 0, "Correct today's formatting (cached)");
+
+ g_date_subtract_days (gdate, 1);
+ snprintf( date_string, 40, "Mon, %d %s %d 23:10:53", g_date_day (gdate), month_names[g_date_month (gdate)], g_date_year (gdate) );
+ rss_rfc_date_set (date, date_string);
+ g_print ("Date string '%s' '%s'\n", date_string, rss_rfc_date_as_string (date));
+ fail_unless ( strcmp( "Yesterday, 23:10:53", rss_rfc_date_as_string (date) ) == 0, "Correct yesterday's formatting");
+ fail_unless ( strcmp( "Yesterday, 23:10:53", rss_rfc_date_as_string (date) ) == 0, "Correct yesterday's formatting (cached)");
+
+ g_date_free (gdate);
+ g_object_unref (date);
+}
+END_TEST
+
Suite*
date_suite (void)
{
@@ -30,6 +107,8 @@
TCase *tc_core = tcase_create ("Core");
tcase_add_test (tc_core, test_create_date);
tcase_add_test (tc_core, test_parsing_date);
+ tcase_add_test (tc_core, test_month_names);
+ tcase_add_test (tc_core, test_string_representation);
suite_add_tcase (s, tc_core);
More information about the commitlog
mailing list