Beispiel #1
0
int main (int argc, char** argv)
{
#if defined(HAVE_LIBLUA)
  UnitTest t (7);
#else
  UnitTest t (6);
#endif

  try
  {
    // Prime the pump.
    context.a3.capture ("task");

    // TODO dom.get rc.name
    DOM dom;
    t.is (dom.get ("system.version"),     VERSION,     "DOM system.version -> VERSION");
#ifdef HAVE_LIBLUA
    t.is (dom.get ("system.lua.version"), LUA_RELEASE, "DOM system.lua.version -> LUA_RELEASE");
#endif
    t.ok (dom.get ("system.os") != "<unknown>",        "DOM system.os -> != Unknown");
    t.is (dom.get ("context.program"),    "task",      "DOM context.program -> 'task'");
    t.is (dom.get ("context.args"),       "task",      "DOM context.args -> 'task'");
    t.is (dom.get ("context.width"),      "0",         "DOM context.width -> '0'");
    t.is (dom.get ("context.height"),     "0",         "DOM context.height -> '0'");

    // TODO dom.set rc.name
  }

  catch (std::string& error)
  {
    t.diag (error);
    return -1;
  }

  catch (...)
  {
    t.diag ("Unknown error.");
    return -2;
  }

  return 0;
}
Beispiel #2
0
int main (int argc, char** argv)
{
    UnitTest t (11);

    // Ensure environment has no influence.
    unsetenv ("TASKDATA");
    unsetenv ("TASKRC");

    try
    {
        t.is (format ("pre {1} post",    "mid"),        "pre mid post",    "format 1a");
        t.is (format ("pre {1}{1} post", "mid"),        "pre midmid post", "format 1b");
        t.is (format ("pre {1} post",    0),            "pre 0 post",      "format 1c");
        t.is (format ("pre {1}{1} post", 0),            "pre 00 post",     "format 1d");

        t.is (format ("pre {1}{2} post", "one", "two"), "pre onetwo post", "format 2a");
        t.is (format ("pre {2}{1} post", "one", "two"), "pre twoone post", "format 2b");
        t.is (format ("pre {1}{2} post", "one", 2),     "pre one2 post",   "format 2c");
        t.is (format ("pre {1}{2} post", 1, "two"),     "pre 1two post",   "format 2d");
        t.is (format ("pre {1}{2} post", 1, 2),         "pre 12 post",     "format 2e");

        t.is (format ("pre {1}{2}{3} post", "one", "two", "three"), "pre onetwothree post", "format 3a");
        t.is (format ("pre {3}{1}{2} post", "one", "two", "three"), "pre threeonetwo post", "format 3b");
    }

    catch (const std::string& error)
    {
        t.diag (error);
        return -1;
    }

    catch (...)
    {
        t.diag ("Unknown error.");
        return -2;
    }

    return 0;
}
Beispiel #3
0
int main (int argc, char** argv)
{
#ifdef NIBBLER_FEATURE_DATE
#ifdef NIBBLER_FEATURE_REGEX
  UnitTest t (410);
#else
  UnitTest t (380);
#endif
#else
#ifdef NIBBLER_FEATURE_REGEX
  UnitTest t (346);
#else
  UnitTest t (322);
#endif
#endif

  // Ensure environment has no influence.
  unsetenv ("TASKDATA");
  unsetenv ("TASKRC");

  try
  {
    Nibbler n;
    std::string s;
    int i;
    double d;
    time_t ti;

#ifdef NIBBLER_FEATURE_DATE
    Date dt;
#endif
    std::vector <std::string> options;

    // Make sure the nibbler behaves itself with trivial input.
    t.diag ("Test all nibbler calls given empty input");
    n = Nibbler ("");
    t.notok (n.getUntil (' ', s),        "trivial: getUntil");
    t.notok (n.getUntil ("hi", s),       "trivial: getUntil");
    t.notok (n.getUntilOneOf ("ab", s),  "trivial: getUntilOneOf");
    t.notok (n.skipN (123),              "trivial: skipN");
    t.notok (n.skip ('x'),               "trivial: skip");
    t.notok (n.skipAll ('x'),            "trivial: skipAll");
    t.notok (n.skipAllOneOf ("abc"),     "trivial: skipAllOneOf");
    t.notok (n.backN (1),                "trivial: backN");
    t.notok (n.getQuoted ('"', s),       "trivial: getQuoted");
    t.notok (n.getDigit (i),             "trivial: getDigit");
    t.notok (n.getInt (i),               "trivial: getInt"); // 10
    t.notok (n.getUnsignedInt (i),       "trivial: getUnsignedInt");
    t.notok (n.getUntilEOL (s),          "trivial: getUntilEOL");
    t.notok (n.getUntilEOS (s),          "trivial: getUntilEOS");
    t.notok (n.getDateISO (ti),          "trivial: getDateISO");
#ifdef NIBBLER_FEATURE_DATE
    t.notok (n.getDate ("YYYYMMDD", ti), "trivial: getDate");
#endif
    t.notok (n.getOneOf (options, s),    "trivial: getOneOf");
    t.ok    (n.depleted (),              "trivial: depleted");

    // bool getUntil (char, std::string&);
    t.diag ("Nibbler::getUntil");
    n = Nibbler ("one two");
    t.ok    (n.getUntil (' ', s),        " 'one two' :       getUntil (' ')    -> true");
    t.is    (s, "one",                   " 'one two' :       getUntil (' ')    -> 'one'"); // 20
    t.ok    (n.getUntil (' ', s),        "    ' two' :       getUntil (' ')    -> true");
    t.is    (s, "",                      "    ' two' :       getUntil (' ')    -> ''");
    t.ok    (n.skip (' '),               "    ' two' :           skip (' ')    -> true");
    t.ok    (n.getUntil (' ', s),        "     'two' :       getUntil (' ')    -> 'two'");
    t.notok (n.getUntil (' ', s),        "        '' :       getUntil (' ')    -> false");
    t.ok    (n.depleted (),              "        '' :       depleted ()       -> true");

#ifdef NIBBLER_FEATURE_REGEX
    // bool getUntilRx (const std::string&, std::string&);
    t.diag ("Nibbler::getUntilRx");
    n = Nibbler ("one two");
    t.ok    (n.getUntilRx ("th", s),     " 'one two' :     getUntilRx ('th')   -> true");
    t.is    (s, "one two",               " 'one two' :     getUntilRx ('th')   -> 'one two'");

    n = Nibbler ("one two");
    t.ok    (n.getUntilRx ("e", s),      " 'one two' :     getUntilRx ('e')    -> true");
    t.is    (s, "on",                    " 'one two' :     getUntilRx ('e')    -> 'on'"); // 30
    t.ok    (n.getUntilRx ("tw", s),     "   'e two' :     getUntilRx ('tw')   -> true");
    t.is    (s, "e ",                    "   'e two' :     getUntilRx ('tw')   -> 'e '");
    t.ok    (n.getUntilRx ("$", s),      "     'two' :     getUntilRx ('$')    -> true");
    t.is    (s, "two",                   "     'two' :     getUntilRx ('$')    -> 'two'");
    t.ok    (n.depleted (),              "        '' :       depleted ()       -> true");
#endif

    // bool getUntilOneOf (const std::string&, std::string&);
    t.diag ("Nibbler::getUntilOneOf");
    n = Nibbler ("ab.cd");
    t.ok    (n.getUntilOneOf (".:", s),  "   'ab.cd' :  getUntilOneOf ('.:')   -> true");
    t.is    (s, "ab",                    "   'ab.cd' :  getUntilOneOf ('.:')   -> 'ab'");
    t.ok    (n.skipN (),                 "     '.cd' :          skipN ()       -> true");
    t.ok    (n.getUntilOneOf (".:", s),  "      'cd' :  getUntilOneOf ('.:')   -> true");
    t.notok (n.getUntilOneOf (".:", s),  "        '' :  getUntilOneOf ('.:')   -> false");
    t.ok    (n.depleted (),              "        '' :       depleted ()       -> true");

    // bool getUntil (const std::string&, std::string&);
    t.diag ("Nibbler::getUntil");
    n = Nibbler ("ab\r\ncd");
    t.ok (n.getUntil ("\r\n", s),     "'ab\\r\\ncd' :       getUntil ('\\r\\n') -> true");
    t.ok (n.skipN (2),                "  '\\r\\ncd' :          skipN (2)      -> true");
    t.ok (n.getUntil ("\r\n", s),     "      'cd' :       getUntil ('\\r\\n') -> true");
    t.ok (n.depleted (),              "        '' :       depleted ()       -> true");

    // bool getUntilWS (std::string&);
    t.diag ("Nibbler::getUntilWS");
    n = Nibbler ("ab \t\ncd");
    t.ok    (n.getUntilWS (s),        " 'ab \\t\\ncd' :     getUntilWS ()     -> true");
    t.is    (s, "ab",                 " 'ab \\t\\ncd' :     getUntilWS ()     -> 'ab'");
    t.ok    (n.getUntilWS (s),        "   ' \\t\\ncd' :     getUntilWS ()     -> true");
    t.is    (s, "",                   "   ' \\t\\ncd' :     getUntilWS ()     -> ''");
    t.ok    (n.skipWS (),             "        'cd' :         skipWS ()     -> true");
    t.ok    (n.getUntilWS (s),        "          '' :     getUntilWS ()     -> true");
    t.is    (s, "cd",                 "        'cd' :     getUntilWS ()     -> 'cd'");
    t.ok    (n.depleted (),           "          '' :       depleted ()     -> true");

    // bool skipN (const int quantity = 1);
    t.diag ("Nibbler::skipN");
    n = Nibbler ("abcde");
    t.ok    (n.skipN (),              "   'abcde' :          skipN ()       -> true");
    t.ok    (n.skipN (2),             "    'bcde' :          skipN (2       -> true");
    t.notok (n.skipN (3),             "      'de' :          skipN (3       -> false");
    t.notok (n.depleted (),           "      'de' :       depleted ()       -> true");

    // bool skip (char);
    t.diag ("Nibbler::skip");
    n = Nibbler ("  a");
    t.ok    (n.skip (' '),            "     '  a' :           skip (' ')    -> true");
    t.ok    (n.skip (' '),            "      ' a' :           skip (' ')    -> true");
    t.notok (n.skip (' '),            "       'a' :           skip (' ')    -> false");
    t.notok (n.depleted (),           "       'a' :       depleted ()       -> false");
    t.ok    (n.skip ('a'),            "       'a' :           skip ('a')    -> true");
    t.ok    (n.depleted (),           "        '' :       depleted ()       -> true");

    // bool skipAll (char);
    t.diag ("Nibbler::skipAll");
    n = Nibbler ("aaaabb");
    t.ok    (n.skipAll ('a'),         "  'aaaabb' :        skipAll ('a')    -> true");
    t.notok (n.skipAll ('a'),         "      'bb' :        skipAll ('a')    -> false");
    t.ok    (n.skipAll ('b'),         "      'bb' :        skipAll ('b')    -> true");
    t.notok (n.skipAll ('b'),         "        '' :        skipAll ('b')    -> false");
    t.ok    (n.depleted (),           "        '' :       depleted ()       -> true");

    // bool skipAllOneOf (const std::string&);
    t.diag ("Nibbler::skipAllOneOf");
    n = Nibbler ("abababcc");
    t.ok    (n.skipAllOneOf ("ab"),   "'abababcc' :   skipAllOneOf ('ab')   -> true");
    t.notok (n.skipAllOneOf ("ab"),   "      'cc' :   skipAllOneOf ('ab')   -> false");
    t.ok    (n.skipAllOneOf ("c"),    "      'cc' :   skipAllOneOf ('ab')   -> false");
    t.ok    (n.depleted (),           "        '' :       depleted ()       -> true");

    // bool skipWS ();
    t.diag ("Nibbler::skipWS");
    n = Nibbler (" \tfoo");
    t.ok    (n.skipWS (),             "  ' \\tfoo' :         skipWS ()       -> true");
    t.notok (n.skipWS (),             "     'foo' :         skipWS ()       -> false");
    t.ok    (n.getUntilEOS (s),       "     'foo' :    getUntilEOS ()       -> true");
    t.is    (s, "foo",                "     'foo' :    getUntilEOS ()       -> 'foo'");
    t.ok    (n.depleted (),           "        '' :       depleted ()       -> true");

#ifdef NIBBLER_FEATURE_REGEX
    // bool skipRx (const std::string&);
    t.diag ("Nibbler::skipRx");
    n = Nibbler ("one two");
    t.ok    (n.skipRx ("o."),         " 'one two' :         skipRx ('o.')   -> true");
    t.notok (n.skipRx ("A+"),         "   'e two' :         skipRx ('A+')   -> false");
    t.ok    (n.skipRx ("e+"),         "   'e two' :         skipRx ('e+')   -> true");
    t.ok    (n.skipRx ("...."),       "    ' two' :         skipRx ('....') -> true");
    t.ok    (n.depleted (),           "        '' :       depleted ()       -> true");
#endif

    // bool backN (const int quantity = 1);
    t.diag ("Nibbler::backN");
    n = Nibbler ("/a/b/");
    t.ok (n.getQuoted ('/', s),       "   '/a/b/' :         getQuoted ('/') -> true");
    t.is (s, "a",                     "      'b/' :         getQuoted ('/') -> 'a'");
    t.ok (n.backN (),                 "      'b/' :         backN ()        -> true");
    t.ok (n.getQuoted ('/', s),       "     '/b/' :         getQuoted ('/') -> true");
    t.is (s, "b",                     "     '/b/' :         getQuoted ('/') -> 'b'");

    // bool getQuoted (char, std::string&);
    t.diag ("Nibbler::getQuoted");
    n = Nibbler ("''");
    t.ok    (n.getQuoted ('\'', s),   "      '''' :      getQuoted (''')    -> true");
    t.is    (s, "",                   "      '''' :      getQuoted (''')    -> ''");

    n = Nibbler ("'\"'");
    t.ok    (n.getQuoted ('\'', s),   "      ''\"'' :     getQuoted (''')    -> true");
    t.is    (s, "\"",                  "     ''\"'' :      getQuoted (''')    -> '\"'");

    n = Nibbler ("'x'");
    t.ok    (n.getQuoted ('\'', s),   "      ''x'' :     getQuoted (''')    -> true");
    t.is    (s, "x",                  "      ''x'' :     getQuoted (''')    -> ''");

    n = Nibbler ("'x");
    t.notok (n.getQuoted ('\'', s),   "      ''x' :      getQuoted (''')    -> false");

    n = Nibbler ("x");
    t.notok (n.getQuoted ('\'', s),   "       'x' :      getQuoted (''')    -> false"); // 90

    n = Nibbler ("\"one\\\"two\"");
    t.notok (n.getQuoted ('\'', s),   "\"one\\\"two\" :      getQuoted (''')    -> false");

    n = Nibbler ("\"one\\\"two\"");
    t.ok (n.getQuoted ('"', s, false), "\"one\\\"two\" :      getQuoted ('\"', false, false) -> true");
    t.is (s, "one\\\"two", "getQuoted ('\"', false) -> one\\\"two");

    n = Nibbler ("\"one\\\"two\"");
    t.ok (n.getQuoted ('"', s, true),  "\"one\\\"two\" :      getQuoted ('\"', false, true)  -> true");
    t.is (s, "\"one\\\"two\"", "getQuoted ('\"', true) -> \"one\\\"two\"");

    n = Nibbler ("\"one\\\"two\"");
    t.ok (n.getQuoted ('"', s, false), "\"one\\\"two\" :      getQuoted ('\"', true,  false) -> true");
    t.is (s, "one\\\"two", "getQuoted ('\"', false) -> one\\\"two");

    n = Nibbler ("\"one\\\"two\"");
    t.ok (n.getQuoted ('"', s, true),  "\"one\\\"two\" :      getQuoted ('\"', s,  true)  -> true");
    t.is (s, "\"one\\\"two\"", "getQuoted ('\"', s, true) -> \"one\\\"two\"");

    n = Nibbler ("\"one\\\\\"");
    t.ok (n.getQuoted ('\"', s, true), "\"one\\\" :           getQuoted ('\"', s, true)      -> true");
    t.is (s, "\"one\\\\\"",                                   "getQuoted ('\"', s, true)      -> \"one\\\\\"");

    n = Nibbler ("\"one\\\\\"");
    t.ok (n.getQuoted ('\"', s, false), "one\\ :              getQuoted ('\"', s, false)      -> true");
    t.is (s, "one\\\\",                                       "getQuoted ('\"', s, false)      -> \"one\\\\\"");

    // bool getDigit (int&);
    t.diag ("Nibbler::getDigit");
    n = Nibbler ("12x");
    t.ok    (n.getDigit (i),          "     '12x' :         getDigit ()     -> true");
    t.is    (i, 1,                    "     '12x' :         getDigit ()     -> 1");
    t.ok    (n.getDigit (i),          "      '2x' :         getDigit ()     -> true");
    t.is    (i, 2,                    "      '2x' :         getDigit ()     -> 2");
    t.notok (n.getDigit (i),          "       'x' :         getDigit ()     -> false");

    // bool getDigit6 (int&);
    t.diag ("Nibbler::getDigit6");
    n = Nibbler ("654321");
    t.ok    (n.getDigit6 (i),         "    654321 :         getDigit6 ()    -> true");
    t.is    (i, 654321,               "    654321 :         getDigit6 ()    -> 654321");

    // bool getDigit4 (int&);
    t.diag ("Nibbler::getDigit4");
    n = Nibbler ("4321");
    t.ok    (n.getDigit4 (i),         "      4321 :         getDigit4 ()    -> true");
    t.is    (i, 4321,                 "      4321 :         getDigit4 ()    -> 4321");

    // bool getDigit2 (int&);
    t.diag ("Nibbler::getDigit2");
    n = Nibbler ("21");
    t.ok    (n.getDigit2 (i),         "        21 :         getDigit2 ()    -> true");
    t.is    (i, 21,                   "        21 :         getDigit2 ()    -> 21");

    // bool getInt (int&);
    t.diag ("Nibbler::getInt");
    n = Nibbler ("123 -4");
    t.ok    (n.getInt (i),            "  '123 -4' :         getInt ()       -> true");
    t.is    (i, 123,                  "  '123 -4' :         getInt ()       -> '123'");
    t.ok    (n.skip (' '),            "     ' -4' :           skip (' ')    -> true");
    t.ok    (n.getInt (i),            "      '-4' :         getInt ()       -> true");
    t.is    (i, -4,                   "      '-4' :         getInt ()       -> '-4'");
    t.ok    (n.depleted (),           "        '' :       depleted ()       -> true");

    // bool getUnsignedInt (int&i);
    t.diag ("Nibbler::getUnsignedInt");
    n = Nibbler ("123 4");
    t.ok    (n.getUnsignedInt (i),    "   '123 4' : getUnsignedInt ()       -> true");
    t.is    (i, 123,                  "   '123 4' : getUnsignedInt ()       -> '123'");
    t.ok    (n.skip (' '),            "      ' 4' :           skip (' ')    -> true");
    t.ok    (n.getUnsignedInt (i),    "       '4' : getUnsignedInt ()       -> true");
    t.is    (i, 4,                    "       '4' : getUnsignedInt ()       -> '4'");
    t.ok    (n.depleted (),           "        '' :       depleted ()       -> true");

    // bool getNumber (double&);
    t.diag ("Nibbler::getNumber");
    n = Nibbler ("-1.234 2.3e4");
    t.ok    (n.getNumber (d),         "'-1.234 2.3e4' : getNumber ()       -> true");
    t.is    (d, -1.234, 0.000001,     "'-1.234 2.3e4' : getNumber ()       -> '-1.234'");
    t.ok    (n.skip (' '),            "      ' 2.3e4' : skip (' ')         -> true");
    t.ok    (n.getNumber (d),         "       '2.3e4' : getNumber ()       -> true");
    t.is    (d, 2.3e4,                "       '2.3e4' : getNumber ()       -> '2.3e4'");
    t.ok    (n.depleted (),           "            '' : depleted ()        -> true");

    n = Nibbler ("2.0");
    t.ok    (n.getNumber (d),         "'2.0' : getNumber ()                -> true");
    t.is    (d, 2.0, 0.000001,        "'2.0' : getNumber ()                -> '2.0'");
    t.ok    (n.depleted (),           "            '' : depleted ()        -> true");

    n = Nibbler ("-864000.00000");
    t.ok    (n.getNumber (d),         "'-864000.00000' : getNumber ()      -> true");
    t.is    (d, -864000.0,            "'-864000.00000' : getNumber ()      -> -864000.0");
    t.ok    (n.depleted (),           "             '' : depleted ()       -> true");

    // bool getLiteral (const std::string&);
    t.diag ("Nibbler::getLiteral");
    n = Nibbler ("foobar");
    t.ok    (n.getLiteral ("foo"),    "  'foobar' :     getLiteral ('foo')  -> true");
    t.notok (n.getLiteral ("foo"),    "     'bar' :     getLiteral ('foo')  -> false");
    t.ok    (n.getLiteral ("bar"),    "     'bar' :     getLiteral ('bar')  -> true");
    t.ok    (n.depleted (),           "        '' :       depleted ()       -> true");

#ifdef NIBBLER_FEATURE_REGEX
    // bool getRx (const std::string&, std::string&);
    t.diag ("Nibbler::getRx");
    n = Nibbler ("one two three");
    t.ok    (n.getRx ("^(o..)", s),   "'one two three' :   getRx ('^(o..)')  -> true");
    t.is    (s, "one",                "'one two three' :   getRx ('^(o..)')  -> 'one'");
    t.ok    (n.skip (' '),            "   ' two three' :         skip (' ')  -> true");
    t.ok    (n.getRx ("t..", s),      "    'two three' :   getRx ('t..')     -> true");
    t.is    (s, "two",                "    'two three' :   getRx ('t..')     -> 'two'");
    t.notok (n.getRx ("th...", s),    "       ' three' :   getRx ('th...')   -> false");
    t.ok    (n.skip (' '),            "       ' three' :         skip (' ')  -> true");
    t.ok    (n.getRx ("th...", s),    "        'three' :   getRx ('th...')   -> true");
    t.is    (s, "three",              "        'three' :   getRx ('th...')   -> 'three'");
    t.ok    (n.depleted (),           "             '' :       depleted ()   -> true");
#endif

    // bool getUUID (std::string&);
    t.diag ("Nibbler::getUUID");
    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3c4d5");
    t.ok (n.getUUID (s),                             "uuid 1 found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3c4d5", "uuid 1 -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("00000000-0000-0000-0000-000000000000,a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3c4d5");
    t.ok (n.getUUID (s),                             "uuid 1 found");
    t.is (s, "00000000-0000-0000-0000-000000000000", "uuid 1 -> correct");
    t.ok (n.skip (','),                              "comma -> skipped");
    t.ok (n.getUUID (s),                             "uuid 2 -> found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3c4d5", "uuid 2 -> correct");
    t.ok (n.depleted (),                             "depleted");

    // bool getPartialUUID (std::string&);
    t.diag ("Nibbler::getPartialUUID");
    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3c4d5");
    t.ok (n.getPartialUUID (s),                      "partial uuid [36] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3c4d5", "partial uuid [36] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3c4d");
    t.ok (n.getPartialUUID (s),                      "partial uuid [35] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3c4d",  "partial uuid [35] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3c4");
    t.ok (n.getPartialUUID (s),                      "partial uuid [34] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3c4",   "partial uuid [34] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3c");
    t.ok (n.getPartialUUID (s),                      "partial uuid [33] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3c",    "partial uuid [33] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3");
    t.ok (n.getPartialUUID (s),                      "partial uuid [32] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b3",     "partial uuid [32] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b");
    t.ok (n.getPartialUUID (s),                      "partial uuid [31] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2b",      "partial uuid [31] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2");
    t.ok (n.getPartialUUID (s),                      "partial uuid [30] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F1a2",       "partial uuid [30] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E0F1a");
    t.ok (n.getPartialUUID (s),                      "partial uuid [29] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F1a",        "partial uuid [29] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E0F1");
    t.ok (n.getPartialUUID (s),                      "partial uuid [28] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F1",         "partial uuid [28] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E0F");
    t.ok (n.getPartialUUID (s),                      "partial uuid [27] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0F",          "partial uuid [27] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E0");
    t.ok (n.getPartialUUID (s),                      "partial uuid [26] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E0",           "partial uuid [26] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-E");
    t.ok (n.getPartialUUID (s),                      "partial uuid [25] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-E",            "partial uuid [25] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9-");
    t.ok (n.getPartialUUID (s),                      "partial uuid [24] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9-",             "partial uuid [24] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D9");
    t.ok (n.getPartialUUID (s),                      "partial uuid [23] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D9",              "partial uuid [23] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8D");
    t.ok (n.getPartialUUID (s),                      "partial uuid [22] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8D",               "partial uuid [22] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C8");
    t.ok (n.getPartialUUID (s),                      "partial uuid [21] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C8",                "partial uuid [21] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-C");
    t.ok (n.getPartialUUID (s),                      "partial uuid [20] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-C",                 "partial uuid [20] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7-");
    t.ok (n.getPartialUUID (s),                      "partial uuid [19] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7-",                  "partial uuid [19] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B7");
    t.ok (n.getPartialUUID (s),                      "partial uuid [18] found");
    t.is (s, "a0b1c2d3-e4f5-A6B7",                   "partial uuid [18] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6B");
    t.ok (n.getPartialUUID (s),                      "partial uuid [17] found");
    t.is (s, "a0b1c2d3-e4f5-A6B",                    "partial uuid [17] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A6");
    t.ok (n.getPartialUUID (s),                      "partial uuid [16] found");
    t.is (s, "a0b1c2d3-e4f5-A6",                     "partial uuid [16] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-A");
    t.ok (n.getPartialUUID (s),                      "partial uuid [15] found");
    t.is (s, "a0b1c2d3-e4f5-A",                      "partial uuid [15] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5-");
    t.ok (n.getPartialUUID (s),                      "partial uuid [14] found");
    t.is (s, "a0b1c2d3-e4f5-",                       "partial uuid [14] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f5");
    t.ok (n.getPartialUUID (s),                      "partial uuid [13] found");
    t.is (s, "a0b1c2d3-e4f5",                        "partial uuid [13] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4f");
    t.ok (n.getPartialUUID (s),                      "partial uuid [12] found");
    t.is (s, "a0b1c2d3-e4f",                         "partial uuid [12] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e4");
    t.ok (n.getPartialUUID (s),                      "partial uuid [11] found");
    t.is (s, "a0b1c2d3-e4",                          "partial uuid [11] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-e");
    t.ok (n.getPartialUUID (s),                      "partial uuid [10] found");
    t.is (s, "a0b1c2d3-e",                           "partial uuid [10] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3-");
    t.ok (n.getPartialUUID (s),                      "partial uuid [9] found");
    t.is (s, "a0b1c2d3-",                            "partial uuid [9] -> correct");
    t.ok (n.depleted (),                             "depleted");

    n = Nibbler ("a0b1c2d3");
    t.ok (n.getPartialUUID (s),                      "partial uuid [8] found");
    t.is (s, "a0b1c2d3",                             "partial uuid [8] -> correct");
    t.ok (n.depleted (),                             "not depleted");

    // bool getDateISO (time_t&);
    t.diag ("Nibbler::getDateISO");
    n = Nibbler ("19980119T070000Z");
    t.ok    (n.getDateISO (ti),       "'19980119T070000Z': getDateISO ()  -> true");
    t.is    (ti, 885193200,           "'19980119T070000Z': getDateISO ()  -> 885193200");
    t.ok    (n.depleted (),           "depleted");

    n = Nibbler ("20090213T233130Z");
    t.ok    (n.getDateISO (ti),       "'20090213T233130Z': getDateISO ()  -> true");
    t.is    (ti, 1234567890,          "'20090213T233130Z': getDateISO ()  -> 1234567890");
    t.ok    (n.depleted (),           "depleted");

#ifdef NIBBLER_FEATURE_DATE
    // bool getDate (time_t&, const std::string&);
    t.diag ("Nibbler::getDate");
    n = Nibbler ("1/1/2008");
    t.ok (n.getDate ("m/d/Y", ti), "m/d/Y ok");
    dt = Date (ti);
    t.is (dt.month (),   1, "ctor (std::string) -> m");
    t.is (dt.day (),     1, "ctor (std::string) -> d");
    t.is (dt.year (), 2008, "ctor (std::string) -> y");

    n = Nibbler ("20080101");
    t.ok (n.getDate ("YMD", ti), "YMD ok");
    dt = Date (ti);
    t.is (dt.month (),   1, "ctor (std::string) -> m");
    t.is (dt.day (),     1, "ctor (std::string) -> d");
    t.is (dt.year (), 2008, "ctor (std::string) -> y");

    n = Nibbler ("12/31/2007");
    t.ok (n.getDate ("m/d/Y", ti), "m/d/Y ok");
    dt = Date (ti);
    t.is (dt.month (),  12, "ctor (std::string) -> m");
    t.is (dt.day (),    31, "ctor (std::string) -> d");
    t.is (dt.year (), 2007, "ctor (std::string) -> y");

    n = Nibbler ("20071231");
    t.ok (n.getDate ("YMD", ti), "YMD ok");
    dt = Date (ti);
    t.is (dt.month (),  12, "ctor (std::string) -> m");
    t.is (dt.day (),    31, "ctor (std::string) -> d");
    t.is (dt.year (), 2007, "ctor (std::string) -> y");

    n = Nibbler ("Tue 01 Jan 2008 (01)");
    t.ok (n.getDate ("a D b Y (V)", ti), "a D b Y (V)");
    dt = Date (ti);
    t.is (dt.month (),   1, "ctor (std::string) -> m");
    t.is (dt.day (),     1, "ctor (std::string) -> d");
    t.is (dt.year (), 2008, "ctor (std::string) -> y");

    n = Nibbler ("Tuesday, January 1, 2008");
    t.ok (n.getDate ("A, B d, Y", ti), "A, B d, Y ok");
    dt = Date (ti);
    t.is (dt.month (),   1, "ctor (std::string) -> m");
    t.is (dt.day (),     1, "ctor (std::string) -> d");
    t.is (dt.year (), 2008, "ctor (std::string) -> y");

    n = Nibbler ("w01 Tue 2008-01-01");
    t.ok (n.getDate ("wV a Y-M-D", ti), "wV a Y-M-D ok");
    dt = Date (ti);
    t.is (dt.month (),   1, "ctor (std::string) -> m");
    t.is (dt.day (),     1, "ctor (std::string) -> d");
    t.is (dt.year (), 2008, "ctor (std::string) -> y");

    n = Nibbler ("6/7/2010 1:23:45");
    t.ok (n.getDate ("m/d/Y h:N:S", ti), "m/d/Y h:N:S ok");
    dt = Date (ti);
    t.is (dt.month (),     6, "ctor (std::string) -> m");
    t.is (dt.day (),       7, "ctor (std::string) -> d");
    t.is (dt.year (),   2010, "ctor (std::string) -> Y");
    t.is (dt.hour (),      1, "ctor (std::string) -> h");
    t.is (dt.minute (),   23, "ctor (std::string) -> N");
    t.is (dt.second (),   45, "ctor (std::string) -> S");

    n = Nibbler ("6/7/2010 01:23:45");
    t.ok (n.getDate ("m/d/Y H:N:S", ti), "m/d/Y H:N:S ok");
    dt = Date (ti);
    t.is (dt.month (),     6, "ctor (std::string) -> m");
    t.is (dt.day (),       7, "ctor (std::string) -> d");
    t.is (dt.year (),   2010, "ctor (std::string) -> Y");
    t.is (dt.hour (),      1, "ctor (std::string) -> h");
    t.is (dt.minute (),   23, "ctor (std::string) -> N");
    t.is (dt.second (),   45, "ctor (std::string) -> S");

    n = Nibbler ("6/7/2010 12:34:56");
    t.ok (n.getDate ("m/d/Y H:N:S", ti), "m/d/Y H:N:S ok");
    dt = Date (ti);
    t.is (dt.month (),     6, "ctor (std::string) -> m");
    t.is (dt.day (),       7, "ctor (std::string) -> d");
    t.is (dt.year (),   2010, "ctor (std::string) -> Y");
    t.is (dt.hour (),     12, "ctor (std::string) -> h");
    t.is (dt.minute (),   34, "ctor (std::string) -> N");
    t.is (dt.second (),   56, "ctor (std::string) -> S");

    n = Nibbler ("2010");
    t.ok (n.getDate ("Y", ti), "Y ok");
    dt = Date (ti);
    t.is (dt.month (),     1, "ctor (std::string) -> m");
    t.is (dt.day (),       1, "ctor (std::string) -> d");
    t.is (dt.year (),   2010, "ctor (std::string) -> Y");
    t.is (dt.hour (),      0, "ctor (std::string) -> h");
    t.is (dt.minute (),    0, "ctor (std::string) -> N");
    t.is (dt.second (),    0, "ctor (std::string) -> S");

    n = Nibbler ("17:18:19");
    t.ok (n.getDate ("H:N:S", ti), "H:N:S ok");
    dt = Date (ti);
    Date now = Date ();
    t.is (dt.month (), now.month(), "ctor (std::string) -> m");
    t.is (dt.day (),     now.day(), "ctor (std::string) -> d");
    t.is (dt.year (),   now.year(), "ctor (std::string) -> Y");
    t.is (dt.hour (),           17, "ctor (std::string) -> h");
    t.is (dt.minute (),         18, "ctor (std::string) -> N");
    t.is (dt.second (),         19, "ctor (std::string) -> S");
#endif

    // bool getOneOf (const std::vector <std::string>&, std::string&);
    t.diag ("Nibbler::getOneOf");
    options.push_back ("one");
    options.push_back ("two");
    options.push_back ("three");
    n = Nibbler ("onetwothreefour");
    t.ok    (n.getOneOf (options, s),         "'onetwothreefour':   getOneOf () -> true");
    t.is    (s, "one",                        "'onetwothreefour':   getOneOf () -> one");
    t.ok    (n.getOneOf (options, s),         "   'twothreefour':   getOneOf () -> true");
    t.is    (s, "two",                        "   'twothreefour':   getOneOf () -> two");
    t.ok    (n.getOneOf (options, s),         "      'threefour':   getOneOf () -> true");
    t.is    (s, "three",                      "      'threefour':   getOneOf () -> three");
    t.notok (n.getOneOf (options, s),         "           'four':   getOneOf () -> false");

    // bool getName (std::string&);
    t.diag ("Nibbler::getName");
    n = Nibbler ("a1 one one.two 9 foo_bar");
    t.ok    (n.getName (s),       "'a1 one one.two 9 foo_bar' getName -> ok");
    t.is    (s, "a1",             "  ' one one.two 9 foo_bar' getName -> 'a1'");
    t.ok    (n.skipWS (),         "   'one one.two 9 foo_bar' skipWS  -> ok");

    t.ok    (n.getName (s),       "   'one one.two 9 foo_bar' getName -> ok");
    t.is    (s, "one",            "      ' one.two 9 foo_bar' getName -> 'one'");
    t.ok    (n.skipWS (),         "       'one.two 9 foo_bar' skipWS  -> ok");

    t.ok    (n.getName (s),       "       'one.two 9 foo_bar' getName -> ok");
    t.is    (s, "one",            "          '.two 9 foo_bar' getName -> 'one'");
    t.ok    (n.skip ('.'),        "           'two 9 foo_bar' skip .  -> ok");

    t.ok    (n.getName (s),       "           'two 9 foo_bar' getName -> ok");
    t.is    (s, "two",            "              ' 9 foo_bar' getName -> 'two'");
    t.ok    (n.skipWS (),         "               '9 foo_bar' skipWS  -> ok");

    t.notok (n.getName (s),       "               '9 foo_bar' getName -> not ok");
    t.ok    (n.skip ('9'),        "                ' foo_bar' skip 9  -> ok");
    t.ok    (n.skipWS (),         "                 'foo_bar' skipWS  -> ok");

    t.ok    (n.getName (s),       "                 'foo_bar' getName -> ok");
    t.is    (s, "foo_bar",        "                        '' getName -> 'foo_bar'");
    t.ok    (n.depleted (),       "depleted");

    n = Nibbler ("entrée");
    t.ok (n.getName (s), "'entrée' -> ok");
    t.is (s, "entrée",   "'entrée' -> 'entrée'");

    // bool getWord (std::string&);
    t.diag ("Nibbler::getWord");
    n = Nibbler ("one two th3ee");
    t.ok (n.getWord (s),       "'one'              getWord -> ok");
    t.is (s, "one",            "'one'              getWord -> 'one'");
    t.ok (n.skipWS (),         "skipWS");
    t.ok (n.getWord (s),       "'two'              getWord -> ok");
    t.is (s, "two",            "'two'              getWord -> 'two'");
    t.ok (n.skipWS (),         "skipWS");
    t.ok (n.getWord (s),       "'th'               getWord -> ok");
    t.is (s, "th",             "'th'               getWord -> 'th'");
    t.ok (n.skip ('3'),        "skip(3)");
    t.ok (n.getWord (s),       "'ee'               getWord -> ok");
    t.is (s, "ee",             "'ee'               getWord -> 'ee'");
    t.ok (n.depleted (),       "depleted");

    t.diag ("Nibbler::getWord");
    n = Nibbler ("one TWO,three,f ");
    t.ok (n.getWord (s),              "'one TWO,three,f '   getWord  -> ok");
    t.is (s, "one",                   "   ' TWO,three,f '   getWord  -> one");
    t.ok (n.skipWS (),                "    'TWO,three,f '   skipWS   -> ok");

    t.ok (n.getWord (s),              "    'TWO,three,f '   getWord  -> ok");
    t.is (s, "TWO",                   "       ',three,f '   getWord  -> TWO");
    t.ok (n.skip (','),               "        'three,f '   skip ,   -> ok");

    t.ok (n.getWord (s),              "        'three,f '   getWord  -> ok");
    t.is (s, "three",                 "             ',f '   getWord  -> three");
    t.ok (n.skip (','),               "              'f '   skip ,   -> ok");

    t.ok (n.getWord (s),              "              'f '   getWord  -> ok");
    t.is (s, "f",                     "               ' '   getWord  -> f");
    t.ok (n.skipWS (),                "                ''   skip ,   -> ok");
    t.ok (n.depleted (),              "                ''   depleted -> true");

    // bool getN (int, std::string&);
    t.diag ("Nibbler::getN");
    n = Nibbler ("111223");
    t.ok (n.getN (3, s),              "  '111223' : getN (3)         -> true");
    t.is (s, "111",                   "  '111223' : getN (3)         -> '111'");
    t.ok (n.getN (2, s),              "     '223' : getN (2)         -> true");
    t.is (s, "22",                    "     '223' : getN (2)         -> '22'");
    t.ok (n.getN (1, s),              "       '3' : getN (1)         -> true");
    t.is (s, "3",                     "       '3' : getN (1)         -> '1'");
    t.ok    (n.depleted (),           "        '' : depleted ()      -> true");

    // bool getUntilEOL (std::string&);
    t.diag ("Nibbler::getUntilEOL");
    n = Nibbler ("one\ntwo");
    t.ok    (n.getUntilEOL (s),       "'one\\ntwo' :   getUntilEOL ()       -> true");
    t.is    (s, "one",                "'one\\ntwo' :   getUntilEOL ()       -> 'one'");
    t.ok    (n.skip ('\n'),           "   '\\ntwo' :          skip ('\\n')   -> true");
    t.ok    (n.getUntilEOL (s),       "     'two' :    getUntilEOL ()       -> true");
    t.is    (s, "two",                "     'two' :    getUntilEOL ()       -> 'two'");
    t.ok    (n.depleted (),           "        '' :       depleted ()       -> true");

    // bool getUntilEOS (std::string&);
    t.diag ("Nibbler::getUntilEOS");
    n = Nibbler ("one two");
    t.ok    (n.getUntilEOS (s),       " 'one two' :    getUntilEOS ()       -> 'one two'");
    t.ok    (n.depleted (),           "        '' :       depleted ()       -> true");

    // char next ();
    t.diag ("Nibbler::next");
    n = Nibbler ("hello");
    t.is    (n.next (), 'h',          "   'hello' :           next ()       -> 'h'");
    t.is    (n.next (), 'h',          "   'hello' :           next ()       -> 'h'");
    t.ok    (n.skipN (4),             "   'hello' :          skipN (4)      -> true");
    t.is    (n.next (), 'o',          "       'o' :           next ()       -> 'o'");
    t.ok    (n.skipN (1),             "       'o' :          skipN ()       -> true");
    t.ok    (n.depleted (),           "        '' :       depleted ()       -> true");

    // std::string next (const int quantity);
    t.diag ("Nibbler::next");
    n = Nibbler ("hello");
    t.is    (n.next (1), "h",         "   'hello' :          next (1)       -> 'h'");
    t.is    (n.next (1), "h",         "   'hello' :          next (1)       -> 'h'");
    t.is    (n.next (2), "he",        "   'hello' :          next (2)       -> 'he'");
    t.is    (n.next (3), "hel",       "   'hello' :          next (3)       -> 'hel'");
    t.is    (n.next (4), "hell",      "   'hello' :          next (4)       -> 'hell'");
    t.is    (n.next (5), "hello",     "   'hello' :          next (5)       -> 'hello'");
    t.is    (n.next (6), "",          "   'hello' :          next (6)       -> ''");

    // bool depleted ();
    t.diag ("Nibbler::depleted");
    n = Nibbler (" ");
    t.notok (n.depleted (),           "       ' ' :       depleted ()       -> false");
    t.ok    (n.skipN (),              "        '' :           skip ()       -> true");
    t.ok    (n.depleted (),           "        '' :       depleted ()       -> true");

    // void save ();
    // void restore ();
    n = Nibbler ("abcde");
    t.ok (n.skipN (),                 "   'abcde' :           skip ()       -> true");
    n.save ();
    t.ok (n.skipN (),                 "    'bcde' :           skip ()       -> true");
    t.ok (n.skipN (),                 "     'cde' :           skip ()       -> true");
    t.ok (n.skipN (),                 "      'de' :           skip ()       -> true");
    n.restore ();
    t.is (n.next (1), "b",            "    'bcde' :           skip ()       -> 'b'");
  }

  catch (const std::string& e) {t.diag (e);}

  return 0;
}
Beispiel #4
0
int main (int argc, char** argv)
{
  UnitTest t (14);

  try
  {
    // Basic parsing tests.
    std::string input = "{}";
    std::cout << "-- j1 -------------------\n"
              << "input: " << input << "\n";
    JSON j1 (input);
    j1.tree ()->dump ();

    input = "{\"name\":123}";
    std::cout << "-- j2 -------------------\n"
              << "input: " << input << "\n";
    JSON j2 (input);
    j2.tree ()->dump ();

    input = "{\"name\":123, \"array\":[1,2,3.4], \"map\":{\"m1\":\"v1\", \"m2\":\"v2\"}}";
    std::cout << "-- j3 -------------------\n"
              << "input: " << input << "\n";
    JSON j3 (input);
    j3.tree ()->dump ();

    // Sample ticket as a parsing test.
    input = "{\n"
            "\"ticket\": { \"type\":\"add\", \"client\":\"taskwarrior 2.x\"},\n"
            "\"auth\":   { \"user\":\"paul\", \"org\":\"gbf\", \"key\":\".........\",\n"
            "            \"locale\":\"en-US\" },\n"
            "\n"
            "\"add\":    { \"description\":\"Wash the dog\",\n"
            "            \"project\":\"home\",\n"
            "            \"due\":\"20101101T000000Z\" }\n"
            "}";
    std::cout << "-- j4 -------------------\n"
              << "input: " << input << "\n";
    JSON j4 (input);
    j4.tree ()->dump ();
    std::cout << "-------------------------\n";

    // Regular unit tests.
    t.is (JSON::encode ("1\b2"), "1\\b2",  "JSON::encode \\b -> \\\\b");
    t.is (JSON::decode ("1\\b2"), "1\b2",  "JSON::decode \\\\b -> \\b");

    t.is (JSON::encode ("1\n2"), "1\\n2",  "JSON::encode \\n -> \\\\n");
    t.is (JSON::decode ("1\\n2"), "1\n2",  "JSON::decode \\\\n -> \\n");

    t.is (JSON::encode ("1\r2"), "1\\r2",  "JSON::encode \\r -> \\\\r");
    t.is (JSON::decode ("1\\r2"), "1\r2",  "JSON::decode \\\\r -> \\r");

    t.is (JSON::encode ("1\t2"), "1\\t2",  "JSON::encode \\t -> \\\\t");
    t.is (JSON::decode ("1\\t2"), "1\t2",  "JSON::decode \\\\t -> \\t");

    t.is (JSON::encode ("1\\2"), "1\\\\2", "JSON::encode \\ -> \\\\");
    t.is (JSON::decode ("1\\\\2"), "1\\2", "JSON::decode \\\\ -> \\");

    t.is (JSON::encode ("1\x2"), "1\x2",   "JSON::encode \\x -> \\x (NOP)");
    t.is (JSON::decode ("1\x2"), "1\x2",   "JSON::decode \\x -> \\x (NOP)");

    t.is (JSON::encode ("1€2"), "1€2",     "JSON::encode € -> €");
    t.is (JSON::decode ("1\\u20ac2"),      "1€2", "JSON::decode \\u20ac -> €");

/*
  {
    "ticket":
    {
      "type":"synch",
      "client":"taskd-test-suite 1.0"
    },

    "synch":
    {
      "user":
      {
        "data":
        [
          {
            "uuid":"11111111-1111-1111-1111-111111111111",
            "status":"pending",
            "description":"This is a test",
            "entry":"20110111T124000Z"
          }
        ],
        "synch":"key"
      }
    },
  
    "auth":
    {
      "org":"gbf",
      "user":"******",
      "key":"K",
      "locale":"en-US"
    }
  }
*/
    input = "{\"ticket\":{\"type\":\"synch\",\"client\":\"taskd-test-suite 1.0\"},\"synch\":{\"user\":{\"data\":[{\"uuid\":\"11111111-1111-1111-1111-111111111111\",\"status\":\"pending\",\"description\":\"This is a test\",\"entry\":\"20110111T124000Z\"}],\"synch\":\"key\"}},\"auth\":{\"org\":\"gbf\",\"user\":\"Paul Beckingham\",\"key\":\"K\",\"locale\":\"en-US\"}}";
    std::cout << "-- j4 -------------------\n"
              << "input: " << input << "\n";
    JSON j5 (input);
    j5.tree ()->dump ();
  }

  catch (std::string& e) {t.diag (e);}

  return 0;
}
Beispiel #5
0
int main (int argc, char** argv)
{
/*
  UnitTest t (15);
*/
  UnitTest t (9);

  try
  {
    // Remove any residual test files.
    rmdir ("./extensions");
    unlink ("./pending.data");
    unlink ("./completed.data");
    unlink ("./undo.data");
/*
    unlink ("./backlog.data");
*/
    unlink ("./synch.key");

    // Set the context to allow GC.
    context.config.set ("gc", "on");
    context.config.set ("debug", "on");

    context.tdb2.set_location (".");

    // Try reading an empty database.
    std::vector <Task> pending          = context.tdb2.pending.get_tasks ();
    std::vector <Task> completed        = context.tdb2.completed.get_tasks ();
    std::vector <std::string> undo      = context.tdb2.undo.get_lines ();
/*
    std::vector <Task> backlog          = context.tdb2.backlog.get_tasks ();
    std::vector <std::string> synch_key = context.tdb2.synch_key.get_lines ();
*/

    t.is ((int) pending.size (),   0, "TDB2 Read empty pending");
    t.is ((int) completed.size (), 0, "TDB2 Read empty completed");
    t.is ((int) undo.size (),      0, "TDB2 Read empty undo");
/*
    t.is ((int) backlog.size (),   0, "TDB2 Read empty backlog");
    t.is ((int) synch_key.size (), 0, "TDB2 Read empty synch.key");
*/

    // Add a task.
    Task task ("[description:\"description\" name:\"value\"]");
    context.tdb2.add (task);

    pending   = context.tdb2.pending.get_tasks ();
    completed = context.tdb2.completed.get_tasks ();
    undo      = context.tdb2.undo.get_lines ();
/*
    backlog   = context.tdb2.backlog.get_tasks ();
    synch_key = context.tdb2.synch_key.get_lines ();
*/

    t.is ((int) pending.size (),   1, "TDB2 after add, 1 pending task");
    t.is ((int) completed.size (), 0, "TDB2 after add, 0 completed tasks");
    t.is ((int) undo.size (),      3, "TDB2 after add, 3 undo lines");
/*
    t.is ((int) backlog.size (),   1, "TDB2 after add, 1 backlog task");
    t.is ((int) synch_key.size (), 0, "TDB2 after add, 0 synch.key");
*/

    task.set ("description", "This is a test");
    context.tdb2.modify (task);

    pending   = context.tdb2.pending.get_tasks ();
    completed = context.tdb2.completed.get_tasks ();
    undo      = context.tdb2.undo.get_lines ();
/*
    backlog   = context.tdb2.backlog.get_tasks ();
    synch_key = context.tdb2.synch_key.get_lines ();
*/

    t.is ((int) pending.size (),   1, "TDB2 after add, 1 pending task");
    t.is ((int) completed.size (), 0, "TDB2 after add, 0 completed tasks");
    t.is ((int) undo.size (),      7, "TDB2 after add, 7 undo lines");
/*
    t.is ((int) backlog.size (),   2, "TDB2 after add, 2 backlog task");
    t.is ((int) synch_key.size (), 0, "TDB2 after add, 0 synch.key");
*/

    context.tdb2.commit ();

    // Reset for reuse.
    context.tdb2.clear ();
    context.tdb2.set_location (".");

    // TODO commit
    // TODO complete a task
    // TODO gc
  }

  catch (std::string& error)
  {
    t.diag (error);
    return -1;
  }

  catch (...)
  {
    t.diag ("Unknown error.");
    return -2;
  }

  rmdir ("./extensions");
  unlink ("./pending.data");
  unlink ("./completed.data");
  unlink ("./undo.data");
  unlink ("./backlog.data");
  unlink ("./synch.key");

  return 0;
}
Beispiel #6
0
int main (int argc, char** argv)
{
  UnitTest t (21);

  // (blank)
  bool good = true;
  Record record;

  try {record = Record ("");}
  catch (std::string& e){t.diag (e); good = false;}
  t.notok (good, "Record::Record ('')");

  // []
  good = true;
  try {record = Record ("[]");}
  catch (std::string& e){t.diag (e); good = false;}
  t.notok (good, "Record::Record ('[]')");

  // [name:value]
  good = true;
  try {record = Record ("[name:value]");}
  catch (std::string& e){t.diag (e); good = false;}
  t.ok (good, "Record::Record ('[name:value]')");
  t.is (record.get ("name"), "value", "name=value");

  // [name:"value"]
  good = true;
  try {record = Record ("[name:\"value\"]");}
  catch (std::string& e){t.diag (e); good = false;}
  t.ok (good, "Record::Record ('[name:\"value\"]')");
  t.is (record.get ("name"), "value", "name=value");

  // [name:"one two"]
  good = true;
  try {record = Record ("[name:\"one two\"]");}
  catch (std::string& e){t.diag (e); good = false;}
  t.ok (good, "Record::Record ('[name:\"one two\"]')");
  t.is (record.get ("name"), "one two", "name=one two");

  // [one:two three:four]
  good = true;
  try {record = Record ("[one:\"two\" three:\"four\"]");}
  catch (std::string& e){t.diag (e); good = false;}
  t.ok (good, "Record::Record ('[one:\"two\" three:\"four\"]')");
  t.is (record.get ("one"), "two", "one=two");
  t.is (record.get ("three"), "four", "three=four");

  // Record::set
  record.clear ();
  record.set ("name", "value");
  t.is (record.composeF4 (), "[name:\"value\"]\n", "Record::set");

  // Record::has
  t.ok    (record.has ("name"), "Record::has");
  t.notok (record.has ("woof"), "Record::has not");

  // Record::get_int
  record.set ("one", 1);
  t.is (record.composeF4 (), "[name:\"value\" one:\"1\"]\n", "Record::set");
  t.is (record.get_int ("one"), 1, "Record::get_int");

  // Record::get_ulong
  record.set ("two", "4294967295");
  t.is (record.composeF4 (), "[name:\"value\" one:\"1\" two:\"4294967295\"]\n", "Record::set");
  t.is ((size_t)record.get_ulong ("two"), (size_t)4294967295, "Record::get_ulong");

  // Record::remove
  record.remove ("one");
  record.remove ("two");
  t.is (record.composeF4 (), "[name:\"value\"]\n", "Record::remove");

  // Record::all
  std::vector <Att> all = record.all ();
  t.is (all.size (), (size_t)1, "Record::all size");
  t.is (all[0].name (), "name", "Record::all[0].name ()");

  return 0;
}
Beispiel #7
0
int main (int argc, char** argv)
{
  UnitTest t (NUM_POSITIVE_TESTS + NUM_NEGATIVE_TESTS + 22);

  // Positive tests.
  for (int i = 0; i < NUM_POSITIVE_TESTS; ++i)
  {
    try
    {
      json::value* root = json::parse (positive_tests[i]);
      t.ok (root, std::string ("positive: ") + positive_tests[i]);
      if (root)
      {
        t.diag (root->dump ());
        delete root;
      }
    }

    catch (const std::string& e) { t.diag (e); }
    catch (...)                  { t.diag ("Unknown error"); }
  }

  // Negative tests.
  for (int i = 0; i < NUM_NEGATIVE_TESTS; ++i)
  {
    try
    {
      json::value* root = json::parse (negative_tests[i]);
      t.is ((const char*) root, (const char*) NULL,
            std::string ("negative: ") + negative_tests[i]);
    }

    catch (const std::string& e) { t.pass (e); }
    catch (...)                  { t.fail ("Unknown error"); }
  }

  // Other tests.
  try
  {
    // Regular unit tests.
    t.is (json::encode ("1\b2"), "1\\b2",    "json::encode \\b -> \\\\b");
    t.is (json::decode ("1\\b2"), "1\b2",    "json::decode \\\\b -> \\b");

    t.is (json::encode ("1\n2"), "1\\n2",    "json::encode \\n -> \\\\n");
    t.is (json::decode ("1\\n2"), "1\n2",    "json::decode \\\\n -> \\n");

    t.is (json::encode ("1\r2"), "1\\r2",    "json::encode \\r -> \\\\r");
    t.is (json::decode ("1\\r2"), "1\r2",    "json::decode \\\\r -> \\r");

    t.is (json::encode ("1\t2"), "1\\t2",    "json::encode \\t -> \\\\t");
    t.is (json::decode ("1\\t2"), "1\t2",    "json::decode \\\\t -> \\t");

    t.is (json::encode ("1\\2"), "1\\\\2",   "json::encode \\ -> \\\\");
    t.is (json::decode ("1\\\\2"), "1\\2",   "json::decode \\\\ -> \\");

    t.is (json::encode ("1\x2"), "1\x2",     "json::encode \\x -> \\x (NOP)");
    t.is (json::decode ("1\x2"), "1\x2",     "json::decode \\x -> \\x (NOP)");

    t.is (json::encode ("1€2"), "1€2",       "json::encode € -> €");
    t.is (json::decode ("1\\u20ac2"), "1€2", "json::decode \\u20ac -> €");

    std::string encoded = json::encode ("one\\");
    t.is (encoded, "one\\\\",                "json::encode one\\\\ -> one\\\\\\\\");
    t.is ((int)encoded.length (), 5,         "json::encode one\\\\ -> length 5");
    t.is (encoded[0], 'o',                   "json::encode one\\\\[0] -> o");
    t.is (encoded[1], 'n',                   "json::encode one\\\\[1] -> n");
    t.is (encoded[2], 'e',                   "json::encode one\\\\[2] -> e");
    t.is (encoded[3], '\\',                  "json::encode one\\\\[3] -> \\");
    t.is (encoded[4], '\\',                  "json::encode one\\\\[4] -> \\");

    t.is (json::decode (encoded), "one\\",   "json::decode one\\\\\\\\ -> one\\\\");
  }

  catch (std::string& e) {t.diag (e);}

  return 0;
}
Beispiel #8
0
int main (int argc, char** argv)
{
  UnitTest t (211);

  // Ensure environment has no influence.
  unsetenv ("TASKDATA");
  unsetenv ("TASKRC");

  try
  {
    Date now;
    Date yesterday;
    yesterday -= 86400;
    Date tomorrow;
    tomorrow += 86400;

    t.ok    (yesterday <= now,       "yesterday <= now");
    t.ok    (yesterday <  now,       "yesterday < now");
    t.notok (yesterday == now,       "!(yesterday == now)");
    t.ok    (yesterday != now,       "yesterday != now");
    t.ok    (now       >= yesterday, "now >= yesterday");
    t.ok    (now       >  yesterday, "now > yesterday");

    t.ok    (tomorrow >= now,        "tomorrow >= now");
    t.ok    (tomorrow >  now,        "tomorrow > now");
    t.notok (tomorrow == now,        "!(tomorrow == now)");
    t.ok    (tomorrow != now,        "tomorrow != now");
    t.ok    (now      <= tomorrow,   "now <= tomorrow");
    t.ok    (now      <  tomorrow,   "now < tomorrow");

    // Date::Date ("now")
    context.config.set ("weekstart", "monday");
    Date relative_now ("now");
    t.ok (relative_now.sameHour (now),  "Date ().sameHour (Date (now))");
    t.ok (relative_now.sameDay (now),   "Date ().sameDay (Date (now))");
    t.ok (relative_now.sameWeek (now),  "Date ().sameWeek (Date (now))");
    t.ok (relative_now.sameMonth (now), "Date ().sameMonth (Date (now))");
    t.ok (relative_now.sameYear (now),  "Date ().sameYear (Date (now))");

    // Loose comparisons.
    Date left ("7/4/2008");
    Date comp1 ("7/4/2008");
    t.ok (left.sameDay   (comp1), "7/4/2008 is on the same day as 7/4/2008");
    t.ok (left.sameWeek  (comp1), "7/4/2008 is on the same week as 7/4/2008");
    t.ok (left.sameMonth (comp1), "7/4/2008 is in the same month as 7/4/2008");
    t.ok (left.sameYear  (comp1), "7/4/2008 is in the same year as 7/4/2008");

    Date comp2 ("7/5/2008");
    t.notok (left.sameDay   (comp2), "7/4/2008 is not on the same day as 7/5/2008");
    t.ok    (left.sameMonth (comp2), "7/4/2008 is in the same month as 7/5/2008");
    t.ok    (left.sameYear  (comp2), "7/4/2008 is in the same year as 7/5/2008");

    Date comp3 ("8/4/2008");
    t.notok (left.sameDay   (comp3), "7/4/2008 is not on the same day as 8/4/2008");
    t.notok (left.sameWeek  (comp3), "7/4/2008 is not on the same week as 8/4/2008");
    t.notok (left.sameMonth (comp3), "7/4/2008 is not in the same month as 8/4/2008");
    t.ok    (left.sameYear  (comp3), "7/4/2008 is in the same year as 8/4/2008");

    Date comp4 ("7/4/2009");
    t.notok (left.sameDay   (comp4), "7/4/2008 is not on the same day as 7/4/2009");
    t.notok (left.sameWeek  (comp3), "7/4/2008 is not on the same week as 7/4/2009");
    t.notok (left.sameMonth (comp4), "7/4/2008 is not in the same month as 7/4/2009");
    t.notok (left.sameYear  (comp4), "7/4/2008 is not in the same year as 7/4/2009");

    // Validity.
    t.ok    (Date::valid (2, 29, 2008), "valid: 2/29/2008");
    t.notok (Date::valid (2, 29, 2007), "invalid: 2/29/2007");

    t.ok    (Date::valid ("2/29/2008"), "valid: 2/29/2008");
    t.notok (Date::valid ("2/29/2007"), "invalid: 2/29/2007");

    t.ok    (Date::valid (366, 2008), "valid: 366 days in 2008");
    t.notok (Date::valid (366, 2007), "invalid: 366 days in 2007");

    // Time validity.
    t.ok    (Date::valid (2, 28, 2010,  0,  0,  0), "valid 2/28/2010 0:00:00");
    t.ok    (Date::valid (2, 28, 2010, 23, 59, 59), "valid 2/28/2010 23:59:59");
    t.notok (Date::valid (2, 28, 2010, 24, 59, 59), "valid 2/28/2010 24:59:59");
    t.notok (Date::valid (2, 28, 2010, -1,  0,  0), "valid 2/28/2010 -1:00:00");

    // Leap year.
    t.ok    (Date::leapYear (2008), "2008 is a leap year");
    t.notok (Date::leapYear (2007), "2007 is not a leap year");
    t.ok    (Date::leapYear (2000), "2000 is a leap year");
    t.notok (Date::leapYear (1900), "1900 is not a leap year");

    // Days in month.
    t.is (Date::daysInMonth (2, 2008), 29, "29 days in February 2008");
    t.is (Date::daysInMonth (2, 2007), 28, "28 days in February 2007");

    // Names.
    t.is (Date::monthName (1), "January",   "1 = January");
    t.is (Date::monthName (2), "February",  "2 = February");
    t.is (Date::monthName (3), "March",     "3 = March");
    t.is (Date::monthName (4), "April",     "4 = April");
    t.is (Date::monthName (5), "May",       "5 = May");
    t.is (Date::monthName (6), "June",      "6 = June");
    t.is (Date::monthName (7), "July",      "7 = July");
    t.is (Date::monthName (8), "August",    "8 = August");
    t.is (Date::monthName (9), "September", "9 = September");
    t.is (Date::monthName (10), "October",  "10 = October");
    t.is (Date::monthName (11), "November", "11 = November");
    t.is (Date::monthName (12), "December", "12 = December");

    t.is (Date::dayName (0), "Sunday",    "0 == Sunday");
    t.is (Date::dayName (1), "Monday",    "1 == Monday");
    t.is (Date::dayName (2), "Tuesday",   "2 == Tuesday");
    t.is (Date::dayName (3), "Wednesday", "3 == Wednesday");
    t.is (Date::dayName (4), "Thursday",  "4 == Thursday");
    t.is (Date::dayName (5), "Friday",    "5 == Friday");
    t.is (Date::dayName (6), "Saturday",  "6 == Saturday");

    t.is (Date::dayOfWeek ("SUNDAY"),    0, "SUNDAY == 0");
    t.is (Date::dayOfWeek ("sunday"),    0, "sunday == 0");
    t.is (Date::dayOfWeek ("Sunday"),    0, "Sunday == 0");
    t.is (Date::dayOfWeek ("Monday"),    1, "Monday == 1");
    t.is (Date::dayOfWeek ("Tuesday"),   2, "Tuesday == 2");
    t.is (Date::dayOfWeek ("Wednesday"), 3, "Wednesday == 3");
    t.is (Date::dayOfWeek ("Thursday"),  4, "Thursday == 4");
    t.is (Date::dayOfWeek ("Friday"),    5, "Friday == 5");
    t.is (Date::dayOfWeek ("Saturday"),  6, "Saturday == 6");

    Date happyNewYear (1, 1, 2008);
    t.is (happyNewYear.dayOfWeek (), 2, "1/1/2008 == Tuesday");
    t.is (happyNewYear.month (),     1, "1/1/2008 == January");
    t.is (happyNewYear.day (),       1, "1/1/2008 == 1");
    t.is (happyNewYear.year (),   2008, "1/1/2008 == 2008");

    t.is (happyNewYear.toString (), "1/1/2008", "toString 1/1/2008");

    int m, d, y;
    happyNewYear.toMDY (m, d, y);
    t.is (m, 1, "1/1/2008 == January");
    t.is (d, 1, "1/1/2008 == 1");
    t.is (y, 2008, "1/1/2008 == 2008");

    Date epoch (9, 8, 2001);
    t.ok ((int)epoch.toEpoch () < 1000000000, "9/8/2001 < 1,000,000,000");
    epoch += 172800;
    t.ok ((int)epoch.toEpoch () > 1000000000, "9/10/2001 > 1,000,000,000");

    Date fromEpoch (epoch.toEpoch ());
    t.is (fromEpoch.toString (), epoch.toString (), "ctor (time_t)");

    Date iso (1000000000);
    t.is (iso.toISO (), "20010909T014640Z", "1,000,000,000 -> 20010909T014640Z");

    // Quantization.
    Date quant (1234526400);
    t.is (quant.startOfDay ().toString ("YMDHNS"),   "20090213000000", "1234526400 -> 2/13/2009 12:00:00 UTC -> 2/13/2009 0:00:00");
    t.is (quant.startOfWeek ().toString ("YMDHNS"),  "20090208000000", "1234526400 -> 2/13/2009 12:00:00 UTC -> 2/8/2009 0:00:00");
    t.is (quant.startOfMonth ().toString ("YMDHNS"), "20090201000000", "1234526400 -> 2/13/2009 12:00:00 UTC -> 2/1/2009 0:00:00");
    t.is (quant.startOfYear ().toString ("YMDHNS"),  "20090101000000", "1234526400 -> 2/13/2009 12:00:00 UTC -> 1/1/2009 0:00:00");

    // Date parsing.
    Date fromString1 ("1/1/2008");
    t.is (fromString1.month (),   1, "ctor (std::string) -> m");
    t.is (fromString1.day (),     1, "ctor (std::string) -> d");
    t.is (fromString1.year (), 2008, "ctor (std::string) -> y");

    Date fromString2 ("1/1/2008", "m/d/Y");
    t.is (fromString2.month (),   1, "ctor (std::string) -> m");
    t.is (fromString2.day (),     1, "ctor (std::string) -> d");
    t.is (fromString2.year (), 2008, "ctor (std::string) -> y");

    Date fromString3 ("20080101", "YMD");
    t.is (fromString3.month (),   1, "ctor (std::string) -> m");
    t.is (fromString3.day (),     1, "ctor (std::string) -> d");
    t.is (fromString3.year (), 2008, "ctor (std::string) -> y");

    Date fromString4 ("12/31/2007");
    t.is (fromString4.month (),  12, "ctor (std::string) -> m");
    t.is (fromString4.day (),    31, "ctor (std::string) -> d");
    t.is (fromString4.year (), 2007, "ctor (std::string) -> y");

    Date fromString5 ("12/31/2007", "m/d/Y");
    t.is (fromString5.month (),  12, "ctor (std::string) -> m");
    t.is (fromString5.day (),    31, "ctor (std::string) -> d");
    t.is (fromString5.year (), 2007, "ctor (std::string) -> y");

    Date fromString6 ("20071231", "YMD");
    t.is (fromString6.month (),  12, "ctor (std::string) -> m");
    t.is (fromString6.day (),    31, "ctor (std::string) -> d");
    t.is (fromString6.year (), 2007, "ctor (std::string) -> y");

    Date fromString7 ("01/01/2008", "m/d/Y");
    t.is (fromString7.month (),   1, "ctor (std::string) -> m");
    t.is (fromString7.day (),     1, "ctor (std::string) -> d");
    t.is (fromString7.year (), 2008, "ctor (std::string) -> y");

    Date fromString8 ("Tue 05 Feb 2008 (06)", "a D b Y (V)");
    t.is (fromString8.month (),   2, "ctor (std::string) -> m");
    t.is (fromString8.day (),     5, "ctor (std::string) -> d");
    t.is (fromString8.year (), 2008, "ctor (std::string) -> y");

    Date fromString9 ("Tuesday, February 5, 2008", "A, B d, Y");
    t.is (fromString9.month (),   2, "ctor (std::string) -> m");
    t.is (fromString9.day (),     5, "ctor (std::string) -> d");
    t.is (fromString9.year (), 2008, "ctor (std::string) -> y");

    Date fromString10 ("w01 Tue 2008-01-01", "wV a Y-M-D");
    t.is (fromString10.month (),   1, "ctor (std::string) -> m");
    t.is (fromString10.day (),     1, "ctor (std::string) -> d");
    t.is (fromString10.year (), 2008, "ctor (std::string) -> y");

    Date fromString11 ("6/7/2010 1:23:45",  "m/d/Y h:N:S");
    t.is (fromString11.month (),     6, "ctor (std::string) -> m");
    t.is (fromString11.day (),       7, "ctor (std::string) -> d");
    t.is (fromString11.year (),   2010, "ctor (std::string) -> Y");
    t.is (fromString11.hour (),      1, "ctor (std::string) -> h");
    t.is (fromString11.minute (),   23, "ctor (std::string) -> N");
    t.is (fromString11.second (),   45, "ctor (std::string) -> S");

    Date fromString12 ("6/7/2010 01:23:45", "m/d/Y H:N:S");
    t.is (fromString12.month (),     6, "ctor (std::string) -> m");
    t.is (fromString12.day (),       7, "ctor (std::string) -> d");
    t.is (fromString12.year (),   2010, "ctor (std::string) -> Y");
    t.is (fromString12.hour (),      1, "ctor (std::string) -> h");
    t.is (fromString12.minute (),   23, "ctor (std::string) -> N");
    t.is (fromString12.second (),   45, "ctor (std::string) -> S");

    Date fromString13 ("6/7/2010 12:34:56", "m/d/Y H:N:S");
    t.is (fromString13.month (),     6, "ctor (std::string) -> m");
    t.is (fromString13.day (),       7, "ctor (std::string) -> d");
    t.is (fromString13.year (),   2010, "ctor (std::string) -> Y");
    t.is (fromString13.hour (),     12, "ctor (std::string) -> h");
    t.is (fromString13.minute (),   34, "ctor (std::string) -> N");
    t.is (fromString13.second (),   56, "ctor (std::string) -> S");

    // Day of year
    t.is (Date ("1/1/2011",   "m/d/Y").dayOfYear (),   1, "dayOfYear (1/1/2011)   ->   1");
    t.is (Date ("5/1/2011",   "m/d/Y").dayOfYear (), 121, "dayOfYear (5/1/2011)   -> 121");
    t.is (Date ("12/31/2011", "m/d/Y").dayOfYear (), 365, "dayOfYear (12/31/2011) -> 365");

    // Easter
    Date e1 (Date::easter(1980));
    t.is (e1.toString (), "4/6/1980", "Easter 4/6/1980");
    Date e2 (Date::easter(1995));
    t.is (e2.toString (), "4/16/1995", "Easter 4/16/1995");
    Date e3 (Date::easter(2000));
    t.is (e3.toString (), "4/23/2000", "Easter 4/23/2000");
    Date e4 (Date::easter(2009));
    t.is (e4.toString (), "4/12/2009", "Easter 4/12/2009");
    Date e5 (Date::easter(2010));
    t.is (e5.toString (), "4/4/2010", "Easter 4/4/2010");
    Date e6 (Date::easter(2011));
    t.is (e6.toString (), "4/24/2011", "Easter 4/24/2011");
    Date e7 (Date::easter(2012));
    t.is (e7.toString (), "4/8/2012", "Easter 4/8/2012");
    Date e8 (Date::easter(2020));
    t.is (e8.toString (), "4/12/2020", "Easter 4/12/2020");

    // Relative dates.
    Date r1 ("today");
    t.ok (r1.sameDay (now), "today = now");

    Date r4 ("sunday");
    if (now.dayOfWeek () >= 0)
      t.ok (r4.sameDay (now + (0 - now.dayOfWeek () + 7) * 86400), "next sunday");
    else
      t.ok (r4.sameDay (now + (0 - now.dayOfWeek ()) * 86400), "next sunday");;

    Date r5 ("monday");
    if (now.dayOfWeek () >= 1)
      t.ok (r5.sameDay (now + (1 - now.dayOfWeek () + 7) * 86400), "next monday");
    else
      t.ok (r5.sameDay (now + (1 - now.dayOfWeek ()) * 86400), "next monday");;

    Date r6 ("tuesday");
    if (now.dayOfWeek () >= 2)
      t.ok (r6.sameDay (now + (2 - now.dayOfWeek () + 7) * 86400), "next tuesday");
    else
      t.ok (r6.sameDay (now + (2 - now.dayOfWeek ()) * 86400), "next tuesday");;

    Date r7 ("wednesday");
    if (now.dayOfWeek () >= 3)
      t.ok (r7.sameDay (now + (3 - now.dayOfWeek () + 7) * 86400), "next wednesday");
    else
      t.ok (r7.sameDay (now + (3 - now.dayOfWeek ()) * 86400), "next wednesday");;

    Date r8 ("thursday");
    if (now.dayOfWeek () >= 4)
      t.ok (r8.sameDay (now + (4 - now.dayOfWeek () + 7) * 86400), "next thursday");
    else
      t.ok (r8.sameDay (now + (4 - now.dayOfWeek ()) * 86400), "next thursday");;

    Date r9 ("friday");
    if (now.dayOfWeek () >= 5)
      t.ok (r9.sameDay (now + (5 - now.dayOfWeek () + 7) * 86400), "next friday");
    else
      t.ok (r9.sameDay (now + (5 - now.dayOfWeek ()) * 86400), "next friday");;

    Date r10 ("saturday");
    if (now.dayOfWeek () >= 6)
      t.ok (r10.sameDay (now + (6 - now.dayOfWeek () + 7) * 86400), "next saturday");
    else
      t.ok (r10.sameDay (now + (6 - now.dayOfWeek ()) * 86400), "next saturday");;

    Date r11 ("eow");
    t.ok (r11 < now + (8 * 86400), "eow < 7 days away");

    Date r12 ("eocw");
    t.ok (r12 > now - (8 * 86400), "eocw < 7 days in the past");

    Date r13 ("eom");
    t.ok (r13.sameMonth (now), "eom in same month as now");

    Date r14 ("eocm");
    t.ok (r14.sameMonth (now), "eocm in same month as now");

    Date r15 ("eoy");
    t.ok (r15.sameYear (now), "eoy in same year as now");

    Date r16 ("sow");
    t.ok (r16 < now + (8 * 86400), "sow < 7 days away");

    Date r23 ("socw");
    t.ok (r23 > now - (8 * 86400), "sow < 7 days in the past");

    Date r17 ("som");
    t.notok (r17.sameMonth (now), "som not in same month as now");

    Date r18 ("socm");
    t.ok (r18.sameMonth (now), "socm in same month as now");

    Date r19 ("soy");
    t.notok (r19.sameYear (now), "soy not in same year as now");

    Date first ("1st");
    t.notok (first.sameMonth (now), "1st not in same month as now");
    t.is (first.day (),   1, "1st day is 1");

    Date later ("later");
    t.is (later.month (),   1, "later -> m = 1");
    t.is (later.day (),    18, "later -> d = 18");
    t.is (later.year (), 2038, "later -> y = 2038");

    // Quarters
    Date soq ("soq");
    Date eoq ("eoq");
    t.is (soq.day (),  1,      "soq is the first day of a month");
    t.is (eoq.day () / 10,  3, "eoq is the 30th or 31th of a month");
    t.is (soq.month () % 3, 1, "soq month is 1, 4, 7 or 10");
    t.is (eoq.month () % 3, 0, "eoq month is 3, 6, 9 or 12");

    // Note: these fail during the night of daylight savings end.
    t.ok (soq.sameYear (now) ||
          (now.month () >= 10 &&
           soq.year () == now.year () + 1), "soq is in same year as now");
    t.ok (eoq.sameYear (now),  "eoq is in same year as now");

    // Date::sameHour
    Date r20 ("6/7/2010 01:00:00", "m/d/Y H:N:S");
    Date r21 ("6/7/2010 01:59:59", "m/d/Y H:N:S");
    t.ok (r20.sameHour (r21), "two dates within the same hour");

    Date r22 ("6/7/2010 00:59:59", "m/d/Y H:N:S");
    t.notok (r20.sameHour (r22), "two dates not within the same hour");

    // Date::operator-
    Date r25 (1234567890);
    t.is ((r25 - 1).toEpoch (), 1234567889, "1234567890 - 1 = 1234567889");

    // Date::operator--
    Date r26 (11, 7, 2010, 23, 59, 59);
    r26--;
    t.is (r26.toString ("YMDHNS"), "20101106235959", "decrement across fall DST boundary");

    Date r27 (3, 14, 2010, 23, 59, 59);
    r27--;
    t.is (r27.toString ("YMDHNS"), "20100313235959", "decrement across spring DST boundary");

    // Date::operator++
    Date r28 (11, 6, 2010, 23, 59, 59);
    r28++;
    t.is (r28.toString ("YMDHNS"), "20101107235959", "increment across fall DST boundary");

    Date r29 (3, 13, 2010, 23, 59, 59);
    r29++;
    t.is (r29.toString ("YMDHNS"), "20100314235959", "increment across spring DST boundary");

    // int Date::length (const std::string&);
    t.is (Date::length ("m"), 2,  "length 'm' --> 2");
    t.is (Date::length ("M"), 2,  "length 'M' --> 2");
    t.is (Date::length ("d"), 2,  "length 'd' --> 2");
    t.is (Date::length ("D"), 2,  "length 'D' --> 2");
    t.is (Date::length ("y"), 2,  "length 'y' --> 2");
    t.is (Date::length ("Y"), 4,  "length 'Y' --> 4");
    t.is (Date::length ("a"), 3,  "length 'a' --> 3");
    t.is (Date::length ("A"), 10, "length 'A' --> 10");
    t.is (Date::length ("b"), 3,  "length 'b' --> 3");
    t.is (Date::length ("B"), 10, "length 'B' --> 10");
    t.is (Date::length ("v"), 2,  "length 'v' --> 2");
    t.is (Date::length ("V"), 2,  "length 'V' --> 2");
    t.is (Date::length ("h"), 2,  "length 'h' --> 2");
    t.is (Date::length ("H"), 2,  "length 'H' --> 2");
    t.is (Date::length ("n"), 2,  "length 'n' --> 2");
    t.is (Date::length ("N"), 2,  "length 'N' --> 2");
    t.is (Date::length ("s"), 2,  "length 's' --> 2");
    t.is (Date::length ("S"), 2,  "length 'S' --> 2");
    t.is (Date::length ("j"), 3,  "length 'j' --> 3");
    t.is (Date::length ("J"), 3,  "length 'J' --> 3");

    t.is (Date::length (" "), 1, "length ' ' --> 1");

    // Depletion requirement.
    Date r30 ("Mon Jun 30 2014", "a b D Y", false, false);
    t.is (r30.toString ("YMDHNS"), "20140630000000", "Depletion required on complex format with spaces");

    std::string::size_type i = 0;
    Date r31 ("Mon Jun 30 2014 xxx", i, "a b D Y", false, false);
    t.is (r31.toString ("YMDHNS"), "20140630000000", "Depletion not required on complex format with spaces");
    t.is ((int)i, 15,                                "Depletion not required on complex format with spaces, 15 chars");
  }

  catch (const std::string& e)
  {
    t.fail ("Exception thrown.");
    t.diag (e);
  }

  return 0;
}
Beispiel #9
0
int main (int, char**)
{
  UnitTest t (966);

  ISO8601d iso;
  std::string::size_type start = 0;
  t.notok (iso.parse ("foo", start), "foo --> false");
  t.is ((int)start, 0,               "foo[0]");

  // Determine local and UTC time.
  time_t now = time (NULL);
  struct tm* local_now = localtime (&now);
  int local_s = (local_now->tm_hour * 3600) +
                (local_now->tm_min  * 60)   +
                local_now->tm_sec;
  local_now->tm_hour  = 0;
  local_now->tm_min   = 0;
  local_now->tm_sec   = 0;
  local_now->tm_isdst = -1;
  time_t local = mktime (local_now);
  std::cout << "# local midnight today " << local << "\n";

  local_now->tm_year  = 2013 - 1900;
  local_now->tm_mon   = 12 - 1;
  local_now->tm_mday  = 6;
  local_now->tm_isdst = 0;
  time_t local6 = mktime (local_now);
  std::cout << "# local midnight 2013-12-06 " << local6 << "\n";

  local_now->tm_year  = 2013 - 1900;
  local_now->tm_mon   = 12 - 1;
  local_now->tm_mday  = 1;
  local_now->tm_isdst = 0;
  time_t local1 = mktime (local_now);
  std::cout << "# local midnight 2013-12-01 " << local1 << "\n";

  struct tm* utc_now = gmtime (&now);
  int utc_s = (utc_now->tm_hour * 3600) +
              (utc_now->tm_min  * 60)   +
              utc_now->tm_sec;
  utc_now->tm_hour  = 0;
  utc_now->tm_min   = 0;
  utc_now->tm_sec   = 0;
  utc_now->tm_isdst = -1;
  time_t utc = timegm (utc_now);
  std::cout << "# utc midnight today " << utc << "\n";

  utc_now->tm_year  = 2013 - 1900;
  utc_now->tm_mon   = 12 - 1;
  utc_now->tm_mday  = 6;
  utc_now->tm_isdst = 0;
  time_t utc6 = timegm (utc_now);
  std::cout << "# utc midnight 2013-12-06 " << utc6 << "\n";

  utc_now->tm_year  = 2013 - 1900;
  utc_now->tm_mon   = 12 - 1;
  utc_now->tm_mday  = 1;
  utc_now->tm_isdst = 0;
  time_t utc1 = timegm (utc_now);
  std::cout << "# utc midnight 2013-12-01 " << utc1 << "\n";

  int hms = (12 * 3600) + (34 * 60) + 56; // The time 12:34:56 in seconds.
  int hm  = (12 * 3600) + (34 * 60);      // The time 12:34:00 in seconds.
  int z   = 3600;                         // TZ offset.

  int ld = local_s > hms ? 86400 : 0;     // Local extra day if now > hms.
  int ud = utc_s   > hms ? 86400 : 0;     // UTC extra day if now > hms.
  std::cout << "# ld " << ld << "\n";
  std::cout << "# ud " << ud << "\n";

  // Aggregated.
  //            input                         i  Year  Mo  Wk WD  Jul  Da   Secs     TZ    UTC      time_t
  testParse (t, "12:34:56  ",                 8,    0,  0,  0, 0,   0,  0,   hms,     0, false, local+hms+ld );

  // time-ext
  //            input                         i  Year  Mo  Wk WD  Jul  Da   Secs     TZ    UTC      time_t
  testParse (t, "12:34:56Z",                  9,    0,  0,  0, 0,   0,  0,   hms,     0,  true, utc+hms+ud   );
  testParse (t, "12:34Z",                     6,    0,  0,  0, 0,   0,  0,    hm,     0,  true, utc+hm+ud    );
  testParse (t, "12:34:56+01:00",            14,    0,  0,  0, 0,   0,  0,   hms,  3600, false, utc+hms-z+ud );
  testParse (t, "12:34:56+01",               11,    0,  0,  0, 0,   0,  0,   hms,  3600, false, utc+hms-z+ud );
  testParse (t, "12:34+01:00",               11,    0,  0,  0, 0,   0,  0,    hm,  3600, false, utc+hm-z+ud  );
  testParse (t, "12:34+01",                   8,    0,  0,  0, 0,   0,  0,    hm,  3600, false, utc+hm-z+ud  );
  testParse (t, "12:34:56",                   8,    0,  0,  0, 0,   0,  0,   hms,     0, false, local+hms+ld );
  testParse (t, "12:34",                      5,    0,  0,  0, 0,   0,  0,    hm,     0, false, local+hm+ld  );

  // datetime-ext
  //            input                         i  Year  Mo  Wk WD  Jul  Da   Secs     TZ    UTC      time_t
  testParse (t, "2013-12-06",                10, 2013, 12,  0, 0,   0,  6,     0,     0, false, local6    );
  testParse (t, "2013-340",                   8, 2013,  0,  0, 0, 340,  0,     0,     0, false, local6    );
  testParse (t, "2013-W49-5",                10, 2013,  0, 49, 5,   0,  0,     0,     0, false, local6    );
  testParse (t, "2013-W49",                   8, 2013,  0, 49, 0,   0,  0,     0,     0, false, local1    );

  testParse (t, "2013-12-06T12:34:56",       19, 2013, 12,  0, 0,   0,  6,   hms,     0, false, local6+hms);
  testParse (t, "2013-12-06T12:34",          16, 2013, 12,  0, 0,   0,  6,    hm,     0, false, local6+hm );
  testParse (t, "2013-340T12:34:56",         17, 2013,  0,  0, 0, 340,  0,   hms,     0, false, local6+hms);
  testParse (t, "2013-340T12:34",            14, 2013,  0,  0, 0, 340,  0,    hm,     0, false, local6+hm );
  testParse (t, "2013-W49-5T12:34:56",       19, 2013,  0, 49, 5,   0,  0,   hms,     0, false, local6+hms);
  testParse (t, "2013-W49-5T12:34",          16, 2013,  0, 49, 5,   0,  0,    hm,     0, false, local6+hm );
  testParse (t, "2013-W49T12:34:56",         17, 2013,  0, 49, 0,   0,  0,   hms,     0, false, local1+hms);
  testParse (t, "2013-W49T12:34",            14, 2013,  0, 49, 0,   0,  0,    hm,     0, false, local1+hm );

  testParse (t, "2013-12-06T12:34:56Z",      20, 2013, 12,  0, 0,   0,  6,   hms,     0,  true, utc6+hms  );
  testParse (t, "2013-12-06T12:34Z",         17, 2013, 12,  0, 0,   0,  6,    hm,     0,  true, utc6+hm   );
  testParse (t, "2013-340T12:34:56Z",        18, 2013,  0,  0, 0, 340,  0,   hms,     0,  true, utc6+hms  );
  testParse (t, "2013-340T12:34Z",           15, 2013,  0,  0, 0, 340,  0,    hm,     0,  true, utc6+hm   );
  testParse (t, "2013-W49-5T12:34:56Z",      20, 2013,  0, 49, 5,   0,  0,   hms,     0,  true, utc6+hms  );
  testParse (t, "2013-W49-5T12:34Z",         17, 2013,  0, 49, 5,   0,  0,    hm,     0,  true, utc6+hm   );
  testParse (t, "2013-W49T12:34:56Z",        18, 2013,  0, 49, 0,   0,  0,   hms,     0,  true, utc1+hms  );
  testParse (t, "2013-W49T12:34Z",           15, 2013,  0, 49, 0,   0,  0,    hm,     0,  true, utc1+hm   );

  testParse (t, "2013-12-06T12:34:56+01:00", 25, 2013, 12,  0, 0,   0,  6,   hms,  3600, false, utc6+hms-z);
  testParse (t, "2013-12-06T12:34:56+01",    22, 2013, 12,  0, 0,   0,  6,   hms,  3600, false, utc6+hms-z);
  testParse (t, "2013-12-06T12:34:56-01:00", 25, 2013, 12,  0, 0,   0,  6,   hms, -3600, false, utc6+hms+z);
  testParse (t, "2013-12-06T12:34:56-01",    22, 2013, 12,  0, 0,   0,  6,   hms, -3600, false, utc6+hms+z);
  testParse (t, "2013-12-06T12:34+01:00",    22, 2013, 12,  0, 0,   0,  6,    hm,  3600, false, utc6+hm-z );
  testParse (t, "2013-12-06T12:34+01",       19, 2013, 12,  0, 0,   0,  6,    hm,  3600, false, utc6+hm-z );
  testParse (t, "2013-12-06T12:34-01:00",    22, 2013, 12,  0, 0,   0,  6,    hm, -3600, false, utc6+hm+z );
  testParse (t, "2013-12-06T12:34-01",       19, 2013, 12,  0, 0,   0,  6,    hm, -3600, false, utc6+hm+z );
  testParse (t, "2013-340T12:34:56+01:00",   23, 2013,  0,  0, 0, 340,  0,   hms,  3600, false, utc6+hms-z);
  testParse (t, "2013-340T12:34:56+01",      20, 2013,  0,  0, 0, 340,  0,   hms,  3600, false, utc6+hms-z);
  testParse (t, "2013-340T12:34:56-01:00",   23, 2013,  0,  0, 0, 340,  0,   hms, -3600, false, utc6+hms+z);
  testParse (t, "2013-340T12:34:56-01",      20, 2013,  0,  0, 0, 340,  0,   hms, -3600, false, utc6+hms+z);
  testParse (t, "2013-340T12:34+01:00",      20, 2013,  0,  0, 0, 340,  0,    hm,  3600, false, utc6+hm-z );
  testParse (t, "2013-340T12:34+01",         17, 2013,  0,  0, 0, 340,  0,    hm,  3600, false, utc6+hm-z );
  testParse (t, "2013-340T12:34-01:00",      20, 2013,  0,  0, 0, 340,  0,    hm, -3600, false, utc6+hm+z );
  testParse (t, "2013-340T12:34-01",         17, 2013,  0,  0, 0, 340,  0,    hm, -3600, false, utc6+hm+z );
  testParse (t, "2013-W49-5T12:34:56+01:00", 25, 2013,  0, 49, 5,   0,  0,   hms,  3600, false, utc6+hms-z);
  testParse (t, "2013-W49-5T12:34:56+01",    22, 2013,  0, 49, 5,   0,  0,   hms,  3600, false, utc6+hms-z);
  testParse (t, "2013-W49-5T12:34:56-01:00", 25, 2013,  0, 49, 5,   0,  0,   hms, -3600, false, utc6+hms+z);
  testParse (t, "2013-W49-5T12:34:56-01",    22, 2013,  0, 49, 5,   0,  0,   hms, -3600, false, utc6+hms+z);
  testParse (t, "2013-W49-5T12:34+01:00",    22, 2013,  0, 49, 5,   0,  0,    hm,  3600, false, utc6+hm-z );
  testParse (t, "2013-W49-5T12:34+01",       19, 2013,  0, 49, 5,   0,  0,    hm,  3600, false, utc6+hm-z );
  testParse (t, "2013-W49-5T12:34-01:00",    22, 2013,  0, 49, 5,   0,  0,    hm, -3600, false, utc6+hm+z );
  testParse (t, "2013-W49-5T12:34-01",       19, 2013,  0, 49, 5,   0,  0,    hm, -3600, false, utc6+hm+z );
  testParse (t, "2013-W49T12:34:56+01:00",   23, 2013,  0, 49, 0,   0,  0,   hms,  3600, false, utc1+hms-z);
  testParse (t, "2013-W49T12:34:56+01",      20, 2013,  0, 49, 0,   0,  0,   hms,  3600, false, utc1+hms-z);
  testParse (t, "2013-W49T12:34:56-01:00",   23, 2013,  0, 49, 0,   0,  0,   hms, -3600, false, utc1+hms+z);
  testParse (t, "2013-W49T12:34:56-01",      20, 2013,  0, 49, 0,   0,  0,   hms, -3600, false, utc1+hms+z);
  testParse (t, "2013-W49T12:34+01:00",      20, 2013,  0, 49, 0,   0,  0,    hm,  3600, false, utc1+hm-z );
  testParse (t, "2013-W49T12:34+01",         17, 2013,  0, 49, 0,   0,  0,    hm,  3600, false, utc1+hm-z );
  testParse (t, "2013-W49T12:34-01:00",      20, 2013,  0, 49, 0,   0,  0,    hm, -3600, false, utc1+hm+z );
  testParse (t, "2013-W49T12:34-01",         17, 2013,  0, 49, 0,   0,  0,    hm, -3600, false, utc1+hm+z );

  // The only non-extended forms.
  testParse (t, "20131206T123456Z",          16, 2013, 12,  0, 0,   0,  6,   hms,     0,  true, utc6+hms  );
  testParse (t, "20131206T123456",           15, 2013, 12,  0, 0,   0,  6,   hms,     0, false, local6+hms);

  try
  {
    ISO8601d now;
    t.ok (now.toISO ().find ("1969") == std::string::npos, "'now' != 1969");

    ISO8601d yesterday;
    yesterday -= 86400;
    ISO8601d tomorrow;
    tomorrow += 86400;

    t.ok    (yesterday <= now,       "yesterday <= now");
    t.ok    (yesterday <  now,       "yesterday < now");
    t.notok (yesterday == now,       "!(yesterday == now)");
    t.ok    (yesterday != now,       "yesterday != now");
    t.ok    (now       >= yesterday, "now >= yesterday");
    t.ok    (now       >  yesterday, "now > yesterday");

    t.ok    (tomorrow >= now,        "tomorrow >= now");
    t.ok    (tomorrow >  now,        "tomorrow > now");
    t.notok (tomorrow == now,        "!(tomorrow == now)");
    t.ok    (tomorrow != now,        "tomorrow != now");
    t.ok    (now      <= tomorrow,   "now <= tomorrow");
    t.ok    (now      <  tomorrow,   "now < tomorrow");

    // ctor ("now")
    context.config.set ("weekstart", "monday");
    ISO8601d relative_now;
    t.ok (relative_now.sameHour (now),  "ISO8601d ().sameHour (ISO8601d (now))");
    t.ok (relative_now.sameDay (now),   "ISO8601d ().sameDay (ISO8601d (now))");
    t.ok (relative_now.sameWeek (now),  "ISO8601d ().sameWeek (ISO8601d (now))");
    t.ok (relative_now.sameMonth (now), "ISO8601d ().sameMonth (ISO8601d (now))");
    t.ok (relative_now.sameYear (now),  "ISO8601d ().sameYear (ISO8601d (now))");

    // Loose comparisons.
    ISO8601d left ("7/4/2008", "m/d/Y");
    ISO8601d comp1 ("7/4/2008", "m/d/Y");
    t.ok (left.sameDay   (comp1), "7/4/2008 is on the same day as 7/4/2008");
    t.ok (left.sameWeek  (comp1), "7/4/2008 is on the same week as 7/4/2008");
    t.ok (left.sameMonth (comp1), "7/4/2008 is in the same month as 7/4/2008");
    t.ok (left.sameYear  (comp1), "7/4/2008 is in the same year as 7/4/2008");

    ISO8601d comp2 ("7/5/2008", "m/d/Y");
    t.notok (left.sameDay   (comp2), "7/4/2008 is not on the same day as 7/5/2008");
    t.ok    (left.sameMonth (comp2), "7/4/2008 is in the same month as 7/5/2008");
    t.ok    (left.sameYear  (comp2), "7/4/2008 is in the same year as 7/5/2008");

    ISO8601d comp3 ("8/4/2008", "m/d/Y");
    t.notok (left.sameDay   (comp3), "7/4/2008 is not on the same day as 8/4/2008");
    t.notok (left.sameWeek  (comp3), "7/4/2008 is not on the same week as 8/4/2008");
    t.notok (left.sameMonth (comp3), "7/4/2008 is not in the same month as 8/4/2008");
    t.ok    (left.sameYear  (comp3), "7/4/2008 is in the same year as 8/4/2008");

    ISO8601d comp4 ("7/4/2009", "m/d/Y");
    t.notok (left.sameDay   (comp4), "7/4/2008 is not on the same day as 7/4/2009");
    t.notok (left.sameWeek  (comp3), "7/4/2008 is not on the same week as 7/4/2009");
    t.notok (left.sameMonth (comp4), "7/4/2008 is not in the same month as 7/4/2009");
    t.notok (left.sameYear  (comp4), "7/4/2008 is not in the same year as 7/4/2009");

    // Validity.
    t.ok    (ISO8601d::valid (2, 29, 2008), "valid: 2/29/2008");
    t.notok (ISO8601d::valid (2, 29, 2007), "invalid: 2/29/2007");

    t.ok    (ISO8601d::valid ("2/29/2008", "m/d/Y"), "valid: 2/29/2008");
    t.notok (ISO8601d::valid ("2/29/2007", "m/d/Y"), "invalid: 2/29/2007");

    t.ok    (ISO8601d::valid (366, 2008), "valid: 366 days in 2008");
    t.notok (ISO8601d::valid (366, 2007), "invalid: 366 days in 2007");

    // Time validity.
    t.ok    (ISO8601d::valid (2, 28, 2010,  0,  0,  0), "valid 2/28/2010 0:00:00");
    t.ok    (ISO8601d::valid (2, 28, 2010, 23, 59, 59), "valid 2/28/2010 23:59:59");
    t.notok (ISO8601d::valid (2, 28, 2010, 24, 59, 59), "valid 2/28/2010 24:59:59");
    t.notok (ISO8601d::valid (2, 28, 2010, -1,  0,  0), "valid 2/28/2010 -1:00:00");

    // Leap year.
    t.ok    (ISO8601d::leapYear (2008), "2008 is a leap year");
    t.notok (ISO8601d::leapYear (2007), "2007 is not a leap year");
    t.ok    (ISO8601d::leapYear (2000), "2000 is a leap year");
    t.notok (ISO8601d::leapYear (1900), "1900 is not a leap year");

    // Days in year.
    t.is (ISO8601d::daysInYear (2016), 366, "366 days in 2016");
    t.is (ISO8601d::daysInYear (2015), 365, "365 days in 2015");

    // Days in month.
    t.is (ISO8601d::daysInMonth (2, 2008), 29, "29 days in February 2008");
    t.is (ISO8601d::daysInMonth (2, 2007), 28, "28 days in February 2007");

    // Names.
    t.is (ISO8601d::monthName (1), "January",   "1 = January");
    t.is (ISO8601d::monthName (2), "February",  "2 = February");
    t.is (ISO8601d::monthName (3), "March",     "3 = March");
    t.is (ISO8601d::monthName (4), "April",     "4 = April");
    t.is (ISO8601d::monthName (5), "May",       "5 = May");
    t.is (ISO8601d::monthName (6), "June",      "6 = June");
    t.is (ISO8601d::monthName (7), "July",      "7 = July");
    t.is (ISO8601d::monthName (8), "August",    "8 = August");
    t.is (ISO8601d::monthName (9), "September", "9 = September");
    t.is (ISO8601d::monthName (10), "October",  "10 = October");
    t.is (ISO8601d::monthName (11), "November", "11 = November");
    t.is (ISO8601d::monthName (12), "December", "12 = December");

    // Names.
    t.is (ISO8601d::monthOfYear ("January"),   1,  "January   =  1");
    t.is (ISO8601d::monthOfYear ("February"),  2,  "February  =  2");
    t.is (ISO8601d::monthOfYear ("March"),     3,  "March     =  3");
    t.is (ISO8601d::monthOfYear ("April"),     4,  "April     =  4");
    t.is (ISO8601d::monthOfYear ("May"),       5,  "May       =  5");
    t.is (ISO8601d::monthOfYear ("June"),      6,  "June      =  6");
    t.is (ISO8601d::monthOfYear ("July"),      7,  "July      =  7");
    t.is (ISO8601d::monthOfYear ("August"),    8,  "August    =  8");
    t.is (ISO8601d::monthOfYear ("September"), 9,  "September =  9");
    t.is (ISO8601d::monthOfYear ("October"),   10, "October   = 10");
    t.is (ISO8601d::monthOfYear ("November"),  11, "November  = 11");
    t.is (ISO8601d::monthOfYear ("December"),  12, "December  = 12");

    t.is (ISO8601d::dayName (0), "Sunday",    "0 == Sunday");
    t.is (ISO8601d::dayName (1), "Monday",    "1 == Monday");
    t.is (ISO8601d::dayName (2), "Tuesday",   "2 == Tuesday");
    t.is (ISO8601d::dayName (3), "Wednesday", "3 == Wednesday");
    t.is (ISO8601d::dayName (4), "Thursday",  "4 == Thursday");
    t.is (ISO8601d::dayName (5), "Friday",    "5 == Friday");
    t.is (ISO8601d::dayName (6), "Saturday",  "6 == Saturday");

    t.is (ISO8601d::dayOfWeek ("SUNDAY"),    0, "SUNDAY == 0");
    t.is (ISO8601d::dayOfWeek ("sunday"),    0, "sunday == 0");
    t.is (ISO8601d::dayOfWeek ("Sunday"),    0, "Sunday == 0");
    t.is (ISO8601d::dayOfWeek ("Monday"),    1, "Monday == 1");
    t.is (ISO8601d::dayOfWeek ("Tuesday"),   2, "Tuesday == 2");
    t.is (ISO8601d::dayOfWeek ("Wednesday"), 3, "Wednesday == 3");
    t.is (ISO8601d::dayOfWeek ("Thursday"),  4, "Thursday == 4");
    t.is (ISO8601d::dayOfWeek ("Friday"),    5, "Friday == 5");
    t.is (ISO8601d::dayOfWeek ("Saturday"),  6, "Saturday == 6");

    ISO8601d happyNewYear (1, 1, 2008);
    t.is (happyNewYear.dayOfWeek (), 2, "1/1/2008 == Tuesday");
    t.is (happyNewYear.month (),     1, "1/1/2008 == January");
    t.is (happyNewYear.day (),       1, "1/1/2008 == 1");
    t.is (happyNewYear.year (),   2008, "1/1/2008 == 2008");

    t.is (happyNewYear.toString (), "1/1/2008", "toString 1/1/2008");

    int m, d, y;
    happyNewYear.toMDY (m, d, y);
    t.is (m, 1, "1/1/2008 == January");
    t.is (d, 1, "1/1/2008 == 1");
    t.is (y, 2008, "1/1/2008 == 2008");

    ISO8601d epoch (9, 8, 2001);
    t.ok ((int)epoch.toEpoch () < 1000000000, "9/8/2001 < 1,000,000,000");
    epoch += 172800;
    t.ok ((int)epoch.toEpoch () > 1000000000, "9/10/2001 > 1,000,000,000");

    ISO8601d fromEpoch (epoch.toEpoch ());
    t.is (fromEpoch.toString (), epoch.toString (), "ctor (time_t)");

    ISO8601d iso (1000000000);
    t.is (iso.toISO (), "20010909T014640Z", "1,000,000,000 -> 20010909T014640Z");

    // Quantization.
    ISO8601d quant (1234526400);
    t.is (quant.startOfDay ().toString ("YMDHNS"),   "20090213000000", "1234526400 -> 2/13/2009 12:00:00 UTC -> 2/13/2009 0:00:00");
    t.is (quant.startOfWeek ().toString ("YMDHNS"),  "20090208000000", "1234526400 -> 2/13/2009 12:00:00 UTC -> 2/8/2009 0:00:00");
    t.is (quant.startOfMonth ().toString ("YMDHNS"), "20090201000000", "1234526400 -> 2/13/2009 12:00:00 UTC -> 2/1/2009 0:00:00");
    t.is (quant.startOfYear ().toString ("YMDHNS"),  "20090101000000", "1234526400 -> 2/13/2009 12:00:00 UTC -> 1/1/2009 0:00:00");

    // Format parsing.
    ISO8601d fromString1 ("1/1/2008", "m/d/Y");
    t.is (fromString1.month (),   1, "ctor (std::string) -> m");
    t.is (fromString1.day (),     1, "ctor (std::string) -> d");
    t.is (fromString1.year (), 2008, "ctor (std::string) -> y");

    ISO8601d fromString2 ("20080101", "YMD");
    t.is (fromString2.month (),   1, "ctor (std::string) -> m");
    t.is (fromString2.day (),     1, "ctor (std::string) -> d");
    t.is (fromString2.year (), 2008, "ctor (std::string) -> y");

    ISO8601d fromString3 ("12/31/2007", "m/d/Y");
    t.is (fromString3.month (),  12, "ctor (std::string) -> m");
    t.is (fromString3.day (),    31, "ctor (std::string) -> d");
    t.is (fromString3.year (), 2007, "ctor (std::string) -> y");

    ISO8601d fromString4 ("01/01/2008", "m/d/Y");
    t.is (fromString4.month (),   1, "ctor (std::string) -> m");
    t.is (fromString4.day (),     1, "ctor (std::string) -> d");
    t.is (fromString4.year (), 2008, "ctor (std::string) -> y");

    ISO8601d fromString5 ("Tue 05 Feb 2008 (06)", "a D b Y (V)");
    t.is (fromString5.month (),   2, "ctor (std::string) -> m");
    t.is (fromString5.day (),     5, "ctor (std::string) -> d");
    t.is (fromString5.year (), 2008, "ctor (std::string) -> y");

    ISO8601d fromString6 ("Tuesday, February 5, 2008", "A, B d, Y");
    t.is (fromString6.month (),   2, "ctor (std::string) -> m");
    t.is (fromString6.day (),     5, "ctor (std::string) -> d");
    t.is (fromString6.year (), 2008, "ctor (std::string) -> y");

    ISO8601d fromString7 ("w01 Tue 2008-01-01", "wV a Y-M-D");
    t.is (fromString7.month (),   1, "ctor (std::string) -> m");
    t.is (fromString7.day (),     1, "ctor (std::string) -> d");
    t.is (fromString7.year (), 2008, "ctor (std::string) -> y");

    ISO8601d fromString8 ("6/7/2010 1:23:45",  "m/d/Y h:N:S");
    t.is (fromString8.month (),     6, "ctor (std::string) -> m");
    t.is (fromString8.day (),       7, "ctor (std::string) -> d");
    t.is (fromString8.year (),   2010, "ctor (std::string) -> Y");
    t.is (fromString8.hour (),      1, "ctor (std::string) -> h");
    t.is (fromString8.minute (),   23, "ctor (std::string) -> N");
    t.is (fromString8.second (),   45, "ctor (std::string) -> S");

    ISO8601d fromString9 ("6/7/2010 01:23:45", "m/d/Y H:N:S");
    t.is (fromString9.month (),     6, "ctor (std::string) -> m");
    t.is (fromString9.day (),       7, "ctor (std::string) -> d");
    t.is (fromString9.year (),   2010, "ctor (std::string) -> Y");
    t.is (fromString9.hour (),      1, "ctor (std::string) -> h");
    t.is (fromString9.minute (),   23, "ctor (std::string) -> N");
    t.is (fromString9.second (),   45, "ctor (std::string) -> S");

    ISO8601d fromString10 ("6/7/2010 12:34:56", "m/d/Y H:N:S");
    t.is (fromString10.month (),     6, "ctor (std::string) -> m");
    t.is (fromString10.day (),       7, "ctor (std::string) -> d");
    t.is (fromString10.year (),   2010, "ctor (std::string) -> Y");
    t.is (fromString10.hour (),     12, "ctor (std::string) -> h");
    t.is (fromString10.minute (),   34, "ctor (std::string) -> N");
    t.is (fromString10.second (),   56, "ctor (std::string) -> S");

    // Day of year
    t.is (ISO8601d ("1/1/2011",   "m/d/Y").dayOfYear (),   1, "dayOfYear (1/1/2011)   ->   1");
    t.is (ISO8601d ("5/1/2011",   "m/d/Y").dayOfYear (), 121, "dayOfYear (5/1/2011)   -> 121");
    t.is (ISO8601d ("12/31/2011", "m/d/Y").dayOfYear (), 365, "dayOfYear (12/31/2011) -> 365");

    // Relative dates.
    ISO8601d r1 ("today");
    t.ok (r1.sameDay (now), "today = now");

    ISO8601d r4 ("sunday");
    if (now.dayOfWeek () >= 0)
      t.ok (r4.sameDay (now + (0 - now.dayOfWeek () + 7) * 86400), "next sunday");
    else
      t.ok (r4.sameDay (now + (0 - now.dayOfWeek ()) * 86400), "next sunday");;

    ISO8601d r5 ("monday");
    if (now.dayOfWeek () >= 1)
      t.ok (r5.sameDay (now + (1 - now.dayOfWeek () + 7) * 86400), "next monday");
    else
      t.ok (r5.sameDay (now + (1 - now.dayOfWeek ()) * 86400), "next monday");;

    ISO8601d r6 ("tuesday");
    if (now.dayOfWeek () >= 2)
      t.ok (r6.sameDay (now + (2 - now.dayOfWeek () + 7) * 86400), "next tuesday");
    else
      t.ok (r6.sameDay (now + (2 - now.dayOfWeek ()) * 86400), "next tuesday");;

    ISO8601d r7 ("wednesday");
    if (now.dayOfWeek () >= 3)
      t.ok (r7.sameDay (now + (3 - now.dayOfWeek () + 7) * 86400), "next wednesday");
    else
      t.ok (r7.sameDay (now + (3 - now.dayOfWeek ()) * 86400), "next wednesday");;

    ISO8601d r8 ("thursday");
    if (now.dayOfWeek () >= 4)
      t.ok (r8.sameDay (now + (4 - now.dayOfWeek () + 7) * 86400), "next thursday");
    else
      t.ok (r8.sameDay (now + (4 - now.dayOfWeek ()) * 86400), "next thursday");;

    ISO8601d r9 ("friday");
    if (now.dayOfWeek () >= 5)
      t.ok (r9.sameDay (now + (5 - now.dayOfWeek () + 7) * 86400), "next friday");
    else
      t.ok (r9.sameDay (now + (5 - now.dayOfWeek ()) * 86400), "next friday");;

    ISO8601d r10 ("saturday");
    if (now.dayOfWeek () >= 6)
      t.ok (r10.sameDay (now + (6 - now.dayOfWeek () + 7) * 86400), "next saturday");
    else
      t.ok (r10.sameDay (now + (6 - now.dayOfWeek ()) * 86400), "next saturday");;

    ISO8601d r11 ("eow");
    t.ok (r11 < now + (8 * 86400), "eow < 7 days away");

    ISO8601d r12 ("eocw");
    t.ok (r12 > now - (8 * 86400), "eocw < 7 days in the past");

    ISO8601d r13 ("eom");
    t.ok (r13.sameMonth (now), "eom in same month as now");

    ISO8601d r14 ("eocm");
    t.ok (r14.sameMonth (now), "eocm in same month as now");

    ISO8601d r15 ("eoy");
    t.ok (r15.sameYear (now), "eoy in same year as now");

    ISO8601d r16 ("sow");
    t.ok (r16 < now + (8 * 86400), "sow < 7 days away");

    ISO8601d r23 ("socw");
    t.ok (r23 > now - (8 * 86400), "sow < 7 days in the past");

    ISO8601d r17 ("som");
    t.notok (r17.sameMonth (now), "som not in same month as now");

    ISO8601d r18 ("socm");
    t.ok (r18.sameMonth (now), "socm in same month as now");

    ISO8601d r19 ("soy");
    t.notok (r19.sameYear (now), "soy not in same year as now");

    ISO8601d first ("1st");
    t.notok (first.sameMonth (now), "1st not in same month as now");
    t.is (first.day (),   1, "1st day is 1");

    ISO8601d later ("later");
    t.is (later.month (),   1, "later -> m = 1");
    t.is (later.day (),    18, "later -> d = 18");
    t.is (later.year (), 2038, "later -> y = 2038");

    // Quarters
    ISO8601d soq ("soq");
    ISO8601d eoq ("eoq");
    t.is (soq.day (),  1,      "soq is the first day of a month");
    t.is (eoq.day () / 10,  3, "eoq is the 30th or 31th of a month");
    t.is (soq.month () % 3, 1, "soq month is 1, 4, 7 or 10");
    t.is (eoq.month () % 3, 0, "eoq month is 3, 6, 9 or 12");

    // Note: these fail during the night of daylight savings end.
    t.ok (soq.sameYear (now) ||
          (now.month () >= 10 &&
           soq.year () == now.year () + 1), "soq is in same year as now");
    t.ok (eoq.sameYear (now),  "eoq is in same year as now");

    // ISO8601d::sameHour
    ISO8601d r20 ("6/7/2010 01:00:00", "m/d/Y H:N:S");
    ISO8601d r21 ("6/7/2010 01:59:59", "m/d/Y H:N:S");
    t.ok (r20.sameHour (r21), "two dates within the same hour");

    ISO8601d r22 ("6/7/2010 00:59:59", "m/d/Y H:N:S");
    t.notok (r20.sameHour (r22), "two dates not within the same hour");

    // ISO8601d::operator-
    ISO8601d r25 (1234567890);
    t.is ((r25 - 1).toEpoch (), 1234567889, "1234567890 - 1 = 1234567889");

    // ISO8601d::operator--
    ISO8601d r26 (11, 7, 2010, 23, 59, 59);
    r26--;
    t.is (r26.toString ("YMDHNS"), "20101106235959", "decrement across fall DST boundary");

    ISO8601d r27 (3, 14, 2010, 23, 59, 59);
    r27--;
    t.is (r27.toString ("YMDHNS"), "20100313235959", "decrement across spring DST boundary");

    // ISO8601d::operator++
    ISO8601d r28 (11, 6, 2010, 23, 59, 59);
    r28++;
    t.is (r28.toString ("YMDHNS"), "20101107235959", "increment across fall DST boundary");

    ISO8601d r29 (3, 13, 2010, 23, 59, 59);
    r29++;
    t.is (r29.toString ("YMDHNS"), "20100314235959", "increment across spring DST boundary");

    // int ISO8601d::length (const std::string&);
    t.is (ISO8601d::length ("m"), 2,  "length 'm' --> 2");
    t.is (ISO8601d::length ("M"), 2,  "length 'M' --> 2");
    t.is (ISO8601d::length ("d"), 2,  "length 'd' --> 2");
    t.is (ISO8601d::length ("D"), 2,  "length 'D' --> 2");
    t.is (ISO8601d::length ("y"), 2,  "length 'y' --> 2");
    t.is (ISO8601d::length ("Y"), 4,  "length 'Y' --> 4");
    t.is (ISO8601d::length ("a"), 3,  "length 'a' --> 3");
    t.is (ISO8601d::length ("A"), 10, "length 'A' --> 10");
    t.is (ISO8601d::length ("b"), 3,  "length 'b' --> 3");
    t.is (ISO8601d::length ("B"), 10, "length 'B' --> 10");
    t.is (ISO8601d::length ("v"), 2,  "length 'v' --> 2");
    t.is (ISO8601d::length ("V"), 2,  "length 'V' --> 2");
    t.is (ISO8601d::length ("h"), 2,  "length 'h' --> 2");
    t.is (ISO8601d::length ("H"), 2,  "length 'H' --> 2");
    t.is (ISO8601d::length ("n"), 2,  "length 'n' --> 2");
    t.is (ISO8601d::length ("N"), 2,  "length 'N' --> 2");
    t.is (ISO8601d::length ("s"), 2,  "length 's' --> 2");
    t.is (ISO8601d::length ("S"), 2,  "length 'S' --> 2");
    t.is (ISO8601d::length ("j"), 3,  "length 'j' --> 3");
    t.is (ISO8601d::length ("J"), 3,  "length 'J' --> 3");

    t.is (ISO8601d::length (" "), 1, "length ' ' --> 1");

    // Depletion requirement.
    ISO8601d r30 ("Mon Jun 30 2014", "a b D Y");
    t.is (r30.toString ("YMDHNS"), "20140630000000", "Depletion required on complex format with spaces");

    ISO8601d r31 ("Mon Jun 30 2014 xxx", "a b D Y");
    t.is (r31.toString ("YMDHNS"), "20140630000000", "Depletion not required on complex format with spaces");
  }

  catch (const std::string& e)
  {
    t.fail ("Exception thrown.");
    t.diag (e);
  }

  return 0;
}