[glib] gdate: Comment that g_date_valid_dmy() does all necessary bounds checks
- From: Philip Withnall <pwithnall src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib] gdate: Comment that g_date_valid_dmy() does all necessary bounds checks
- Date: Fri, 13 Apr 2018 14:32:37 +0000 (UTC)
commit 33749d837cc9ebada6c1eef722c467a4a655e6e0
Author: Philip Withnall <withnall endlessm com>
Date: Thu Apr 12 18:06:41 2018 +0100
gdate: Comment that g_date_valid_dmy() does all necessary bounds checks
Make it more obvious that an explicit check isn’t needed for the upper
bound on years, since it’s limited by the type width.
Add a unit test to demonstrate this.
Signed-off-by: Philip Withnall <withnall endlessm com>
https://bugzilla.gnome.org/show_bug.cgi?id=540013
glib/gdate.c | 2 ++
glib/tests/date.c | 43 +++++++++++++++++++++++++++++++++++++++++++
2 files changed, 45 insertions(+)
---
diff --git a/glib/gdate.c b/glib/gdate.c
index b2ce2b32f..fbfcadcfd 100644
--- a/glib/gdate.c
+++ b/glib/gdate.c
@@ -494,6 +494,8 @@ g_date_valid_dmy (GDateDay d,
GDateMonth m,
GDateYear y)
{
+ /* No need to check the upper bound of @y, because #GDateYear is 16 bits wide,
+ * just like #GDate.year. */
return ( (m > G_DATE_BAD_MONTH) &&
(m < 13) &&
(d > G_DATE_BAD_DAY) &&
diff --git a/glib/tests/date.c b/glib/tests/date.c
index e05a709d3..e2e1f7fb9 100644
--- a/glib/tests/date.c
+++ b/glib/tests/date.c
@@ -606,6 +606,48 @@ test_copy (void)
g_date_free (d);
}
+/* Check the results of g_date_valid_dmy() for various inputs. */
+static void
+test_valid_dmy (void)
+{
+ const struct
+ {
+ GDateDay day;
+ GDateMonth month;
+ GDateYear year;
+ gboolean expected_valid;
+ }
+ vectors[] =
+ {
+ /* Lower bounds */
+ { 0, 0, 0, FALSE },
+ { 1, 1, 1, TRUE },
+ { 1, 1, 0, FALSE },
+ /* Leap year month lengths */
+ { 30, 2, 2000, FALSE },
+ { 29, 2, 2000, TRUE },
+ { 29, 2, 2001, FALSE },
+ /* Maximum year */
+ { 1, 1, G_MAXUINT16, TRUE },
+ };
+ gsize i;
+
+ for (i = 0; i < G_N_ELEMENTS (vectors); i++)
+ {
+ gboolean valid;
+ g_test_message ("Vector %" G_GSIZE_FORMAT ": %04u-%02u-%02u, %s",
+ i, vectors[i].year, vectors[i].month, vectors[i].day,
+ vectors[i].expected_valid ? "valid" : "invalid");
+
+ valid = g_date_valid_dmy (vectors[i].day, vectors[i].month, vectors[i].year);
+
+ if (vectors[i].expected_valid)
+ g_assert_true (valid);
+ else
+ g_assert_false (valid);
+ }
+}
+
int
main (int argc, char** argv)
{
@@ -653,6 +695,7 @@ main (int argc, char** argv)
g_free (path);
}
g_test_add_func ("/date/copy", test_copy);
+ g_test_add_func ("/date/valid-dmy", test_valid_dmy);
return g_test_run ();
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]