New tests for UTC parser and UTC formatting
authorArjen Baart <arjen@andromeda.nl>
Sat, 28 Mar 2020 13:22:19 +0000 (14:22 +0100)
committerArjen Baart <arjen@andromeda.nl>
Sat, 28 Mar 2020 13:22:19 +0000 (14:22 +0100)
12 files changed:
doc/xml.xml
src/String.h
src/string.cpp
src/utc.cpp
test/Makefile.am
test/string_basics.cpp
test/string_basics.exp
test/utc_convert.cpp [new file with mode: 0644]
test/utc_convert.exp [new file with mode: 0644]
test/utc_parse.cpp [new file with mode: 0644]
test/utc_parse.exp [new file with mode: 0644]
test/xml_node.exp [new file with mode: 0644]

index 11d9e34..09b1da4 100644 (file)
@@ -92,8 +92,20 @@ Return the child node with index n.
 <heading>class xml_element : xml_node</heading>
 <para>
 The class xml_element is derived from the class xml_node and adds properties
-soecific to XML ekements.
+specific to XML elements.
 </para>
+<description>
+<item tag="xml_element()">
+The default constructor creates an empty xml element.
+</item>
+<item tag="xml_element(xml doc)">
+Construct an element from the XML document. The xml_element object will be the root element
+of the XML document.
+</item>
+<item tag="std::vector&lt;xml_element&gt; operator[](String tagname);">
+Find the child elements with the specified tagname.
+</item>
+</description>
 
 </section>
 
index 02cafc4..195f88a 100644 (file)
@@ -141,7 +141,7 @@ public:
 
    /*     */
 
-   char& operator[](int i);    //  Individual character access
+   char& operator[](size_t i);    //  Individual character access
 
    int operator~() const      //  Length of the String
    {
index 742fa1b..7f99d83 100644 (file)
@@ -354,8 +354,11 @@ String & String::operator>>=(int n)
 substring String::operator()(int start, int len)
 {
    substring sub;
+   size_t    _start;
 
-   if (start >= strlen(p->s) || start + len >= strlen(p->s))
+   _start = start;   //  Proper type conversion
+
+   if (_start >= strlen(p->s) || _start + len >= strlen(p->s))
    {
       throw StringException("Substring Out of bounds: (" 
                     + String(start) + ", " + String(len) + ")");
@@ -432,7 +435,7 @@ std::istream& operator>>(std::istream& s, String& x)
 // TODO: boundary check.
 // TODO: Assignment to an individual character does not decouple references (BUG)
 
-char& String::operator[](int i)
+char& String::operator[](size_t i)
 {
    if (i >= strlen(p->s))
    {
index 934ba38..59d87d7 100644 (file)
@@ -64,6 +64,13 @@ UTC::UTC(String s)
       d.month = pd->month;
       d.day   = pd->day;
    }
+
+   if (t.hours == -1)
+      t.hours = 0;
+   if (t.minutes == -1)
+      t.minutes = 0;
+   if (t.seconds == -1)
+      t.seconds = 0;
 }
 
 UTC Now(bool local)
index f593b49..f9e14b7 100644 (file)
@@ -5,7 +5,7 @@ LDADD = ../src/.libs/libACL.la
 check_PROGRAMS = string_assign string_basics string_compare string_cat string_substring string_regex \
                  date_assign date_parse date_compare date_arithmetic date_attributes date_check_today \
                  hour_assign hour_parse hour_compare hour_arithmetic hour_check_now \
-                 utc_assign utc_compare utc_arithmetic \
+                 utc_assign utc_parse utc_compare utc_arithmetic utc_convert \
                  integer_assign integer_factorial integer_fibonacci integer_pow64 integer_modulo \
                  xml_file xml_find xml_node xml_elem \
                  configuration_read configuration_find
@@ -35,8 +35,10 @@ hour_check_now_SOURCES     = hour_check_now.cpp
 
 hour_now : hour_check_now
 utc_assign_SOURCES         = utc_assign.cpp
+utc_parse_SOURCES          = utc_parse.cpp
 utc_compare_SOURCES        = utc_compare.cpp
 utc_arithmetic_SOURCES     = utc_arithmetic.cpp
+utc_convert_SOURCES        = utc_convert.cpp
 
 integer_assign_SOURCES     = integer_assign.cpp
 integer_factorial_SOURCES  = integer_factorial.cpp tInteger.cpp
index e824c6d..6b75ad6 100644 (file)
@@ -74,7 +74,7 @@ int main()
    // Out of bounds
    try
    {
-      c = s1[-1];
+      c = s1[7];
       std::cout << "Seventh character of " << s1 << " char(" << (int)c << ")\n";
    }
    catch (StringException se)
index cc4d34a..f511adf 100644 (file)
@@ -6,7 +6,7 @@ The default contructor makes an empty string: ""
 The length of "abcde" is 5
 First character of abcde 'a'
 Fourth character of abcde 'd'
-String exception: Out of bounds: -1
+String exception: Out of bounds: 7
 After "abcde"[2] = 'Z' : abZde
 String exception: Out of bounds: 5
 After "abcde"[5] = 'X' : abZde
diff --git a/test/utc_convert.cpp b/test/utc_convert.cpp
new file mode 100644 (file)
index 0000000..f9dbb87
--- /dev/null
@@ -0,0 +1,38 @@
+/*******************************************************
+ *  Unit test for the UTC class
+ *
+ * test conversion and formatting of UTC objects
+ ******************************************************
+ *
+ */
+
+#include "date.h"
+#include <assert.h>
+
+int main()
+{
+   String formatted;
+
+   date d1(15, 9, 2002);
+   hour h1(15, 23, 46);
+
+   UTC  u1(d1, h1);
+
+   std::cout << "Contructor with date = 15-9-2002, hour = 15:23:46: " << u1 << "\n";
+   formatted = u1.format();
+   std::cout << "Formatted with default format: " << formatted << "\n";
+   assert(formatted == "2002-09-15 15:23:46");
+
+
+   String s3("23-10-2019 23:45");
+   UTC    u3(s3);
+
+   std::cout << "Contructor from String " << s3 << " : " << u3 << "\n";
+   formatted = u3.format();
+   std::cout << "Formatted with default format: " << formatted << "\n";
+   assert(formatted == "2019-10-23 23:45:00");
+
+
+   return 0;
+}
+
diff --git a/test/utc_convert.exp b/test/utc_convert.exp
new file mode 100644 (file)
index 0000000..bdbfcef
--- /dev/null
@@ -0,0 +1,5 @@
+Contructor with date = 15-9-2002, hour = 15:23:46: 15-09-2002 15:23:46
+Formatted with default format: 2002-09-15 15:23:46
+Contructor from String 23-10-2019 23:45 : 23-10-2019 23:45:00
+Formatted with default format: 2019-10-23 23:45:00
+PASS utc_convert (exit status: 0)
diff --git a/test/utc_parse.cpp b/test/utc_parse.cpp
new file mode 100644 (file)
index 0000000..8a4b4f3
--- /dev/null
@@ -0,0 +1,100 @@
+/****************************************************************
+ *  Unit test for the UTC class
+ *
+ * test the date and time parser that converts String to UTC
+ ***************************************************************
+ *
+ */
+
+#include "date.h"
+#include <assert.h>
+
+int main()
+{
+   UTC   u0;
+   String the_date;
+
+
+   the_date = "20120208 113456";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, 2012));
+   assert(hour(u0) == hour(11, 34, 56));
+
+   the_date = "2012-02-08";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, 2012));
+   assert(hour(u0) == hour(0, 0, 0));
+
+   the_date = "2012-02-08 11:34";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, 2012));
+   assert(hour(u0) == hour(11, 34, 0));
+
+   the_date = "2012-02-08 11:34:56";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, 2012));
+   assert(hour(u0) == hour(11, 34, 56));
+
+   the_date = "2012 02 08 11.34.56";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, 2012));
+   assert(hour(u0) == hour(11, 34, 56));
+
+   the_date = "08-02-2012";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, 2012));
+
+   the_date = "02/08/2012";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, 2012));
+
+   the_date = "Feb 8 2012";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, 2012));
+
+   the_date = "2012 Feb 8";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, 2012));
+
+   the_date = "8 FEB 2012";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, 2012));
+
+   the_date = "2012 8 feb";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, 2012));
+
+   the_date = "8 February 2012 15:00";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, 2012));
+   assert(hour(u0) == hour(15, 0, 0));
+
+   the_date = "Sun Jun 16 13:02:56 CEST 2019";   // A UNIX date string
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(16, 6, 2019));
+   assert(hour(u0) == hour(13, 2, 56));
+
+   //  Without a year: year = -1
+   the_date = "8 February";
+   u0 = UTC(the_date);
+   std::cout << "String \"" << the_date << "\" converts to: \"" << u0 << "\"\n";
+   assert(date(u0) == date(8, 2, -1));
+
+   //  TODO: test for invalid dates and syntax errors
+
+   return 0;
+}
+
diff --git a/test/utc_parse.exp b/test/utc_parse.exp
new file mode 100644 (file)
index 0000000..cb07c6b
--- /dev/null
@@ -0,0 +1,15 @@
+String "20120208 113456" converts to: "08-02-2012 11:34:56"
+String "2012-02-08" converts to: "08-02-2012 00:00:00"
+String "2012-02-08 11:34" converts to: "08-02-2012 11:34:00"
+String "2012-02-08 11:34:56" converts to: "08-02-2012 11:34:56"
+String "2012 02 08 11.34.56" converts to: "08-02-2012 11:34:56"
+String "08-02-2012" converts to: "08-02-2012 00:00:00"
+String "02/08/2012" converts to: "08-02-2012 00:00:00"
+String "Feb 8 2012" converts to: "08-02-2012 00:00:00"
+String "2012 Feb 8" converts to: "08-02-2012 00:00:00"
+String "8 FEB 2012" converts to: "08-02-2012 00:00:00"
+String "2012 8 feb" converts to: "08-02-2012 00:00:00"
+String "8 February 2012 15:00" converts to: "08-02-2012 15:00:00"
+String "Sun Jun 16 13:02:56 CEST 2019" converts to: "16-06-2019 13:02:56"
+String "8 February" converts to: "08-02--1 00:00:00"
+PASS utc_parse (exit status: 0)
diff --git a/test/xml_node.exp b/test/xml_node.exp
new file mode 100644 (file)
index 0000000..d7227f8
--- /dev/null
@@ -0,0 +1,28 @@
+Reading XML file xml_test01.xml
+Root element is doc
+Root node type is 1
+Node 0 type is 3
+Node 0 name is text
+Node 1 type is 1
+Node 1 name is titlepage
+Node 2 type is 3
+Node 2 name is text
+Node 3 type is 1
+Node 3 name is toc
+Node 4 type is 3
+Node 4 name is text
+Node 5 type is 1
+Node 5 name is chapter
+Node 6 type is 3
+Node 6 name is text
+Node 7 type is 1
+Node 7 name is chapter
+Node 8 type is 3
+Node 8 name is text
+Node 9 type is 1
+Node 9 name is chapter
+Node 10 type is 3
+Node 10 name is text
+Node 11 type is 0
+Node 11 name is 
+PASS xml_node (exit status: 0)