File indexing completed on 2024-12-22 04:18:20

0001 /***************************************************************************
0002  *                                                                         *
0003  *   Copyright : (C) 2012 Peter Kümmel                                     *
0004  *   email     : syntheticpp@gmx.net                                       *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 //#define KST_SMALL_PRREALLOC
0014 
0015 #include "kst_atof.h"
0016 #include "math_kst.h"
0017 
0018 #include <QtTest>
0019 #include <QTime>
0020 
0021 
0022 class kst_atofTest: public QObject
0023 {
0024     Q_OBJECT
0025     
0026 public:
0027     
0028     kst_atofTest()
0029     {
0030     }
0031    
0032     void setFormat(const QString& fmt)
0033     {
0034       LexicalCast::instance().setUseDotAsDecimalSeparator(true);
0035       LexicalCast::instance().setTimeFormat(fmt);
0036     }
0037 
0038 
0039     double msecsTo(const QString& time, const QString& fmt)
0040     {
0041       const QTime t = QTime::fromString(time, fmt);
0042       return QTime(0, 0, 0).msecsTo(t) / 1000.0;
0043     }
0044 
0045 
0046     double msecsToDate(const QString& time, const QString& fmt)
0047     {
0048       QDateTime t = QDateTime::fromString(time, fmt);
0049       if (t.isValid()) {
0050         t.setTimeSpec(Qt::UTC);
0051   #if QT_VERSION >= 0x040700
0052         return t.toMSecsSinceEpoch() / 1000.0;
0053   #else
0054         return t.toTime_t();
0055   #endif
0056       }
0057       return 0;
0058     }
0059 
0060 
0061 private slots:
0062 
0063   void time()
0064   {
0065       double ref = msecsTo("12:00:00", "hh:mm:ss");
0066       
0067       setFormat("hh:mm:ss");
0068       QVERIFY(LexicalCast::instance().toDouble("12:00:00") == ref);
0069 
0070       QVERIFY(LexicalCast::instance().toDouble("12:00:00 ") == ref);
0071       QVERIFY(KST_ISNAN(LexicalCast::instance().toDouble("  12:00:00")));
0072 
0073       QVERIFY(LexicalCast::instance().toDouble("12:00:00xxx") == ref);
0074 
0075       QVERIFY(KST_ISNAN(LexicalCast::instance().toDouble("12:00:0")));
0076       QVERIFY(KST_ISNAN(LexicalCast::instance().toDouble(" 12:00:00")));
0077       QVERIFY(KST_ISNAN(LexicalCast::instance().toDouble(" 12:00:00.200")));
0078 
0079       setFormat("hh:mm:ss.zzz");
0080       QVERIFY(LexicalCast::instance().toDouble("12:00:00.123") == ref + 0.123);
0081       QVERIFY(LexicalCast::instance().toDouble("12:00:00.123 ") == ref + 0.123);
0082 
0083       setFormat("hh  :  mm  :  ss");
0084       QVERIFY(LexicalCast::instance().toDouble("12  :  00  :  00") == ref);
0085       QVERIFY(LexicalCast::instance().toDouble("12  :  00  :  00  ") == ref);
0086   }
0087 
0088 
0089   void dateAndTime()
0090   {
0091       double ref = msecsToDate("11.11.2011 12:00:00", "dd.MM.yyyy hh:mm:ss");
0092       
0093       setFormat("dd.MM.yyyy hh:mm:ss");
0094       QVERIFY(LexicalCast::instance().toDouble("11.11.2011 12:00:00") == ref);
0095       QVERIFY(LexicalCast::instance().toDouble("11.11.2011 12:00:00  ") == ref);
0096       QVERIFY(KST_ISNAN(LexicalCast::instance().toDouble("11.11.2011 12:00:0")));
0097       QVERIFY(KST_ISNAN(LexicalCast::instance().toDouble("11.11.2011  12:00:00")));
0098 
0099       setFormat("dd.MM.yyyy  hh:mm:ss");
0100       QVERIFY(LexicalCast::instance().toDouble("11.11.2011  12:00:00") == ref);
0101       setFormat("dd.MM.yyyy  XYZ   hh:mm:ss");
0102       QVERIFY(LexicalCast::instance().toDouble("11.11.2011  XYZ   12:00:00") == ref);
0103   }
0104 
0105 
0106 };
0107 
0108 
0109 
0110 QTEST_MAIN(kst_atofTest)
0111 
0112 
0113 
0114 #include "moc_asciiatoftest.cpp"