├── .gitignore ├── UnitTest++ ├── src │ ├── TestReporter.cpp │ ├── TimeHelpers.h │ ├── tests │ │ ├── Main.cpp │ │ ├── TestTestSuite.cpp │ │ ├── TestCurrentTest.cpp │ │ ├── TestAssertHandler.cpp │ │ ├── TestTestList.cpp │ │ ├── TestTimeConstraintMacro.cpp │ │ ├── ScopedCurrentTest.h │ │ ├── TestTimeConstraint.cpp │ │ ├── TestTest.cpp │ │ ├── RecordingReporter.h │ │ ├── TestTestResults.cpp │ │ ├── TestUnitTest++.cpp │ │ ├── TestDeferredTestReporter.cpp │ │ ├── TestMemoryOutStream.cpp │ │ ├── TestTestMacros.cpp │ │ └── TestXmlTestReporter.cpp │ ├── ReportAssert.h │ ├── TestSuite.h │ ├── ReportAssert.cpp │ ├── CurrentTest.h │ ├── CurrentTest.cpp │ ├── UnitTest++.h │ ├── Posix │ │ ├── TimeHelpers.h │ │ ├── TimeHelpers.cpp │ │ ├── SignalTranslator.h │ │ └── SignalTranslator.cpp │ ├── TestList.h │ ├── DeferredTestResult.cpp │ ├── TestDetails.cpp │ ├── TestReporter.h │ ├── TestReporterStdout.h │ ├── AssertException.h │ ├── TestDetails.h │ ├── TestList.cpp │ ├── DeferredTestResult.h │ ├── Test.h │ ├── DeferredTestReporter.h │ ├── TimeConstraint.cpp │ ├── TimeConstraint.h │ ├── Win32 │ │ ├── TimeHelpers.h │ │ └── TimeHelpers.cpp │ ├── Test.cpp │ ├── AssertException.cpp │ ├── TestResults.h │ ├── DeferredTestReporter.cpp │ ├── Config.h │ ├── XmlTestReporter.h │ ├── ExecuteTest.h │ ├── TestReporterStdout.cpp │ ├── TestRunner.h │ ├── Checks.cpp │ ├── TestResults.cpp │ ├── MemoryOutStream.h │ ├── TestRunner.cpp │ ├── MemoryOutStream.cpp │ ├── XmlTestReporter.cpp │ ├── CheckMacros.h │ ├── TestMacros.h │ └── Checks.h ├── COPYING ├── UnitTest++.vsnet2005.sln ├── README ├── Makefile ├── UnitTest++.vsnet2003.vcproj ├── TestUnitTest++.vsnet2005.vcproj └── UnitTest++.vsnet2005.vcproj ├── easySQLite.xcodeproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── _VERSION.TXT ├── _AUTHOR.TXT ├── README.md ├── easySQLite ├── SqlDatabase.h ├── easySqlite.h ├── SqlFieldSet.h ├── SqlField.h ├── SqlValue.h ├── SqlRecordSet.h ├── SqlTable.h ├── SqlDatabase.cpp ├── SqlRecord.h ├── SqlFieldSet.cpp ├── SqlCommon.h ├── SqlRecordSet.cpp ├── SqlField.cpp ├── SqlValue.cpp ├── SqlCommon.cpp ├── SqlTable.cpp └── SqlRecord.cpp ├── Tests ├── TestSqlValue.cpp ├── TestSqlField.cpp ├── TestSqlDatabase.cpp ├── TestSqlRecordSet.cpp ├── TestSqlFieldSet.cpp ├── TestSqlRecord.cpp └── TestSqlTable.cpp ├── _LICENSE.TXT ├── proj.linux └── Makefile ├── easySQLite.cpp └── _ReadMe.html /.gitignore: -------------------------------------------------------------------------------- 1 | libeasysqlite.a 2 | obj/ 3 | -------------------------------------------------------------------------------- /UnitTest++/src/TestReporter.cpp: -------------------------------------------------------------------------------- 1 | #include "TestReporter.h" 2 | 3 | namespace UnitTest { 4 | 5 | 6 | TestReporter::~TestReporter() 7 | { 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /UnitTest++/src/TimeHelpers.h: -------------------------------------------------------------------------------- 1 | #include "Config.h" 2 | 3 | #if defined UNITTEST_POSIX 4 | #include "Posix/TimeHelpers.h" 5 | #else 6 | #include "Win32/TimeHelpers.h" 7 | #endif 8 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/Main.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | #include "../TestReporterStdout.h" 3 | 4 | 5 | int main(int, char const *[]) 6 | { 7 | return UnitTest::RunAllTests(); 8 | } 9 | -------------------------------------------------------------------------------- /easySQLite.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /UnitTest++/src/ReportAssert.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_ASSERT_H 2 | #define UNITTEST_ASSERT_H 3 | 4 | namespace UnitTest { 5 | 6 | void ReportAssert(char const* description, char const* filename, int lineNumber); 7 | 8 | } 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /UnitTest++/src/TestSuite.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTSUITE_H 2 | #define UNITTEST_TESTSUITE_H 3 | 4 | namespace UnitTestSuite { 5 | 6 | inline char const* GetSuiteName () 7 | { 8 | return "DefaultSuite"; 9 | } 10 | 11 | } 12 | 13 | #endif 14 | 15 | -------------------------------------------------------------------------------- /UnitTest++/src/ReportAssert.cpp: -------------------------------------------------------------------------------- 1 | #include "AssertException.h" 2 | 3 | namespace UnitTest { 4 | 5 | void ReportAssert(char const* description, char const* filename, int lineNumber) 6 | { 7 | throw AssertException(description, filename, lineNumber); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /_VERSION.TXT: -------------------------------------------------------------------------------- 1 | 11-05-2013 2 | * Version 1.11 3 | * Updating code for cross platform games written in Cocos2dx 4 | 5 | 24-01-2013 6 | * Version 1.1 7 | * Adding iOS/OS X Support 8 | 9 | 16-09-2010 10 | * version: 1.0 11 | * new release 12 | * SQLite engine version: 3.7.2 13 | -------------------------------------------------------------------------------- /UnitTest++/src/CurrentTest.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_CURRENTTESTRESULTS_H 2 | #define UNITTEST_CURRENTTESTRESULTS_H 3 | 4 | namespace UnitTest { 5 | 6 | class TestResults; 7 | class TestDetails; 8 | 9 | namespace CurrentTest 10 | { 11 | TestResults*& Results(); 12 | const TestDetails*& Details(); 13 | } 14 | 15 | } 16 | 17 | #endif 18 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestTestSuite.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | 3 | // We're really testing if it's possible to use the same suite in two files 4 | // to compile and link successfuly (TestTestSuite.cpp has suite with the same name) 5 | // Note: we are outside of the anonymous namespace 6 | SUITE(SameTestSuite) 7 | { 8 | TEST(DummyTest2) 9 | { 10 | } 11 | } 12 | 13 | -------------------------------------------------------------------------------- /UnitTest++/src/CurrentTest.cpp: -------------------------------------------------------------------------------- 1 | #include "CurrentTest.h" 2 | #include 3 | 4 | namespace UnitTest { 5 | 6 | TestResults*& CurrentTest::Results() 7 | { 8 | static TestResults* testResults = NULL; 9 | return testResults; 10 | } 11 | 12 | const TestDetails*& CurrentTest::Details() 13 | { 14 | static const TestDetails* testDetails = NULL; 15 | return testDetails; 16 | } 17 | 18 | } 19 | -------------------------------------------------------------------------------- /UnitTest++/src/UnitTest++.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTESTCPP_H 2 | #define UNITTESTCPP_H 3 | 4 | //lint -esym(1509,*Fixture) 5 | 6 | #include "Config.h" 7 | #include "Test.h" 8 | #include "TestList.h" 9 | #include "TestSuite.h" 10 | #include "TestResults.h" 11 | 12 | #include "TestMacros.h" 13 | 14 | #include "CheckMacros.h" 15 | #include "TestRunner.h" 16 | #include "TimeConstraint.h" 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /UnitTest++/src/Posix/TimeHelpers.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TIMEHELPERS_H 2 | #define UNITTEST_TIMEHELPERS_H 3 | 4 | #include 5 | 6 | namespace UnitTest { 7 | 8 | class Timer 9 | { 10 | public: 11 | Timer(); 12 | void Start(); 13 | int GetTimeInMs() const; 14 | 15 | private: 16 | struct timeval m_startTime; 17 | }; 18 | 19 | 20 | namespace TimeHelpers 21 | { 22 | void SleepMs (int ms); 23 | } 24 | 25 | 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /_AUTHOR.TXT: -------------------------------------------------------------------------------- 1 | 2 | easySQLite 3 | ---------- 4 | 5 | This is C++ wrapper for SQLite C API database engine. 6 | 7 | Released under BSD License. 8 | 9 | SQLite part of this wrapper is using Public Domain license. 10 | 11 | Copyright (c) 2013 Dawid Drozd 12 | Copyright (c) 2013 Artcator Inc. 13 | Copyright (c) 2010 Piotr Zagawa 14 | 15 | All rights reserved. 16 | 17 | contact: 18 | mailto:oldpeter72@gmail.com 19 | mailto:drozddawid.uam@gmail.com 20 | 21 | website: 22 | http://vetch.magot.pl 23 | -------------------------------------------------------------------------------- /UnitTest++/src/TestList.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTLIST_H 2 | #define UNITTEST_TESTLIST_H 3 | 4 | 5 | namespace UnitTest { 6 | 7 | class Test; 8 | 9 | class TestList 10 | { 11 | public: 12 | TestList(); 13 | void Add (Test* test); 14 | 15 | Test* GetHead() const; 16 | 17 | private: 18 | Test* m_head; 19 | Test* m_tail; 20 | }; 21 | 22 | 23 | class ListAdder 24 | { 25 | public: 26 | ListAdder(TestList& list, Test* test); 27 | }; 28 | 29 | } 30 | 31 | 32 | #endif 33 | -------------------------------------------------------------------------------- /UnitTest++/src/DeferredTestResult.cpp: -------------------------------------------------------------------------------- 1 | #include "DeferredTestResult.h" 2 | #include "Config.h" 3 | 4 | namespace UnitTest 5 | { 6 | 7 | DeferredTestResult::DeferredTestResult() 8 | : suiteName("") 9 | , testName("") 10 | , failureFile("") 11 | , timeElapsed(0.0f) 12 | , failed(false) 13 | { 14 | } 15 | 16 | DeferredTestResult::DeferredTestResult(char const* suite, char const* test) 17 | : suiteName(suite) 18 | , testName(test) 19 | , failureFile("") 20 | , timeElapsed(0.0f) 21 | , failed(false) 22 | { 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /UnitTest++/src/TestDetails.cpp: -------------------------------------------------------------------------------- 1 | #include "TestDetails.h" 2 | 3 | namespace UnitTest { 4 | 5 | TestDetails::TestDetails(char const* testName_, char const* suiteName_, char const* filename_, int lineNumber_) 6 | : suiteName(suiteName_) 7 | , testName(testName_) 8 | , filename(filename_) 9 | , lineNumber(lineNumber_) 10 | { 11 | } 12 | 13 | TestDetails::TestDetails(const TestDetails& details, int lineNumber_) 14 | : suiteName(details.suiteName) 15 | , testName(details.testName) 16 | , filename(details.filename) 17 | , lineNumber(lineNumber_) 18 | { 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /UnitTest++/src/TestReporter.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTREPORTER_H 2 | #define UNITTEST_TESTREPORTER_H 3 | 4 | namespace UnitTest { 5 | 6 | class TestDetails; 7 | 8 | class TestReporter 9 | { 10 | public: 11 | virtual ~TestReporter(); 12 | 13 | virtual void ReportTestStart(TestDetails const& test) = 0; 14 | virtual void ReportFailure(TestDetails const& test, char const* failure) = 0; 15 | virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed) = 0; 16 | virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) = 0; 17 | }; 18 | 19 | } 20 | #endif 21 | -------------------------------------------------------------------------------- /UnitTest++/src/TestReporterStdout.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTREPORTERSTDOUT_H 2 | #define UNITTEST_TESTREPORTERSTDOUT_H 3 | 4 | #include "TestReporter.h" 5 | 6 | namespace UnitTest { 7 | 8 | class TestReporterStdout : public TestReporter 9 | { 10 | private: 11 | virtual void ReportTestStart(TestDetails const& test); 12 | virtual void ReportFailure(TestDetails const& test, char const* failure); 13 | virtual void ReportTestFinish(TestDetails const& test, float secondsElapsed); 14 | virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); 15 | }; 16 | 17 | } 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /UnitTest++/src/AssertException.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_ASSERTEXCEPTION_H 2 | #define UNITTEST_ASSERTEXCEPTION_H 3 | 4 | #include 5 | 6 | 7 | namespace UnitTest { 8 | 9 | class AssertException : public std::exception 10 | { 11 | public: 12 | AssertException(char const* description, char const* filename, int lineNumber); 13 | virtual ~AssertException() throw(); 14 | 15 | virtual char const* what() const throw(); 16 | 17 | char const* Filename() const; 18 | int LineNumber() const; 19 | 20 | private: 21 | char m_description[512]; 22 | char m_filename[256]; 23 | int m_lineNumber; 24 | }; 25 | 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This is a C++ wrapper for SQLite C API database engine. 2 | It was written to simplify and speedup coding of local database access. 3 | 4 | This fork is modified for Cocos2dx engine and cross platform games :) 5 | 6 | Old repository https://code.google.com/p/easysqlite/ 7 | 8 | Why easySQLite solution is better than others ? 9 | 10 | * elegant, objective solution 11 | * explicit naming and calling 12 | * uses exceptions or methods return values 13 | * clear, understandable usage 14 | * flexible and expandable 15 | * strongly tested (tests included) 16 | * Just take a look at sample code. 17 | 18 | Find more information in included html file and usage examples in project package. 19 | 20 | -------------------------------------------------------------------------------- /UnitTest++/src/TestDetails.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTDETAILS_H 2 | #define UNITTEST_TESTDETAILS_H 3 | 4 | namespace UnitTest { 5 | 6 | class TestDetails 7 | { 8 | public: 9 | TestDetails(char const* testName, char const* suiteName, char const* filename, int lineNumber); 10 | TestDetails(const TestDetails& details, int lineNumber); 11 | 12 | char const* const suiteName; 13 | char const* const testName; 14 | char const* const filename; 15 | int const lineNumber; 16 | 17 | TestDetails(TestDetails const&); // Why is it public? --> http://gcc.gnu.org/bugs.html#cxx_rvalbind 18 | private: 19 | TestDetails& operator=(TestDetails const&); 20 | }; 21 | 22 | } 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /UnitTest++/src/TestList.cpp: -------------------------------------------------------------------------------- 1 | #include "TestList.h" 2 | #include "Test.h" 3 | 4 | #include 5 | 6 | namespace UnitTest { 7 | 8 | TestList::TestList() 9 | : m_head(0) 10 | , m_tail(0) 11 | { 12 | } 13 | 14 | void TestList::Add(Test* test) 15 | { 16 | if (m_tail == 0) 17 | { 18 | assert(m_head == 0); 19 | m_head = test; 20 | m_tail = test; 21 | } 22 | else 23 | { 24 | m_tail->next = test; 25 | m_tail = test; 26 | } 27 | } 28 | 29 | Test* TestList::GetHead() const 30 | { 31 | return m_head; 32 | } 33 | 34 | ListAdder::ListAdder(TestList& list, Test* test) 35 | { 36 | list.Add(test); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /UnitTest++/src/DeferredTestResult.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_DEFERREDTESTRESULT_H 2 | #define UNITTEST_DEFERREDTESTRESULT_H 3 | 4 | #include 5 | #include 6 | 7 | namespace UnitTest 8 | { 9 | 10 | struct DeferredTestResult 11 | { 12 | DeferredTestResult(); 13 | DeferredTestResult(char const* suite, char const* test); 14 | 15 | std::string suiteName; 16 | std::string testName; 17 | std::string failureFile; 18 | 19 | typedef std::pair< int, std::string > Failure; 20 | typedef std::vector< Failure > FailureVec; 21 | FailureVec failures; 22 | 23 | float timeElapsed; 24 | bool failed; 25 | }; 26 | 27 | } 28 | 29 | #endif //UNITTEST_DEFERREDTESTRESULT_H 30 | -------------------------------------------------------------------------------- /UnitTest++/src/Posix/TimeHelpers.cpp: -------------------------------------------------------------------------------- 1 | #include "TimeHelpers.h" 2 | #include 3 | 4 | namespace UnitTest { 5 | 6 | Timer::Timer() 7 | { 8 | m_startTime.tv_sec = 0; 9 | m_startTime.tv_usec = 0; 10 | } 11 | 12 | void Timer::Start() 13 | { 14 | gettimeofday(&m_startTime, 0); 15 | } 16 | 17 | 18 | int Timer::GetTimeInMs() const 19 | { 20 | struct timeval currentTime; 21 | gettimeofday(¤tTime, 0); 22 | int const dsecs = currentTime.tv_sec - m_startTime.tv_sec; 23 | int const dus = currentTime.tv_usec - m_startTime.tv_usec; 24 | return dsecs*1000 + dus/1000; 25 | } 26 | 27 | 28 | void TimeHelpers::SleepMs (int ms) 29 | { 30 | usleep(ms * 1000); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /easySQLite/SqlDatabase.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2010 Piotr Zagawa 3 | // 4 | // Released under BSD License 5 | // 6 | 7 | #pragma once 8 | 9 | #include "sqlite3.h" 10 | #include "SqlCommon.h" 11 | 12 | 13 | namespace sql 14 | { 15 | 16 | class Database 17 | { 18 | private: 19 | sqlite3* _db; 20 | string _err_msg; 21 | int _result_open; 22 | 23 | public: 24 | Database(void); 25 | ~Database(void); 26 | 27 | public: 28 | sqlite3* getHandle(); 29 | string errMsg(); 30 | bool open(string filename); 31 | void close(); 32 | bool isOpen(); 33 | 34 | public: 35 | bool transactionBegin(); 36 | bool transactionCommit(); 37 | bool transactionRollback(); 38 | 39 | }; 40 | 41 | 42 | //sql eof 43 | }; 44 | -------------------------------------------------------------------------------- /UnitTest++/src/Test.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TEST_H 2 | #define UNITTEST_TEST_H 3 | 4 | #include "TestDetails.h" 5 | 6 | namespace UnitTest { 7 | 8 | class TestResults; 9 | class TestList; 10 | 11 | class Test 12 | { 13 | public: 14 | explicit Test(char const* testName, char const* suiteName = "DefaultSuite", char const* filename = "", int lineNumber = 0); 15 | virtual ~Test(); 16 | void Run(); 17 | 18 | TestDetails const m_details; 19 | Test* next; 20 | mutable bool m_timeConstraintExempt; 21 | 22 | static TestList& GetTestList(); 23 | 24 | virtual void RunImpl() const; 25 | 26 | private: 27 | Test(Test const&); 28 | Test& operator =(Test const&); 29 | }; 30 | 31 | 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /easySQLite/easySqlite.h: -------------------------------------------------------------------------------- 1 | /* 2 | * easySqlite.h 3 | * 4 | * Created on: Apr 25, 2013 5 | * Author: Dawid Drozd 6 | */ 7 | 8 | #ifndef EASYSQLITE_H_ 9 | #define EASYSQLITE_H_ 10 | 11 | #include "easySQLite/SqlCommon.h" 12 | #include "easySQLite/SqlField.h" 13 | #include "easySQLite/SqlDatabase.h" 14 | #include "easySQLite/SqlTable.h" 15 | #include "easySQLite/SqlDatabase.h" 16 | #include "easySQLite/SqlField.h" 17 | #include "easySQLite/SHA1.h" 18 | #include "easySQLite/sqlite3.h" 19 | #include "easySQLite/SqlRecord.h" 20 | #include "easySQLite/SqlTable.h" 21 | #include "easySQLite/SqlCommon.h" 22 | #include "easySQLite/SqlValue.h" 23 | #include "easySQLite/SqlFieldSet.h" 24 | #include "easySQLite/SqlRecordSet.h" 25 | 26 | using namespace sql; 27 | 28 | #endif /* EASYSQLITE_H_ */ 29 | -------------------------------------------------------------------------------- /UnitTest++/src/DeferredTestReporter.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_DEFERREDTESTREPORTER_H 2 | #define UNITTEST_DEFERREDTESTREPORTER_H 3 | 4 | #include "TestReporter.h" 5 | #include "DeferredTestResult.h" 6 | 7 | #include 8 | 9 | namespace UnitTest 10 | { 11 | 12 | class DeferredTestReporter : public TestReporter 13 | { 14 | public: 15 | virtual void ReportTestStart(TestDetails const& details); 16 | virtual void ReportFailure(TestDetails const& details, char const* failure); 17 | virtual void ReportTestFinish(TestDetails const& details, float secondsElapsed); 18 | 19 | typedef std::vector< DeferredTestResult > DeferredTestResultList; 20 | DeferredTestResultList& GetResults(); 21 | 22 | private: 23 | DeferredTestResultList m_results; 24 | }; 25 | 26 | } 27 | 28 | #endif 29 | -------------------------------------------------------------------------------- /UnitTest++/src/TimeConstraint.cpp: -------------------------------------------------------------------------------- 1 | #include "TimeConstraint.h" 2 | #include "TestResults.h" 3 | #include "MemoryOutStream.h" 4 | #include "CurrentTest.h" 5 | 6 | namespace UnitTest { 7 | 8 | 9 | TimeConstraint::TimeConstraint(int ms, TestDetails const& details) 10 | : m_details(details) 11 | , m_maxMs(ms) 12 | { 13 | m_timer.Start(); 14 | } 15 | 16 | TimeConstraint::~TimeConstraint() 17 | { 18 | int const totalTimeInMs = m_timer.GetTimeInMs(); 19 | if (totalTimeInMs > m_maxMs) 20 | { 21 | MemoryOutStream stream; 22 | stream << "Time constraint failed. Expected to run test under " << m_maxMs << 23 | "ms but took " << totalTimeInMs << "ms."; 24 | 25 | UnitTest::CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); 26 | } 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /UnitTest++/src/TimeConstraint.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TIMECONSTRAINT_H 2 | #define UNITTEST_TIMECONSTRAINT_H 3 | 4 | #include "TimeHelpers.h" 5 | 6 | namespace UnitTest { 7 | 8 | class TestResults; 9 | class TestDetails; 10 | 11 | class TimeConstraint 12 | { 13 | public: 14 | TimeConstraint(int ms, TestDetails const& details); 15 | ~TimeConstraint(); 16 | 17 | private: 18 | void operator=(TimeConstraint const&); 19 | TimeConstraint(TimeConstraint const&); 20 | 21 | Timer m_timer; 22 | TestDetails const& m_details; 23 | int const m_maxMs; 24 | }; 25 | 26 | #define UNITTEST_TIME_CONSTRAINT(ms) \ 27 | UnitTest::TimeConstraint unitTest__timeConstraint__(ms, UnitTest::TestDetails(m_details, __LINE__)) 28 | 29 | #define UNITTEST_TIME_CONSTRAINT_EXEMPT() do { m_timeConstraintExempt = true; } while (0) 30 | 31 | } 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /UnitTest++/src/Win32/TimeHelpers.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TIMEHELPERS_H 2 | #define UNITTEST_TIMEHELPERS_H 3 | 4 | #include "../Config.h" 5 | 6 | 7 | #ifdef UNITTEST_MINGW 8 | #ifndef __int64 9 | #define __int64 long long 10 | #endif 11 | #endif 12 | 13 | namespace UnitTest { 14 | 15 | class Timer 16 | { 17 | public: 18 | Timer(); 19 | void Start(); 20 | int GetTimeInMs() const; 21 | 22 | private: 23 | __int64 GetTime() const; 24 | 25 | void* m_threadHandle; 26 | 27 | #if defined(_WIN64) 28 | unsigned __int64 m_processAffinityMask; 29 | #else 30 | unsigned long m_processAffinityMask; 31 | #endif 32 | 33 | __int64 m_startTime; 34 | __int64 m_frequency; 35 | }; 36 | 37 | 38 | namespace TimeHelpers 39 | { 40 | void SleepMs (int ms); 41 | } 42 | 43 | 44 | } 45 | 46 | 47 | 48 | #endif 49 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestCurrentTest.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | #include "../CurrentTest.h" 3 | #include "ScopedCurrentTest.h" 4 | 5 | namespace 6 | { 7 | 8 | TEST(CanSetandGetDetails) 9 | { 10 | bool ok = false; 11 | { 12 | ScopedCurrentTest scopedTest; 13 | 14 | const UnitTest::TestDetails* details = reinterpret_cast< const UnitTest::TestDetails* >(12345); 15 | UnitTest::CurrentTest::Details() = details; 16 | 17 | ok = (UnitTest::CurrentTest::Details() == details); 18 | } 19 | 20 | CHECK(ok); 21 | } 22 | 23 | TEST(CanSetAndGetResults) 24 | { 25 | bool ok = false; 26 | { 27 | ScopedCurrentTest scopedTest; 28 | 29 | UnitTest::TestResults results; 30 | UnitTest::CurrentTest::Results() = &results; 31 | 32 | ok = (UnitTest::CurrentTest::Results() == &results); 33 | } 34 | 35 | CHECK(ok); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /UnitTest++/src/Test.cpp: -------------------------------------------------------------------------------- 1 | #include "Config.h" 2 | #include "Test.h" 3 | #include "TestList.h" 4 | #include "TestResults.h" 5 | #include "AssertException.h" 6 | #include "MemoryOutStream.h" 7 | #include "ExecuteTest.h" 8 | 9 | #ifdef UNITTEST_POSIX 10 | #include "Posix/SignalTranslator.h" 11 | #endif 12 | 13 | namespace UnitTest { 14 | 15 | TestList& Test::GetTestList() 16 | { 17 | static TestList s_list; 18 | return s_list; 19 | } 20 | 21 | Test::Test(char const* testName, char const* suiteName, char const* filename, int lineNumber) 22 | : m_details(testName, suiteName, filename, lineNumber) 23 | , next(0) 24 | , m_timeConstraintExempt(false) 25 | { 26 | } 27 | 28 | Test::~Test() 29 | { 30 | } 31 | 32 | void Test::Run() 33 | { 34 | ExecuteTest(*this, m_details); 35 | } 36 | 37 | void Test::RunImpl() const 38 | { 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /UnitTest++/src/AssertException.cpp: -------------------------------------------------------------------------------- 1 | #include "AssertException.h" 2 | #include 3 | 4 | namespace UnitTest { 5 | 6 | //CRT_SECURE_NO_WARNINGS 7 | #pragma warning(disable : 4996) 8 | 9 | AssertException::AssertException(char const* description, char const* filename, int lineNumber) 10 | : m_lineNumber(lineNumber) 11 | { 12 | using namespace std; 13 | 14 | strcpy(m_description, description); 15 | strcpy(m_filename, filename); 16 | } 17 | 18 | #pragma warning(default : 4996) 19 | 20 | AssertException::~AssertException() throw() 21 | { 22 | } 23 | 24 | char const* AssertException::what() const throw() 25 | { 26 | return m_description; 27 | } 28 | 29 | char const* AssertException::Filename() const 30 | { 31 | return m_filename; 32 | } 33 | 34 | int AssertException::LineNumber() const 35 | { 36 | return m_lineNumber; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /easySQLite/SqlFieldSet.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2010 Piotr Zagawa 3 | // 4 | // Released under BSD License 5 | // 6 | 7 | #pragma once 8 | 9 | #include 10 | #include 11 | #include "SqlField.h" 12 | 13 | 14 | namespace sql 15 | { 16 | 17 | class FieldSet 18 | { 19 | private: 20 | std::vector _vec; 21 | std::map _map; 22 | 23 | private: 24 | void copy(const std::vector& definition); 25 | 26 | public: 27 | FieldSet(Field* definition); 28 | FieldSet(std::vector& definition); 29 | FieldSet(const FieldSet& source); 30 | 31 | public: 32 | string toString(); 33 | int count(); 34 | Field* getByIndex(int index); 35 | Field* getByName(string name); 36 | 37 | public: 38 | string definitionHash(); 39 | string getDefinition(); 40 | static FieldSet* createFromDefinition(string value); 41 | 42 | }; 43 | 44 | 45 | //sql eof 46 | }; 47 | -------------------------------------------------------------------------------- /UnitTest++/src/TestResults.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTRESULTS_H 2 | #define UNITTEST_TESTRESULTS_H 3 | 4 | namespace UnitTest { 5 | 6 | class TestReporter; 7 | class TestDetails; 8 | 9 | class TestResults 10 | { 11 | public: 12 | explicit TestResults(TestReporter* reporter = 0); 13 | 14 | void OnTestStart(TestDetails const& test); 15 | void OnTestFailure(TestDetails const& test, char const* failure); 16 | void OnTestFinish(TestDetails const& test, float secondsElapsed); 17 | 18 | int GetTotalTestCount() const; 19 | int GetFailedTestCount() const; 20 | int GetFailureCount() const; 21 | 22 | private: 23 | TestReporter* m_testReporter; 24 | int m_totalTestCount; 25 | int m_failedTestCount; 26 | int m_failureCount; 27 | 28 | bool m_currentTestFailed; 29 | 30 | TestResults(TestResults const&); 31 | TestResults& operator =(TestResults const&); 32 | }; 33 | 34 | } 35 | 36 | #endif 37 | -------------------------------------------------------------------------------- /easySQLite/SqlField.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2010 Piotr Zagawa 3 | // 4 | // Released under BSD License 5 | // 6 | 7 | #pragma once 8 | 9 | #include "SqlCommon.h" 10 | 11 | 12 | namespace sql 13 | { 14 | 15 | class Field 16 | { 17 | public: 18 | friend class FieldSet; 19 | 20 | private: 21 | string _name; 22 | field_use _use; 23 | field_type _type; 24 | int _index; 25 | int _flags; 26 | 27 | public: 28 | Field(field_use use); 29 | Field(string name, field_type type, int flags = flag_none); 30 | Field(const Field& value); 31 | 32 | public: 33 | bool isKeyIdField(); 34 | bool isEndingField(); 35 | 36 | public: 37 | int getIndex(); 38 | string getName() const; 39 | string getTypeStr(); 40 | field_type getType(); 41 | bool isPrimaryKey(); 42 | bool isNotNull(); 43 | 44 | public: 45 | string getDefinition(); 46 | static Field* createFromDefinition(string value); 47 | 48 | }; 49 | 50 | 51 | //sql eof 52 | }; 53 | -------------------------------------------------------------------------------- /UnitTest++/src/DeferredTestReporter.cpp: -------------------------------------------------------------------------------- 1 | #include "DeferredTestReporter.h" 2 | #include "TestDetails.h" 3 | #include "Config.h" 4 | 5 | using namespace UnitTest; 6 | 7 | void DeferredTestReporter::ReportTestStart(TestDetails const& details) 8 | { 9 | m_results.push_back(DeferredTestResult(details.suiteName, details.testName)); 10 | } 11 | 12 | void DeferredTestReporter::ReportFailure(TestDetails const& details, char const* failure) 13 | { 14 | DeferredTestResult& r = m_results.back(); 15 | r.failed = true; 16 | r.failures.push_back(DeferredTestResult::Failure(details.lineNumber, failure)); 17 | r.failureFile = details.filename; 18 | } 19 | 20 | void DeferredTestReporter::ReportTestFinish(TestDetails const&, float secondsElapsed) 21 | { 22 | DeferredTestResult& r = m_results.back(); 23 | r.timeElapsed = secondsElapsed; 24 | } 25 | 26 | DeferredTestReporter::DeferredTestResultList& DeferredTestReporter::GetResults() 27 | { 28 | return m_results; 29 | } 30 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestAssertHandler.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | #include "../AssertException.h" 3 | #include "../ReportAssert.h" 4 | 5 | using namespace UnitTest; 6 | 7 | namespace { 8 | 9 | TEST(ReportAssertThrowsAssertException) 10 | { 11 | bool caught = false; 12 | 13 | try 14 | { 15 | ReportAssert("", "", 0); 16 | } 17 | catch(AssertException const&) 18 | { 19 | caught = true; 20 | } 21 | 22 | CHECK (true == caught); 23 | } 24 | 25 | TEST(ReportAssertSetsCorrectInfoInException) 26 | { 27 | const int lineNumber = 12345; 28 | const char* description = "description"; 29 | const char* filename = "filename"; 30 | 31 | try 32 | { 33 | ReportAssert(description, filename, lineNumber); 34 | } 35 | catch(AssertException const& e) 36 | { 37 | CHECK_EQUAL(description, e.what()); 38 | CHECK_EQUAL(filename, e.Filename()); 39 | CHECK_EQUAL(lineNumber, e.LineNumber()); 40 | } 41 | } 42 | 43 | 44 | } 45 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestTestList.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | #include "../TestList.h" 3 | 4 | using namespace UnitTest; 5 | 6 | namespace { 7 | 8 | 9 | TEST (TestListIsEmptyByDefault) 10 | { 11 | TestList list; 12 | CHECK (list.GetHead() == 0); 13 | } 14 | 15 | TEST (AddingTestSetsHeadToTest) 16 | { 17 | Test test("test"); 18 | TestList list; 19 | list.Add(&test); 20 | 21 | CHECK (list.GetHead() == &test); 22 | CHECK (test.next == 0); 23 | } 24 | 25 | TEST (AddingSecondTestAddsItToEndOfList) 26 | { 27 | Test test1("test1"); 28 | Test test2("test2"); 29 | 30 | TestList list; 31 | list.Add(&test1); 32 | list.Add(&test2); 33 | 34 | CHECK (list.GetHead() == &test1); 35 | CHECK (test1.next == &test2); 36 | CHECK (test2.next == 0); 37 | } 38 | 39 | TEST (ListAdderAddsTestToList) 40 | { 41 | TestList list; 42 | 43 | Test test(""); 44 | ListAdder adder(list, &test); 45 | 46 | CHECK (list.GetHead() == &test); 47 | CHECK (test.next == 0); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestTimeConstraintMacro.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | #include "../TimeHelpers.h" 3 | 4 | #include "RecordingReporter.h" 5 | #include "ScopedCurrentTest.h" 6 | 7 | namespace { 8 | 9 | TEST (TimeConstraintMacroQualifiesNamespace) 10 | { 11 | // If this compiles without a "using namespace UnitTest;", all is well. 12 | UNITTEST_TIME_CONSTRAINT(1); 13 | } 14 | 15 | TEST (TimeConstraintMacroUsesCorrectInfo) 16 | { 17 | int testLine = 0; 18 | RecordingReporter reporter; 19 | { 20 | UnitTest::TestResults testResults(&reporter); 21 | ScopedCurrentTest scopedResults(testResults); 22 | 23 | UNITTEST_TIME_CONSTRAINT(10); testLine = __LINE__; 24 | UnitTest::TimeHelpers::SleepMs(20); 25 | } 26 | 27 | using namespace std; 28 | 29 | CHECK_EQUAL(1, reporter.testFailedCount); 30 | CHECK(strstr(reporter.lastFailedFile, __FILE__)); 31 | CHECK_EQUAL(testLine, reporter.lastFailedLine); 32 | CHECK(strstr(reporter.lastFailedTest, "TimeConstraintMacroUsesCorrectInfo")); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /easySQLite/SqlValue.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2010 Piotr Zagawa 3 | // 4 | // Released under BSD License 5 | // 6 | 7 | #pragma once 8 | 9 | #include "SqlCommon.h" 10 | 11 | 12 | namespace sql 13 | { 14 | 15 | class Value 16 | { 17 | private: 18 | string _value; 19 | bool _isNull; 20 | field_type _type; 21 | 22 | public: 23 | Value(); 24 | Value(const char* value, field_type type); 25 | Value(const Value& value); 26 | Value& operator=(const Value& value); 27 | bool equals(Value& value); 28 | string toSql(field_type type); 29 | string toString(); 30 | 31 | public: 32 | string asString(); 33 | integer asInteger(); 34 | double asDouble(); 35 | bool asBool(); 36 | time asTime(); 37 | 38 | public: 39 | void setNull(); 40 | void setString(string value); 41 | void setInteger(integer value); 42 | void setDouble(double value); 43 | void setBool(bool value); 44 | void setTime(time value); 45 | 46 | public: 47 | bool isNull(); 48 | void setValue(const char* value, field_type type); 49 | 50 | }; 51 | 52 | 53 | //sql eof 54 | }; 55 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/ScopedCurrentTest.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_SCOPEDCURRENTTEST_H 2 | #define UNITTEST_SCOPEDCURRENTTEST_H 3 | 4 | #include "../CurrentTest.h" 5 | #include 6 | 7 | class ScopedCurrentTest 8 | { 9 | public: 10 | ScopedCurrentTest() 11 | : m_oldTestResults(UnitTest::CurrentTest::Results()) 12 | , m_oldTestDetails(UnitTest::CurrentTest::Details()) 13 | { 14 | } 15 | 16 | explicit ScopedCurrentTest(UnitTest::TestResults& newResults, const UnitTest::TestDetails* newDetails = NULL) 17 | : m_oldTestResults(UnitTest::CurrentTest::Results()) 18 | , m_oldTestDetails(UnitTest::CurrentTest::Details()) 19 | { 20 | UnitTest::CurrentTest::Results() = &newResults; 21 | 22 | if (newDetails != NULL) 23 | UnitTest::CurrentTest::Details() = newDetails; 24 | } 25 | 26 | ~ScopedCurrentTest() 27 | { 28 | UnitTest::CurrentTest::Results() = m_oldTestResults; 29 | UnitTest::CurrentTest::Details() = m_oldTestDetails; 30 | } 31 | 32 | private: 33 | UnitTest::TestResults* m_oldTestResults; 34 | const UnitTest::TestDetails* m_oldTestDetails; 35 | }; 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /UnitTest++/src/Config.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_CONFIG_H 2 | #define UNITTEST_CONFIG_H 3 | 4 | // Standard defines documented here: http://predef.sourceforge.net 5 | 6 | #if defined(_MSC_VER) 7 | #pragma warning(disable:4127) // conditional expression is constant 8 | #pragma warning(disable:4702) // unreachable code 9 | #pragma warning(disable:4722) // destructor never returns, potential memory leak 10 | 11 | #if (_MSC_VER == 1200) // VC6 12 | #pragma warning(disable:4786) 13 | #pragma warning(disable:4290) 14 | #endif 15 | #endif 16 | 17 | #if defined(unix) || defined(__unix__) || defined(__unix) || defined(linux) || \ 18 | defined(__APPLE__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) 19 | #define UNITTEST_POSIX 20 | #endif 21 | 22 | #if defined(__MINGW32__) 23 | #define UNITTEST_MINGW 24 | #endif 25 | 26 | // by default, MemoryOutStream is implemented in terms of std::ostringstream, which can be expensive. 27 | // uncomment this line to use the custom MemoryOutStream (no deps on std::ostringstream). 28 | 29 | //#define UNITTEST_USE_CUSTOM_STREAMS 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /Tests/TestSqlValue.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++/src/UnitTest++.h" 2 | #include "../easySQLite/SqlValue.h" 3 | 4 | 5 | namespace Tests 6 | { 7 | 8 | using namespace sql; 9 | 10 | 11 | TEST(ValueStore) 12 | { 13 | Value v_udf; 14 | Value v_int("123", type_int); 15 | Value v_txt("text", type_text); 16 | Value v_dbl("0.5678", type_float); 17 | Value v_bol("1", type_bool); 18 | Value v_tme("0", type_time); 19 | 20 | CHECK(v_udf.isNull()); 21 | CHECK_EQUAL(123, v_int.asInteger()); 22 | CHECK_EQUAL("text", v_txt.asString()); 23 | CHECK_EQUAL(0.5678, v_dbl.asDouble()); 24 | CHECK_EQUAL(true, v_bol.asBool()); 25 | CHECK(v_tme.asTime() == sql::time(0)); 26 | } 27 | 28 | TEST(ValueCompare) 29 | { 30 | Value v("test", type_text); 31 | 32 | Value v1(v); 33 | 34 | Value v2 = v; 35 | 36 | CHECK(v1.equals(v)); 37 | CHECK(v2.equals(v)); 38 | } 39 | 40 | TEST(ValueConvert) 41 | { 42 | Value v("678", type_int); 43 | 44 | CHECK_EQUAL(false, v.asBool()); 45 | CHECK_EQUAL(678, v.asDouble()); 46 | CHECK_EQUAL(678, v.asInteger()); 47 | CHECK_EQUAL("678", v.asString()); 48 | } 49 | 50 | 51 | //eofn 52 | }; 53 | -------------------------------------------------------------------------------- /UnitTest++/src/XmlTestReporter.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_XMLTESTREPORTER_H 2 | #define UNITTEST_XMLTESTREPORTER_H 3 | 4 | #include "DeferredTestReporter.h" 5 | 6 | #include 7 | 8 | namespace UnitTest 9 | { 10 | 11 | class XmlTestReporter : public DeferredTestReporter 12 | { 13 | public: 14 | explicit XmlTestReporter(std::ostream& ostream); 15 | 16 | virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); 17 | 18 | private: 19 | XmlTestReporter(XmlTestReporter const&); 20 | XmlTestReporter& operator=(XmlTestReporter const&); 21 | 22 | void AddXmlElement(std::ostream& os, char const* encoding); 23 | void BeginResults(std::ostream& os, int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed); 24 | void EndResults(std::ostream& os); 25 | void BeginTest(std::ostream& os, DeferredTestResult const& result); 26 | void AddFailure(std::ostream& os, DeferredTestResult const& result); 27 | void EndTest(std::ostream& os, DeferredTestResult const& result); 28 | 29 | std::ostream& m_ostream; 30 | }; 31 | 32 | } 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /UnitTest++/COPYING: -------------------------------------------------------------------------------- 1 | Copyright (c) 2006 Noel Llopis and Charles Nicholson 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included 12 | in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 18 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 19 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 20 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /UnitTest++/src/Posix/SignalTranslator.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_SIGNALTRANSLATOR_H 2 | #define UNITTEST_SIGNALTRANSLATOR_H 3 | 4 | #include 5 | #include 6 | 7 | namespace UnitTest { 8 | 9 | class SignalTranslator 10 | { 11 | public: 12 | SignalTranslator(); 13 | ~SignalTranslator(); 14 | 15 | static sigjmp_buf* s_jumpTarget; 16 | 17 | private: 18 | sigjmp_buf m_currentJumpTarget; 19 | sigjmp_buf* m_oldJumpTarget; 20 | 21 | struct sigaction m_old_SIGFPE_action; 22 | struct sigaction m_old_SIGTRAP_action; 23 | struct sigaction m_old_SIGSEGV_action; 24 | struct sigaction m_old_SIGBUS_action; 25 | struct sigaction m_old_SIGABRT_action; 26 | struct sigaction m_old_SIGALRM_action; 27 | }; 28 | 29 | #if !defined (__GNUC__) 30 | #define UNITTEST_EXTENSION 31 | #else 32 | #define UNITTEST_EXTENSION __extension__ 33 | #endif 34 | 35 | #define UNITTEST_THROW_SIGNALS \ 36 | UnitTest::SignalTranslator sig; \ 37 | if (UNITTEST_EXTENSION sigsetjmp(*UnitTest::SignalTranslator::s_jumpTarget, 1) != 0) \ 38 | throw ("Unhandled system exception"); 39 | 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /UnitTest++/src/ExecuteTest.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_EXECUTE_TEST_H 2 | #define UNITTEST_EXECUTE_TEST_H 3 | 4 | #include "TestDetails.h" 5 | #include "MemoryOutStream.h" 6 | #include "AssertException.h" 7 | #include "CurrentTest.h" 8 | 9 | #ifdef UNITTEST_POSIX 10 | #include "Posix/SignalTranslator.h" 11 | #endif 12 | 13 | namespace UnitTest { 14 | 15 | template< typename T > 16 | void ExecuteTest(T& testObject, TestDetails const& details) 17 | { 18 | CurrentTest::Details() = &details; 19 | 20 | try 21 | { 22 | #ifdef UNITTEST_POSIX 23 | UNITTEST_THROW_SIGNALS 24 | #endif 25 | testObject.RunImpl(); 26 | } 27 | catch (AssertException const& e) 28 | { 29 | CurrentTest::Results()->OnTestFailure( 30 | TestDetails(details.testName, details.suiteName, e.Filename(), e.LineNumber()), e.what()); 31 | } 32 | catch (std::exception const& e) 33 | { 34 | MemoryOutStream stream; 35 | stream << "Unhandled exception: " << e.what(); 36 | CurrentTest::Results()->OnTestFailure(details, stream.GetText()); 37 | } 38 | catch (...) 39 | { 40 | CurrentTest::Results()->OnTestFailure(details, "Unhandled exception: Crash!"); 41 | } 42 | } 43 | 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /easySQLite/SqlRecordSet.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2010 Piotr Zagawa 3 | // 4 | // Released under BSD License 5 | // 6 | 7 | #pragma once 8 | 9 | #include 10 | #include "sqlite3.h" 11 | #include "SqlCommon.h" 12 | #include "SqlRecord.h" 13 | 14 | 15 | namespace sql 16 | { 17 | 18 | class RecordSet 19 | { 20 | private: 21 | enum 22 | { 23 | DATASET_ITERATION_CONTINUE = 0, 24 | DATASET_ITERATION_ABORT = 1, 25 | }; 26 | 27 | private: 28 | sqlite3* _db; 29 | string _err_msg; 30 | int _result_query; 31 | FieldSet _fields; 32 | std::vector _records; 33 | 34 | private: 35 | static int on_next_record(void* param, int column_count, char** values, char** columns); 36 | 37 | public: 38 | RecordSet(sqlite3* db); 39 | RecordSet(sqlite3* db, Field* definition); 40 | RecordSet(sqlite3* db, FieldSet* fields); 41 | ~RecordSet(void); 42 | 43 | public: 44 | string errMsg(); 45 | bool isResult(); 46 | bool query(string sql); 47 | void close(); 48 | FieldSet* fields(); 49 | 50 | public: 51 | int count(); 52 | Record* getRecord(int record_index); 53 | Record* getTopRecord(); 54 | Value* getTopRecordFirstValue(); 55 | string toString(); 56 | 57 | }; 58 | 59 | 60 | //sql eof 61 | }; 62 | -------------------------------------------------------------------------------- /UnitTest++/src/Posix/SignalTranslator.cpp: -------------------------------------------------------------------------------- 1 | #include "SignalTranslator.h" 2 | 3 | namespace UnitTest { 4 | 5 | sigjmp_buf* SignalTranslator::s_jumpTarget = 0; 6 | 7 | namespace { 8 | 9 | void SignalHandler(int sig) 10 | { 11 | siglongjmp(*SignalTranslator::s_jumpTarget, sig ); 12 | } 13 | 14 | } 15 | 16 | 17 | SignalTranslator::SignalTranslator() 18 | { 19 | m_oldJumpTarget = s_jumpTarget; 20 | s_jumpTarget = &m_currentJumpTarget; 21 | 22 | struct sigaction action; 23 | action.sa_flags = 0; 24 | action.sa_handler = SignalHandler; 25 | sigemptyset( &action.sa_mask ); 26 | 27 | sigaction( SIGSEGV, &action, &m_old_SIGSEGV_action ); 28 | sigaction( SIGFPE , &action, &m_old_SIGFPE_action ); 29 | sigaction( SIGTRAP, &action, &m_old_SIGTRAP_action ); 30 | sigaction( SIGBUS , &action, &m_old_SIGBUS_action ); 31 | sigaction( SIGILL , &action, &m_old_SIGBUS_action ); 32 | } 33 | 34 | SignalTranslator::~SignalTranslator() 35 | { 36 | sigaction( SIGILL , &m_old_SIGBUS_action , 0 ); 37 | sigaction( SIGBUS , &m_old_SIGBUS_action , 0 ); 38 | sigaction( SIGTRAP, &m_old_SIGTRAP_action, 0 ); 39 | sigaction( SIGFPE , &m_old_SIGFPE_action , 0 ); 40 | sigaction( SIGSEGV, &m_old_SIGSEGV_action, 0 ); 41 | 42 | s_jumpTarget = m_oldJumpTarget; 43 | } 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /UnitTest++/src/TestReporterStdout.cpp: -------------------------------------------------------------------------------- 1 | #include "TestReporterStdout.h" 2 | #include 3 | 4 | #include "TestDetails.h" 5 | 6 | namespace UnitTest { 7 | 8 | void TestReporterStdout::ReportFailure(TestDetails const& details, char const* failure) 9 | { 10 | #if defined(__APPLE__) || defined(__GNUG__) 11 | char const* const errorFormat = "%s:%d: error: Failure in %s: %s\n"; 12 | #else 13 | char const* const errorFormat = "%s(%d): error: Failure in %s: %s\n"; 14 | #endif 15 | 16 | using namespace std; 17 | printf(errorFormat, details.filename, details.lineNumber, details.testName, failure); 18 | } 19 | 20 | void TestReporterStdout::ReportTestStart(TestDetails const& /*test*/) 21 | { 22 | } 23 | 24 | void TestReporterStdout::ReportTestFinish(TestDetails const& /*test*/, float) 25 | { 26 | } 27 | 28 | void TestReporterStdout::ReportSummary(int const totalTestCount, int const failedTestCount, 29 | int const failureCount, float secondsElapsed) 30 | { 31 | using namespace std; 32 | 33 | if (failureCount > 0) 34 | printf("FAILURE: %d out of %d tests failed (%d failures).\n", failedTestCount, totalTestCount, failureCount); 35 | else 36 | printf("Success: %d tests passed.\n", totalTestCount); 37 | 38 | printf("Test time: %.2f seconds.\n", secondsElapsed); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /UnitTest++/src/Win32/TimeHelpers.cpp: -------------------------------------------------------------------------------- 1 | #include "TimeHelpers.h" 2 | #include 3 | 4 | namespace UnitTest { 5 | 6 | Timer::Timer() 7 | : m_startTime(0) 8 | , m_threadHandle(::GetCurrentThread()) 9 | { 10 | #if defined(_MSC_VER) && (_MSC_VER == 1200) // VC6 doesn't have DWORD_PTR? 11 | typedef unsigned long DWORD_PTR; 12 | #endif 13 | 14 | DWORD_PTR systemMask; 15 | ::GetProcessAffinityMask(GetCurrentProcess(), &m_processAffinityMask, &systemMask); 16 | 17 | ::SetThreadAffinityMask(m_threadHandle, 1); 18 | ::QueryPerformanceFrequency(reinterpret_cast< LARGE_INTEGER* >(&m_frequency)); 19 | ::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask); 20 | } 21 | 22 | void Timer::Start() 23 | { 24 | m_startTime = GetTime(); 25 | } 26 | 27 | int Timer::GetTimeInMs() const 28 | { 29 | __int64 const elapsedTime = GetTime() - m_startTime; 30 | double const seconds = double(elapsedTime) / double(m_frequency); 31 | return int(seconds * 1000.0f); 32 | } 33 | 34 | __int64 Timer::GetTime() const 35 | { 36 | LARGE_INTEGER curTime; 37 | ::SetThreadAffinityMask(m_threadHandle, 1); 38 | ::QueryPerformanceCounter(&curTime); 39 | ::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask); 40 | return curTime.QuadPart; 41 | } 42 | 43 | 44 | 45 | void TimeHelpers::SleepMs(int const ms) 46 | { 47 | ::Sleep(ms); 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /UnitTest++/src/TestRunner.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTRUNNER_H 2 | #define UNITTEST_TESTRUNNER_H 3 | 4 | #include "Test.h" 5 | #include "TestList.h" 6 | #include "CurrentTest.h" 7 | 8 | namespace UnitTest { 9 | 10 | class TestReporter; 11 | class TestResults; 12 | class Timer; 13 | 14 | int RunAllTests(); 15 | 16 | struct True 17 | { 18 | bool operator()(const Test* const) const 19 | { 20 | return true; 21 | } 22 | }; 23 | 24 | class TestRunner 25 | { 26 | public: 27 | explicit TestRunner(TestReporter& reporter); 28 | ~TestRunner(); 29 | 30 | template 31 | int RunTestsIf(TestList const& list, char const* suiteName, 32 | const Predicate& predicate, int maxTestTimeInMs) const 33 | { 34 | Test* curTest = list.GetHead(); 35 | 36 | while (curTest != 0) 37 | { 38 | if (IsTestInSuite(curTest,suiteName) && predicate(curTest)) 39 | { 40 | RunTest(m_result, curTest, maxTestTimeInMs); 41 | } 42 | 43 | curTest = curTest->next; 44 | } 45 | 46 | return Finish(); 47 | } 48 | 49 | private: 50 | TestReporter* m_reporter; 51 | TestResults* m_result; 52 | Timer* m_timer; 53 | 54 | int Finish() const; 55 | bool IsTestInSuite(const Test* const curTest, char const* suiteName) const; 56 | void RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const; 57 | }; 58 | 59 | } 60 | 61 | #endif 62 | -------------------------------------------------------------------------------- /UnitTest++/src/Checks.cpp: -------------------------------------------------------------------------------- 1 | #include "Checks.h" 2 | #include 3 | 4 | namespace UnitTest { 5 | 6 | namespace { 7 | 8 | void CheckStringsEqual(TestResults& results, char const* expected, char const* actual, 9 | TestDetails const& details) 10 | { 11 | using namespace std; 12 | 13 | if (strcmp(expected, actual)) 14 | { 15 | UnitTest::MemoryOutStream stream; 16 | stream << "Expected " << expected << " but was " << actual; 17 | 18 | results.OnTestFailure(details, stream.GetText()); 19 | } 20 | } 21 | 22 | } 23 | 24 | 25 | void CheckEqual(TestResults& results, char const* expected, char const* actual, 26 | TestDetails const& details) 27 | { 28 | CheckStringsEqual(results, expected, actual, details); 29 | } 30 | 31 | void CheckEqual(TestResults& results, char* expected, char* actual, 32 | TestDetails const& details) 33 | { 34 | CheckStringsEqual(results, expected, actual, details); 35 | } 36 | 37 | void CheckEqual(TestResults& results, char* expected, char const* actual, 38 | TestDetails const& details) 39 | { 40 | CheckStringsEqual(results, expected, actual, details); 41 | } 42 | 43 | void CheckEqual(TestResults& results, char const* expected, char* actual, 44 | TestDetails const& details) 45 | { 46 | CheckStringsEqual(results, expected, actual, details); 47 | } 48 | 49 | 50 | } 51 | -------------------------------------------------------------------------------- /UnitTest++/src/TestResults.cpp: -------------------------------------------------------------------------------- 1 | #include "TestResults.h" 2 | #include "TestReporter.h" 3 | 4 | #include "TestDetails.h" 5 | 6 | namespace UnitTest { 7 | 8 | TestResults::TestResults(TestReporter* testReporter) 9 | : m_testReporter(testReporter) 10 | , m_totalTestCount(0) 11 | , m_failedTestCount(0) 12 | , m_failureCount(0) 13 | , m_currentTestFailed(false) 14 | { 15 | } 16 | 17 | void TestResults::OnTestStart(TestDetails const& test) 18 | { 19 | ++m_totalTestCount; 20 | m_currentTestFailed = false; 21 | if (m_testReporter) 22 | m_testReporter->ReportTestStart(test); 23 | } 24 | 25 | void TestResults::OnTestFailure(TestDetails const& test, char const* failure) 26 | { 27 | ++m_failureCount; 28 | if (!m_currentTestFailed) 29 | { 30 | ++m_failedTestCount; 31 | m_currentTestFailed = true; 32 | } 33 | 34 | if (m_testReporter) 35 | m_testReporter->ReportFailure(test, failure); 36 | } 37 | 38 | void TestResults::OnTestFinish(TestDetails const& test, float secondsElapsed) 39 | { 40 | if (m_testReporter) 41 | m_testReporter->ReportTestFinish(test, secondsElapsed); 42 | } 43 | 44 | int TestResults::GetTotalTestCount() const 45 | { 46 | return m_totalTestCount; 47 | } 48 | 49 | int TestResults::GetFailedTestCount() const 50 | { 51 | return m_failedTestCount; 52 | } 53 | 54 | int TestResults::GetFailureCount() const 55 | { 56 | return m_failureCount; 57 | } 58 | 59 | 60 | } 61 | -------------------------------------------------------------------------------- /UnitTest++/src/MemoryOutStream.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_MEMORYOUTSTREAM_H 2 | #define UNITTEST_MEMORYOUTSTREAM_H 3 | 4 | #include "Config.h" 5 | 6 | #ifndef UNITTEST_USE_CUSTOM_STREAMS 7 | 8 | #include 9 | 10 | namespace UnitTest 11 | { 12 | 13 | class MemoryOutStream : public std::ostringstream 14 | { 15 | public: 16 | MemoryOutStream() {} 17 | char const* GetText() const; 18 | 19 | private: 20 | MemoryOutStream(MemoryOutStream const&); 21 | void operator =(MemoryOutStream const&); 22 | 23 | mutable std::string m_text; 24 | }; 25 | 26 | } 27 | 28 | #else 29 | 30 | #include 31 | 32 | namespace UnitTest 33 | { 34 | 35 | class MemoryOutStream 36 | { 37 | public: 38 | explicit MemoryOutStream(int const size = 256); 39 | ~MemoryOutStream(); 40 | 41 | char const* GetText() const; 42 | 43 | MemoryOutStream& operator << (char const* txt); 44 | MemoryOutStream& operator << (int n); 45 | MemoryOutStream& operator << (long n); 46 | MemoryOutStream& operator << (unsigned long n); 47 | MemoryOutStream& operator << (float f); 48 | MemoryOutStream& operator << (double d); 49 | MemoryOutStream& operator << (void const* p); 50 | MemoryOutStream& operator << (unsigned int s); 51 | 52 | enum { GROW_CHUNK_SIZE = 32 }; 53 | int GetCapacity() const; 54 | 55 | private: 56 | void operator= (MemoryOutStream const&); 57 | void GrowBuffer(int capacity); 58 | 59 | int m_capacity; 60 | char* m_buffer; 61 | }; 62 | 63 | } 64 | 65 | #endif 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /easySQLite/SqlTable.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2010 Piotr Zagawa 3 | // 4 | // Released under BSD License 5 | // 6 | 7 | #pragma once 8 | 9 | #include "sqlite3.h" 10 | #include "SqlCommon.h" 11 | #include "SqlFieldSet.h" 12 | #include "SqlRecordSet.h" 13 | 14 | 15 | namespace sql 16 | { 17 | 18 | class Table 19 | { 20 | private: 21 | sqlite3* _db; 22 | string _tableName; 23 | RecordSet _recordset; 24 | 25 | public: 26 | Table(sqlite3* db, string tableName, Field* definition); 27 | Table(sqlite3* db, string tableName, FieldSet* fields); 28 | 29 | public: 30 | string name(); 31 | string getDefinition(); 32 | string toString(); 33 | string errMsg(); 34 | FieldSet* fields(); 35 | sqlite3* getHandle(); 36 | 37 | public: 38 | bool create(); 39 | bool exists(); 40 | bool remove(); 41 | bool truncate(); 42 | 43 | public: 44 | bool open(); 45 | bool open(string whereCondition); 46 | bool open(string whereCondition, string sortBy); 47 | bool query(string queryStr); 48 | int totalRecordCount(); 49 | 50 | public: 51 | int recordCount(); 52 | Record* getRecord(int record_index); 53 | Record* getTopRecord(); 54 | Record* getRecordByKeyId(integer keyId); 55 | 56 | public: 57 | bool addRecord(Record* record); 58 | bool updateRecord(Record* record); 59 | bool deleteRecords(string whereCondition); 60 | bool copyRecords(Table& source); 61 | bool backup(Table& source); 62 | 63 | public: 64 | static Table* createFromDefinition(sqlite3* db, string tableName, string fieldsDefinition); 65 | 66 | }; 67 | 68 | 69 | //sql eof 70 | }; 71 | -------------------------------------------------------------------------------- /UnitTest++/UnitTest++.vsnet2005.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 9.00 3 | # Visual Studio 2005 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "UnitTest++.vsnet2005", "UnitTest++.vsnet2005.vcproj", "{64A4FEFE-0461-4E95-8CC1-91EF5F57DBC6}" 5 | EndProject 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TestUnitTest++.vsnet2005", "TestUnitTest++.vsnet2005.vcproj", "{9CCC3439-309E-4E85-B3B8-CE704D385D48}" 7 | ProjectSection(ProjectDependencies) = postProject 8 | {64A4FEFE-0461-4E95-8CC1-91EF5F57DBC6} = {64A4FEFE-0461-4E95-8CC1-91EF5F57DBC6} 9 | EndProjectSection 10 | EndProject 11 | Global 12 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 13 | Debug|Win32 = Debug|Win32 14 | Release|Win32 = Release|Win32 15 | EndGlobalSection 16 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 17 | {64A4FEFE-0461-4E95-8CC1-91EF5F57DBC6}.Debug|Win32.ActiveCfg = Debug|Win32 18 | {64A4FEFE-0461-4E95-8CC1-91EF5F57DBC6}.Debug|Win32.Build.0 = Debug|Win32 19 | {64A4FEFE-0461-4E95-8CC1-91EF5F57DBC6}.Release|Win32.ActiveCfg = Release|Win32 20 | {64A4FEFE-0461-4E95-8CC1-91EF5F57DBC6}.Release|Win32.Build.0 = Release|Win32 21 | {9CCC3439-309E-4E85-B3B8-CE704D385D48}.Debug|Win32.ActiveCfg = Debug|Win32 22 | {9CCC3439-309E-4E85-B3B8-CE704D385D48}.Debug|Win32.Build.0 = Debug|Win32 23 | {9CCC3439-309E-4E85-B3B8-CE704D385D48}.Release|Win32.ActiveCfg = Release|Win32 24 | {9CCC3439-309E-4E85-B3B8-CE704D385D48}.Release|Win32.Build.0 = Release|Win32 25 | EndGlobalSection 26 | GlobalSection(SolutionProperties) = preSolution 27 | HideSolutionNode = FALSE 28 | EndGlobalSection 29 | EndGlobal 30 | -------------------------------------------------------------------------------- /_LICENSE.TXT: -------------------------------------------------------------------------------- 1 | 2 | Copyright (c) 2013, Dawid Drozd 3 | Copyright (c) 2013, Artcator Inc. 4 | Copyright (c) 2010, Piotr Zagawa 5 | All rights reserved. 6 | 7 | Redistribution and use in source and binary forms, with or without modification, 8 | are permitted provided that the following conditions are met: 9 | 10 | * Redistributions of source code must retain the above copyright notice, this list 11 | of conditions and the following disclaimer. 12 | 13 | * Redistributions in binary form must reproduce the above copyright notice, this list 14 | of conditions and the following disclaimer in the documentation and/or other materials 15 | provided with the distribution. 16 | 17 | * Neither the name of the "Piotr Zagawa" nor the names of its contributors may be used 18 | to endorse or promote products derived from this software without specific prior written 19 | permission. 20 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 22 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 24 | SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 26 | OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 28 | TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 29 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /easySQLite/SqlDatabase.cpp: -------------------------------------------------------------------------------- 1 | #include "SqlDatabase.h" 2 | #include "SqlRecordSet.h" 3 | #include 4 | 5 | 6 | namespace sql 7 | { 8 | 9 | Database::Database(void) 10 | { 11 | _db = NULL; 12 | _result_open = SQLITE_ERROR; 13 | 14 | close(); 15 | } 16 | 17 | Database::~Database(void) 18 | { 19 | close(); 20 | } 21 | 22 | sqlite3* Database::getHandle() 23 | { 24 | return _db; 25 | } 26 | 27 | string Database::errMsg() 28 | { 29 | return _err_msg; 30 | } 31 | 32 | void Database::close() 33 | { 34 | if (_db) 35 | { 36 | sqlite3_close(_db); 37 | _db = NULL; 38 | _err_msg.clear(); 39 | _result_open = SQLITE_ERROR; 40 | } 41 | } 42 | 43 | bool Database::isOpen() 44 | { 45 | return (_result_open == SQLITE_OK); 46 | } 47 | 48 | bool Database::open(string filename) 49 | { 50 | close(); 51 | 52 | _result_open = sqlite3_open(filename.c_str(), &_db); 53 | 54 | if (isOpen()) 55 | { 56 | return true; 57 | } else { 58 | _err_msg = sqlite3_errmsg(_db); 59 | } 60 | 61 | THROW_EXCEPTION("Database::open: " + errMsg()) 62 | 63 | return false; 64 | } 65 | 66 | bool Database::transactionBegin() 67 | { 68 | RecordSet rs(_db); 69 | 70 | if (rs.query("BEGIN TRANSACTION")) 71 | return true; 72 | 73 | return false; 74 | } 75 | 76 | bool Database::transactionCommit() 77 | { 78 | RecordSet rs(_db); 79 | 80 | if (rs.query("COMMIT TRANSACTION")) 81 | return true; 82 | 83 | return false; 84 | } 85 | 86 | bool Database::transactionRollback() 87 | { 88 | RecordSet rs(_db); 89 | 90 | if (rs.query("ROLLBACK TRANSACTION")) 91 | return true; 92 | 93 | return false; 94 | } 95 | 96 | 97 | //sql eof 98 | }; 99 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestTimeConstraint.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | #include "../TestResults.h" 3 | #include "../TimeHelpers.h" 4 | #include "RecordingReporter.h" 5 | #include "ScopedCurrentTest.h" 6 | 7 | using namespace UnitTest; 8 | 9 | namespace 10 | { 11 | 12 | TEST(TimeConstraintSucceedsWithFastTest) 13 | { 14 | TestResults result; 15 | { 16 | ScopedCurrentTest scopedResult(result); 17 | TimeConstraint t(200, TestDetails("", "", "", 0)); 18 | TimeHelpers::SleepMs(5); 19 | } 20 | CHECK_EQUAL(0, result.GetFailureCount()); 21 | } 22 | 23 | TEST(TimeConstraintFailsWithSlowTest) 24 | { 25 | TestResults result; 26 | { 27 | ScopedCurrentTest scopedResult(result); 28 | TimeConstraint t(10, TestDetails("", "", "", 0)); 29 | TimeHelpers::SleepMs(20); 30 | } 31 | CHECK_EQUAL(1, result.GetFailureCount()); 32 | } 33 | 34 | TEST(TimeConstraintFailureIncludesCorrectData) 35 | { 36 | RecordingReporter reporter; 37 | TestResults result(&reporter); 38 | { 39 | ScopedCurrentTest scopedResult(result); 40 | 41 | TestDetails const details("testname", "suitename", "filename", 10); 42 | TimeConstraint t(10, details); 43 | TimeHelpers::SleepMs(20); 44 | } 45 | 46 | using namespace std; 47 | 48 | CHECK(strstr(reporter.lastFailedFile, "filename")); 49 | CHECK_EQUAL(10, reporter.lastFailedLine); 50 | CHECK(strstr(reporter.lastFailedTest, "testname")); 51 | } 52 | 53 | TEST(TimeConstraintFailureIncludesTimeoutInformation) 54 | { 55 | RecordingReporter reporter; 56 | TestResults result(&reporter); 57 | { 58 | ScopedCurrentTest scopedResult(result); 59 | TimeConstraint t(10, TestDetails("", "", "", 0)); 60 | TimeHelpers::SleepMs(20); 61 | } 62 | 63 | using namespace std; 64 | 65 | CHECK(strstr(reporter.lastFailedMessage, "ime constraint")); 66 | CHECK(strstr(reporter.lastFailedMessage, "under 10ms")); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /easySQLite/SqlRecord.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2010 Piotr Zagawa 3 | // 4 | // Released under BSD License 5 | // 6 | 7 | #pragma once 8 | 9 | #include 10 | #include "SqlCommon.h" 11 | #include "SqlValue.h" 12 | #include "SqlFieldSet.h" 13 | 14 | 15 | namespace sql 16 | { 17 | 18 | class Record 19 | { 20 | private: 21 | FieldSet* _fields; 22 | std::vector _values; 23 | 24 | public: 25 | Record(FieldSet* fields); 26 | Record(Record* record); 27 | Record(const Record& record); 28 | 29 | private: 30 | friend class RecordSet; 31 | 32 | void initColumnCount(int columns); 33 | void initColumnValue(int column_index, char* value, field_type type); 34 | 35 | public: 36 | int columnCount(); 37 | Value* getValue(int column_index); 38 | Value* getValue(string fieldName); 39 | Value* getValue(const Field& field); 40 | Value* getKeyIdValue(); 41 | Field* fieldByName(string fieldName); 42 | FieldSet* fields(); 43 | string toString(); 44 | string toSql(); 45 | bool equalsColumnValue(Record* record, string fieldName); 46 | bool equalsValues(Record* record); 47 | 48 | public: 49 | string toSqlInsert(string tableName); 50 | string toSqlUpdate(string tableName); 51 | 52 | public: 53 | void setNull(int index); 54 | void setString(int index, string value); 55 | void setInteger(int index, integer value); 56 | void setDouble(int index, double value); 57 | void setBool(int index, bool value); 58 | void setTime(int index, time value); 59 | 60 | public: 61 | void setNull(string fieldName); 62 | void setString(string fieldName, string value); 63 | void setInteger(string fieldName, integer value); 64 | void setDouble(string fieldName, double value); 65 | void setBool(string fieldName, bool value); 66 | void setTime(string fieldName, time value); 67 | 68 | public: 69 | void setNull(Field& field); 70 | void setString(Field& field, string value); 71 | void setInteger(Field& field, integer value); 72 | void setDouble(Field& field, double value); 73 | void setBool(Field& field, bool value); 74 | void setTime(Field& field, time value); 75 | }; 76 | 77 | 78 | //sql eof 79 | }; 80 | -------------------------------------------------------------------------------- /Tests/TestSqlField.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++/src/UnitTest++.h" 2 | #include "../easySQLite/SqlField.h" 3 | 4 | 5 | namespace Tests 6 | { 7 | 8 | using namespace sql; 9 | 10 | 11 | TEST(FieldKeySetup) 12 | { 13 | Field f(FIELD_KEY); 14 | 15 | CHECK_EQUAL("_ID INTEGER PRIMARY KEY", f.getDefinition()); 16 | CHECK_EQUAL("_ID", f.getName()); 17 | CHECK_EQUAL(type_int, f.getType()); 18 | CHECK_EQUAL("INTEGER", f.getTypeStr()); 19 | CHECK(!f.isEndingField()); 20 | CHECK(f.isKeyIdField()); 21 | CHECK(f.isPrimaryKey()); 22 | } 23 | 24 | TEST(FieldEndSetup) 25 | { 26 | Field f(DEFINITION_END); 27 | 28 | CHECK_EQUAL("", f.getDefinition()); 29 | CHECK_EQUAL("", f.getName()); 30 | CHECK_EQUAL(type_undefined, f.getType()); 31 | CHECK_EQUAL("", f.getTypeStr()); 32 | CHECK(f.isEndingField()); 33 | } 34 | 35 | TEST(FieldsDefinition) 36 | { 37 | Field def[] = 38 | { 39 | Field(FIELD_KEY), 40 | Field("name", type_text, flag_not_null), 41 | Field("valueInt", type_int), 42 | Field("valueDbl", type_float), 43 | Field("valueTxt", type_text), 44 | Field("valueBol", type_bool, flag_not_null), 45 | Field("valueTme", type_time), 46 | Field(DEFINITION_END), 47 | }; 48 | 49 | CHECK_EQUAL("name TEXT NOT NULL", def[1].getDefinition()); 50 | CHECK_EQUAL("valueInt INTEGER", def[2].getDefinition()); 51 | CHECK_EQUAL("valueDbl REAL", def[3].getDefinition()); 52 | CHECK_EQUAL("valueTxt TEXT", def[4].getDefinition()); 53 | CHECK_EQUAL("valueBol INTEGER NOT NULL", def[5].getDefinition()); 54 | CHECK_EQUAL("valueTme INTEGER", def[6].getDefinition()); 55 | } 56 | 57 | TEST(FieldCreation) 58 | { 59 | Field f(FIELD_KEY); 60 | 61 | CHECK_EQUAL("_ID INTEGER PRIMARY KEY", f.getDefinition()); 62 | 63 | Field f1(f); 64 | 65 | CHECK_EQUAL("_ID INTEGER PRIMARY KEY", f1.getDefinition()); 66 | } 67 | 68 | TEST(FieldCreationFromString) 69 | { 70 | Field* f = Field::createFromDefinition("_ID INTEGER PRIMARY KEY"); 71 | 72 | CHECK(f != NULL); 73 | 74 | if (f) 75 | { 76 | CHECK_EQUAL("_ID INTEGER PRIMARY KEY", f->getDefinition()); 77 | delete f; 78 | } 79 | } 80 | 81 | 82 | //eofn 83 | }; 84 | -------------------------------------------------------------------------------- /UnitTest++/src/TestRunner.cpp: -------------------------------------------------------------------------------- 1 | #include "TestRunner.h" 2 | #include "TestResults.h" 3 | #include "TestReporter.h" 4 | #include "TestReporterStdout.h" 5 | #include "TimeHelpers.h" 6 | #include "MemoryOutStream.h" 7 | 8 | #include 9 | 10 | 11 | namespace UnitTest { 12 | 13 | int RunAllTests() 14 | { 15 | TestReporterStdout reporter; 16 | TestRunner runner(reporter); 17 | return runner.RunTestsIf(Test::GetTestList(), NULL, True(), 0); 18 | } 19 | 20 | 21 | TestRunner::TestRunner(TestReporter& reporter) 22 | : m_reporter(&reporter) 23 | , m_result(new TestResults(&reporter)) 24 | , m_timer(new Timer) 25 | { 26 | m_timer->Start(); 27 | } 28 | 29 | TestRunner::~TestRunner() 30 | { 31 | delete m_result; 32 | delete m_timer; 33 | } 34 | 35 | int TestRunner::Finish() const 36 | { 37 | float const secondsElapsed = m_timer->GetTimeInMs() / 1000.0f; 38 | m_reporter->ReportSummary(m_result->GetTotalTestCount(), 39 | m_result->GetFailedTestCount(), 40 | m_result->GetFailureCount(), 41 | secondsElapsed); 42 | 43 | return m_result->GetFailureCount(); 44 | } 45 | 46 | bool TestRunner::IsTestInSuite(const Test* const curTest, char const* suiteName) const 47 | { 48 | using namespace std; 49 | return (suiteName == NULL) || !strcmp(curTest->m_details.suiteName, suiteName); 50 | } 51 | 52 | void TestRunner::RunTest(TestResults* const result, Test* const curTest, int const maxTestTimeInMs) const 53 | { 54 | CurrentTest::Results() = result; 55 | 56 | Timer testTimer; 57 | testTimer.Start(); 58 | 59 | result->OnTestStart(curTest->m_details); 60 | 61 | curTest->Run(); 62 | 63 | int const testTimeInMs = testTimer.GetTimeInMs(); 64 | if (maxTestTimeInMs > 0 && testTimeInMs > maxTestTimeInMs && !curTest->m_timeConstraintExempt) 65 | { 66 | MemoryOutStream stream; 67 | stream << "Global time constraint failed. Expected under " << maxTestTimeInMs << 68 | "ms but took " << testTimeInMs << "ms."; 69 | 70 | result->OnTestFailure(curTest->m_details, stream.GetText()); 71 | } 72 | 73 | result->OnTestFinish(curTest->m_details, testTimeInMs/1000.0f); 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /Tests/TestSqlDatabase.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++/src/UnitTest++.h" 2 | #include "../easySQLite/SqlRecordSet.h" 3 | #include "../easySQLite/SqlDatabase.h" 4 | #include "../easySQLite/SqlTable.h" 5 | 6 | 7 | namespace Tests 8 | { 9 | 10 | using namespace sql; 11 | 12 | 13 | static Field* definition() 14 | { 15 | static Field def[] = 16 | { 17 | Field(FIELD_KEY), 18 | Field("name", type_text, flag_not_null), 19 | Field("valueInt", type_int), 20 | Field("valueDbl", type_float), 21 | Field("valueTxt", type_text), 22 | Field("valueBol", type_bool, flag_not_null), 23 | Field("valueTme", type_time), 24 | Field(DEFINITION_END), 25 | }; 26 | 27 | return &def[0]; 28 | } 29 | 30 | static void setValues(Record& r) 31 | { 32 | r.setString("name", "test_text1"); 33 | r.setInteger("valueInt", 890); 34 | r.setDouble("valueDbl", 0.345); 35 | r.setString("valueTxt", "test_text2"); 36 | r.setBool("valueBol", true); 37 | r.setTime("valueTme", 0); 38 | } 39 | 40 | TEST(Database) 41 | { 42 | sql::Database db; 43 | 44 | try 45 | { 46 | db.open("UnitTests.db"); 47 | 48 | Table tb(db.getHandle(), "test", definition()); 49 | 50 | if (tb.exists()) 51 | tb.remove(); 52 | 53 | tb.create(); 54 | 55 | tb.open(); 56 | 57 | CHECK_EQUAL(0, tb.recordCount()); 58 | 59 | Record r(tb.fields()); 60 | setValues(r); 61 | 62 | //test transaction commit 63 | tb.truncate(); 64 | CHECK_EQUAL(0, tb.totalRecordCount()); 65 | 66 | if (db.transactionBegin()) 67 | { 68 | tb.addRecord(&r); 69 | tb.addRecord(&r); 70 | tb.addRecord(&r); 71 | 72 | db.transactionCommit(); 73 | CHECK_EQUAL(3, tb.totalRecordCount()); 74 | 75 | } else { 76 | CHECK_EQUAL("transaction begin fail", ""); 77 | } 78 | 79 | //test transaction rollback 80 | tb.truncate(); 81 | CHECK_EQUAL(0, tb.totalRecordCount()); 82 | 83 | if (db.transactionBegin()) 84 | { 85 | tb.addRecord(&r); 86 | tb.addRecord(&r); 87 | tb.addRecord(&r); 88 | 89 | db.transactionRollback(); 90 | CHECK_EQUAL(0, tb.totalRecordCount()); 91 | 92 | } else { 93 | CHECK_EQUAL("transaction begin fail", ""); 94 | } 95 | 96 | } catch (Exception e) { 97 | CHECK_EQUAL("*", e.msg()); 98 | } 99 | } 100 | 101 | 102 | //eofn 103 | }; 104 | -------------------------------------------------------------------------------- /UnitTest++/README: -------------------------------------------------------------------------------- 1 | UnitTest++ README 2 | Version: v1.4 3 | Last update: 2008-10-30 4 | 5 | UnitTest++ is free software. You may copy, distribute, and modify it under 6 | the terms of the License contained in the file COPYING distributed 7 | with this package. This license is the same as the MIT/X Consortium 8 | license. 9 | 10 | See src/tests/TestUnitTest++.cpp for usage. 11 | 12 | Authors: 13 | Noel Llopis (llopis@convexhull.com) 14 | Charles Nicholson (charles.nicholson@gmail.com) 15 | 16 | Contributors: 17 | Jim Tilander 18 | Kim Grasman 19 | Jonathan Jansson 20 | Dirck Blaskey 21 | Rory Driscoll 22 | Dan Lind 23 | Matt Kimmel -- Submitted with permission from Blue Fang Games 24 | Anthony Moralez 25 | Jeff Dixon 26 | Randy Coulman 27 | Lieven van der Heide 28 | 29 | Release notes: 30 | -------------- 31 | Version 1.4 (2008-10-30) 32 | - CHECK macros work at arbitrary stack depth from inside TESTs. 33 | - Remove obsolete TEST_UTILITY macros 34 | - Predicated test execution (via TestRunner::RunTestsIf) 35 | - Better exception handling for fixture ctors/dtors. 36 | - VC6/7/8/9 support 37 | 38 | Version 1.3 (2007-4-22) 39 | - Removed dynamic memory allocations (other than streams) 40 | - MinGW support 41 | - Consistent (native) line endings 42 | - Minor bug fixing 43 | 44 | Version 1.2 (2006-10-29) 45 | - First pass at documentation. 46 | - More detailed error crash catching in fixtures. 47 | - Standard streams used for printing objects under check. This should allow the 48 | use of standard class types such as std::string or other custom classes with 49 | stream operators to ostream. 50 | - Standard streams can be optionally compiled off by defining UNITTEST_USE_CUSTOM_STREAMS 51 | in Config.h 52 | - Added named test suites 53 | - Added CHECK_ARRAY2D_CLOSE 54 | - Posix library name is libUnitTest++.a now 55 | - Floating point numbers are postfixed with f in the failure reports 56 | 57 | Version 1.1 (2006-04-18) 58 | - CHECK macros do not have side effects even if one of the parameters changes state 59 | - Removed CHECK_ARRAY_EQUAL (too similar to CHECK_ARRAY_CLOSE) 60 | - Added local and global time constraints 61 | - Removed dependencies on strstream 62 | - Improved Posix signal to exception translator 63 | - Failing tests are added to Visual Studio's error list 64 | - Fixed Visual Studio projects to work with spaces in directories 65 | 66 | Version 1.0 (2006-03-15) 67 | - Initial release 68 | 69 | -------------------------------------------------------------------------------- /proj.linux/Makefile: -------------------------------------------------------------------------------- 1 | TARGET=libeasysqlite.a 2 | 3 | INCLUDES = \ 4 | -I../easySQLite/ 5 | 6 | SOURCES = \ 7 | ../easySQLite/SqlValue.cpp \ 8 | ../easySQLite/SqlCommon.cpp \ 9 | ../easySQLite/SHA1.cpp \ 10 | ../easySQLite/SqlField.cpp \ 11 | ../easySQLite/SqlFieldSet.cpp \ 12 | ../easySQLite/SqlRecord.cpp \ 13 | ../easySQLite/SqlTable.cpp \ 14 | ../easySQLite/SqlRecordSet.cpp \ 15 | ../easySQLite/SqlDatabase.cpp \ 16 | ../easySQLite/sqlite3.c 17 | 18 | 19 | all: 20 | 21 | CC = gcc 22 | CXX = g++ 23 | CCFLAGS += -MMD -Wall -Werror -fPIC -Wno-unknown-pragmas 24 | CXXFLAGS += -MMD -Wall -Werror -fPIC -Wno-unknown-pragmas 25 | ARFLAGS = cr 26 | 27 | DEFINES += -DLINUX 28 | 29 | THIS_MAKEFILE := $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST)) 30 | 31 | OBJ_DIR ?= obj 32 | LIB_DIR = ./lib/linux 33 | BIN_DIR = bin 34 | 35 | ifeq ($(DEBUG), 1) 36 | CCFLAGS += -g3 -O0 37 | CXXFLAGS += -g3 -O0 38 | DEFINES += -D_DEBUG -DCOCOS2D_DEBUG=1 39 | OBJ_DIR := $(OBJ_DIR)/debug 40 | LIB_DIR := $(LIB_DIR)/debug 41 | BIN_DIR := $(BIN_DIR)/debug 42 | else 43 | CCFLAGS += -O3 44 | CXXFLAGS += -O3 45 | DEFINES += -DNDEBUG 46 | OBJ_DIR := $(OBJ_DIR)/release 47 | LIB_DIR := $(LIB_DIR)/release 48 | BIN_DIR := $(BIN_DIR)/release 49 | endif 50 | 51 | ifndef V 52 | LOG_CC = @echo " CC $@"; 53 | LOG_CXX = @echo " CXX $@"; 54 | LOG_AR = @echo " AR $@"; 55 | LOG_LINK = @echo " LINK $@"; 56 | endif 57 | 58 | OBJECTS := $(SOURCES:.cpp=.o) 59 | OBJECTS := $(OBJECTS:.c=.o) 60 | OBJECTS := $(subst ../,,$(OBJECTS)) 61 | OBJECTS := $(subst $(COCOS_ROOT),,$(OBJECTS)) 62 | OBJECTS := $(addprefix $(OBJ_DIR)/, $(OBJECTS)) 63 | DEPS = $(OBJECTS:.o=.d) 64 | CORE_MAKEFILE_LIST := $(MAKEFILE_LIST) 65 | -include $(DEPS) 66 | 67 | 68 | LIBS = -lrt -lz 69 | 70 | .PHONY: all clean 71 | 72 | # If the parent Makefile defines $(EXECUTABLE) then define this as the target 73 | # and create a 'make run' rule to run the app. 74 | ifdef EXECUTABLE 75 | TARGET := $(BIN_DIR)/$(EXECUTABLE) 76 | 77 | all: $(TARGET) 78 | 79 | run: $(TARGET) 80 | cd $(dir $^) && ./$(notdir $^) 81 | 82 | .PHONY: run 83 | endif 84 | 85 | 86 | TARGET := $(LIB_DIR)/$(TARGET) 87 | 88 | all: $(TARGET) 89 | 90 | $(TARGET): $(OBJECTS) $(CORE_MAKEFILE_LIST) 91 | @mkdir -p $(@D) 92 | $(LOG_AR)$(AR) $(ARFLAGS) $@ $(OBJECTS) 93 | 94 | $(OBJ_DIR)/%.o: ../%.cpp $(CORE_MAKEFILE_LIST) 95 | @mkdir -p $(@D) 96 | $(LOG_CXX)$(CXX) $(CXXFLAGS) $(INCLUDES) $(DEFINES) -c $< -o $@ 97 | 98 | $(OBJ_DIR)/%.o: ../%.c $(CORE_MAKEFILE_LIST) 99 | @mkdir -p $(@D) 100 | $(LOG_CC)$(CC) $(CCFLAGS) $(INCLUDES) $(DEFINES) -c $< -o $@ 101 | 102 | clean: 103 | rm -R -f ./lib 104 | rm -R -f ./obj 105 | -------------------------------------------------------------------------------- /UnitTest++/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CXXFLAGS ?= -g -Wall -W -ansi # -pedantic 3 | LDFLAGS ?= 4 | SED = sed 5 | MV = mv 6 | RM = rm 7 | 8 | .SUFFIXES: .o .cpp 9 | 10 | lib = libUnitTest++.a 11 | test = TestUnitTest++ 12 | 13 | src = src/AssertException.cpp \ 14 | src/Test.cpp \ 15 | src/Checks.cpp \ 16 | src/TestRunner.cpp \ 17 | src/TestResults.cpp \ 18 | src/TestReporter.cpp \ 19 | src/TestReporterStdout.cpp \ 20 | src/ReportAssert.cpp \ 21 | src/TestList.cpp \ 22 | src/TimeConstraint.cpp \ 23 | src/TestDetails.cpp \ 24 | src/MemoryOutStream.cpp \ 25 | src/DeferredTestReporter.cpp \ 26 | src/DeferredTestResult.cpp \ 27 | src/XmlTestReporter.cpp \ 28 | src/CurrentTest.cpp 29 | 30 | ifeq ($(MSYSTEM), MINGW32) 31 | src += src/Win32/TimeHelpers.cpp 32 | else 33 | src += src/Posix/SignalTranslator.cpp \ 34 | src/Posix/TimeHelpers.cpp 35 | endif 36 | 37 | test_src = src/tests/Main.cpp \ 38 | src/tests/TestAssertHandler.cpp \ 39 | src/tests/TestChecks.cpp \ 40 | src/tests/TestUnitTest++.cpp \ 41 | src/tests/TestTest.cpp \ 42 | src/tests/TestTestResults.cpp \ 43 | src/tests/TestTestRunner.cpp \ 44 | src/tests/TestCheckMacros.cpp \ 45 | src/tests/TestTestList.cpp \ 46 | src/tests/TestTestMacros.cpp \ 47 | src/tests/TestTimeConstraint.cpp \ 48 | src/tests/TestTimeConstraintMacro.cpp \ 49 | src/tests/TestMemoryOutStream.cpp \ 50 | src/tests/TestDeferredTestReporter.cpp \ 51 | src/tests/TestXmlTestReporter.cpp \ 52 | src/tests/TestCurrentTest.cpp 53 | 54 | objects = $(patsubst %.cpp, %.o, $(src)) 55 | test_objects = $(patsubst %.cpp, %.o, $(test_src)) 56 | dependencies = $(subst .o,.d,$(objects)) 57 | test_dependencies = $(subst .o,.d,$(test_objects)) 58 | 59 | define make-depend 60 | $(CXX) $(CXXFLAGS) -M $1 | \ 61 | $(SED) -e 's,\($(notdir $2)\) *:,$(dir $2)\1: ,' > $3.tmp 62 | $(SED) -e 's/#.*//' \ 63 | -e 's/^[^:]*: *//' \ 64 | -e 's/ *\\$$//' \ 65 | -e '/^$$/ d' \ 66 | -e 's/$$/ :/' $3.tmp >> $3.tmp 67 | $(MV) $3.tmp $3 68 | endef 69 | 70 | 71 | all: $(test) 72 | 73 | 74 | $(lib): $(objects) 75 | @echo Creating $(lib) library... 76 | @ar cr $(lib) $(objects) 77 | 78 | $(test): $(lib) $(test_objects) 79 | @echo Linking $(test)... 80 | @$(CXX) $(LDFLAGS) -o $(test) $(test_objects) $(lib) 81 | @echo Running unit tests... 82 | @./$(test) 83 | 84 | clean: 85 | -@$(RM) $(objects) $(test_objects) $(dependencies) $(test_dependencies) $(test) $(lib) 2> /dev/null 86 | 87 | %.o : %.cpp 88 | @echo $< 89 | @$(call make-depend,$<,$@,$(subst .o,.d,$@)) 90 | @$(CXX) $(CXXFLAGS) -c $< -o $(patsubst %.cpp, %.o, $<) 91 | 92 | 93 | ifneq "$(MAKECMDGOALS)" "clean" 94 | -include $(dependencies) 95 | -include $(test_dependencies) 96 | endif 97 | -------------------------------------------------------------------------------- /easySQLite/SqlFieldSet.cpp: -------------------------------------------------------------------------------- 1 | #include "SqlFieldSet.h" 2 | 3 | 4 | namespace sql 5 | { 6 | 7 | FieldSet::FieldSet(Field* definition) 8 | { 9 | //make fields map from array 10 | if (definition) 11 | { 12 | int index = 0; 13 | while (true) 14 | { 15 | Field& field = definition[index]; 16 | 17 | if (field.isEndingField()) 18 | break; 19 | 20 | field._index = index; 21 | 22 | _vec.push_back(field); 23 | _map[field.getName()] = &field; 24 | 25 | index++; 26 | } 27 | } 28 | } 29 | 30 | FieldSet::FieldSet(std::vector& definition) 31 | { 32 | copy(definition); 33 | } 34 | 35 | FieldSet::FieldSet(const FieldSet& source) 36 | { 37 | copy(source._vec); 38 | } 39 | 40 | void FieldSet::copy(const std::vector& definition) 41 | { 42 | _vec = definition; 43 | 44 | //make fields map from vector 45 | for (int index = 0; index < (int)_vec.size(); index++) 46 | { 47 | Field& field = _vec[index]; 48 | field._index = index; 49 | _map[field.getName()] = &field; 50 | } 51 | } 52 | 53 | int FieldSet::count() 54 | { 55 | return (int)_vec.size(); 56 | } 57 | 58 | Field* FieldSet::getByIndex(int index) 59 | { 60 | if ((index >= 0) && (index < count())) 61 | return &_vec[index]; 62 | 63 | return NULL; 64 | } 65 | 66 | Field* FieldSet::getByName(string name) 67 | { 68 | return _map[name]; 69 | } 70 | 71 | string FieldSet::getDefinition() 72 | { 73 | string s; 74 | 75 | for (int index = 0; index < count(); index++) 76 | { 77 | if (Field* f = getByIndex(index)) 78 | { 79 | s += f->getDefinition(); 80 | if (index < (count() - 1)) 81 | s += ", "; 82 | } 83 | } 84 | 85 | return s; 86 | } 87 | 88 | string FieldSet::definitionHash() 89 | { 90 | string str = getDefinition(); 91 | return generateSHA(str); 92 | } 93 | 94 | string FieldSet::toString() 95 | { 96 | string s; 97 | 98 | for (int index = 0; index < count(); index++) 99 | { 100 | if (Field* f = getByIndex(index)) 101 | { 102 | s += f->getName(); 103 | if (index < (count() - 1)) 104 | s += ", "; 105 | } 106 | } 107 | 108 | return s; 109 | } 110 | 111 | FieldSet* FieldSet::createFromDefinition(string value) 112 | { 113 | std::vector vec; 114 | listToVector(value, vec, ","); 115 | 116 | std::vector fields; 117 | 118 | for (int index = 0; index < (int)vec.size(); index++) 119 | { 120 | std::string definition = vec[index]; 121 | 122 | if (Field* field = Field::createFromDefinition(definition)) 123 | { 124 | Field f(*field); 125 | fields.push_back(f); 126 | delete field; 127 | } else { 128 | return NULL; 129 | } 130 | } 131 | 132 | return new FieldSet(fields); 133 | } 134 | 135 | 136 | //sql eof 137 | }; 138 | -------------------------------------------------------------------------------- /Tests/TestSqlRecordSet.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++/src/UnitTest++.h" 2 | #include "../easySQLite/SqlRecordSet.h" 3 | #include "../easySQLite/SqlDatabase.h" 4 | #include "../easySQLite/SqlTable.h" 5 | 6 | 7 | namespace Tests 8 | { 9 | 10 | using namespace sql; 11 | 12 | 13 | static Field* definition() 14 | { 15 | static Field def[] = 16 | { 17 | Field(FIELD_KEY), 18 | Field("name", type_text, flag_not_null), 19 | Field("valueInt", type_int), 20 | Field("valueDbl", type_float), 21 | Field("valueTxt", type_text), 22 | Field("valueBol", type_bool, flag_not_null), 23 | Field("valueTme", type_time), 24 | Field(DEFINITION_END), 25 | }; 26 | 27 | return &def[0]; 28 | } 29 | 30 | static void setValues(Record& r) 31 | { 32 | r.setString("name", "test_text1"); 33 | r.setInteger("valueInt", 890); 34 | r.setDouble("valueDbl", 0.345); 35 | r.setString("valueTxt", "test_text2"); 36 | r.setBool("valueBol", true); 37 | r.setTime("valueTme", 0); 38 | } 39 | 40 | static void cmpValues(Record& r) 41 | { 42 | CHECK_EQUAL("test_text1", r.getValue("name")->asString()); 43 | CHECK_EQUAL(890, r.getValue("valueInt")->asInteger()); 44 | CHECK_EQUAL(0.345, r.getValue("valueDbl")->asDouble()); 45 | CHECK_EQUAL("test_text2", r.getValue("valueTxt")->asString()); 46 | CHECK_EQUAL(true, r.getValue("valueBol")->asBool()); 47 | CHECK_EQUAL(0, r.getValue("valueTme")->asTime().asInteger()); 48 | } 49 | 50 | TEST(RecordSetModify) 51 | { 52 | sql::Database db; 53 | 54 | try 55 | { 56 | db.open("UnitTests.db"); 57 | 58 | Table tb(db.getHandle(), "test", definition()); 59 | 60 | if (tb.exists()) 61 | tb.remove(); 62 | 63 | tb.create(); 64 | 65 | Record r(tb.fields()); 66 | 67 | setValues(r); 68 | 69 | for (int index = 0; index < 10; index++) 70 | tb.addRecord(&r); 71 | 72 | tb.open(); 73 | 74 | CHECK_EQUAL(10, tb.recordCount()); 75 | 76 | for (int index = 0; index < tb.recordCount(); index++) 77 | if (Record* record = tb.getRecord(index)) 78 | cmpValues(*record); 79 | 80 | } catch (Exception e) { 81 | CHECK_EQUAL("*", e.msg()); 82 | } 83 | } 84 | 85 | TEST(RecordSetData) 86 | { 87 | sql::Database db; 88 | 89 | try 90 | { 91 | db.open("UnitTests.db"); 92 | 93 | Table tb(db.getHandle(), "test", definition()); 94 | 95 | if (tb.exists()) 96 | tb.remove(); 97 | 98 | tb.create(); 99 | 100 | Record r(tb.fields()); 101 | 102 | setValues(r); 103 | 104 | tb.addRecord(&r); 105 | 106 | tb.open(); 107 | 108 | CHECK_EQUAL(1, tb.recordCount()); 109 | 110 | if (Record* record = tb.getTopRecord()) 111 | { 112 | cmpValues(*record); 113 | CHECK_EQUAL("1|test_text1|890|0.345|test_text2|1|01-01-1970 01:00, Thu", record->toString()); 114 | } else { 115 | CHECK_EQUAL("*", "record not found"); 116 | } 117 | 118 | } catch (Exception e) { 119 | CHECK_EQUAL("*", e.msg()); 120 | } 121 | } 122 | 123 | 124 | //eofn 125 | }; 126 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestTest.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | #include "../TestReporter.h" 3 | #include "../TimeHelpers.h" 4 | #include "ScopedCurrentTest.h" 5 | 6 | using namespace UnitTest; 7 | 8 | namespace { 9 | 10 | TEST(PassingTestHasNoFailures) 11 | { 12 | class PassingTest : public Test 13 | { 14 | public: 15 | PassingTest() : Test("passing") {} 16 | virtual void RunImpl() const 17 | { 18 | CHECK(true); 19 | } 20 | }; 21 | 22 | TestResults results; 23 | { 24 | ScopedCurrentTest scopedResults(results); 25 | PassingTest().Run(); 26 | } 27 | 28 | CHECK_EQUAL(0, results.GetFailureCount()); 29 | } 30 | 31 | 32 | TEST(FailingTestHasFailures) 33 | { 34 | class FailingTest : public Test 35 | { 36 | public: 37 | FailingTest() : Test("failing") {} 38 | virtual void RunImpl() const 39 | { 40 | CHECK(false); 41 | } 42 | }; 43 | 44 | TestResults results; 45 | { 46 | ScopedCurrentTest scopedResults(results); 47 | FailingTest().Run(); 48 | } 49 | 50 | CHECK_EQUAL(1, results.GetFailureCount()); 51 | } 52 | 53 | 54 | TEST(ThrowingTestsAreReportedAsFailures) 55 | { 56 | class CrashingTest : public Test 57 | { 58 | public: 59 | CrashingTest() : Test("throwing") {} 60 | virtual void RunImpl() const 61 | { 62 | throw "Blah"; 63 | } 64 | }; 65 | 66 | TestResults results; 67 | { 68 | ScopedCurrentTest scopedResult(results); 69 | CrashingTest().Run(); 70 | } 71 | 72 | CHECK_EQUAL(1, results.GetFailureCount()); 73 | } 74 | 75 | 76 | #ifndef UNITTEST_MINGW 77 | TEST(CrashingTestsAreReportedAsFailures) 78 | { 79 | class CrashingTest : public Test 80 | { 81 | public: 82 | CrashingTest() : Test("crashing") {} 83 | virtual void RunImpl() const 84 | { 85 | reinterpret_cast< void (*)() >(0)(); 86 | } 87 | }; 88 | 89 | TestResults results; 90 | { 91 | ScopedCurrentTest scopedResult(results); 92 | CrashingTest().Run(); 93 | } 94 | 95 | CHECK_EQUAL(1, results.GetFailureCount()); 96 | } 97 | #endif 98 | 99 | TEST(TestWithUnspecifiedSuiteGetsDefaultSuite) 100 | { 101 | Test test("test"); 102 | CHECK(test.m_details.suiteName != NULL); 103 | CHECK_EQUAL("DefaultSuite", test.m_details.suiteName); 104 | } 105 | 106 | TEST(TestReflectsSpecifiedSuiteName) 107 | { 108 | Test test("test", "testSuite"); 109 | CHECK(test.m_details.suiteName != NULL); 110 | CHECK_EQUAL("testSuite", test.m_details.suiteName); 111 | } 112 | 113 | void Fail() 114 | { 115 | CHECK(false); 116 | } 117 | 118 | TEST(OutOfCoreCHECKMacrosCanFailTests) 119 | { 120 | TestResults results; 121 | { 122 | ScopedCurrentTest scopedResult(results); 123 | Fail(); 124 | } 125 | 126 | CHECK_EQUAL(1, results.GetFailureCount()); 127 | } 128 | 129 | } 130 | -------------------------------------------------------------------------------- /easySQLite/SqlCommon.h: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2010 Piotr Zagawa 3 | // 4 | // Released under BSD License 5 | // 6 | 7 | #pragma once 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | namespace sql 16 | { 17 | //field state enums 18 | //-------------------------- 19 | 20 | enum field_use 21 | { 22 | FIELD_DEFAULT, 23 | FIELD_KEY, 24 | DEFINITION_END, 25 | }; 26 | 27 | 28 | enum field_type 29 | { 30 | type_undefined, 31 | type_int, 32 | type_text, 33 | type_float, 34 | type_bool, 35 | type_time, 36 | }; 37 | 38 | 39 | enum field_flags 40 | { 41 | flag_none = 0, 42 | flag_not_null = 1, 43 | flag_primary_key = 2, 44 | }; 45 | 46 | 47 | //common data types 48 | //-------------------------- 49 | 50 | //string 51 | typedef std::string string; 52 | 53 | 54 | //integer 55 | #if defined(_MSC_VER) || defined(__BORLANDC__) 56 | typedef __int64 integer; 57 | #else 58 | typedef long long int integer; 59 | #endif 60 | 61 | 62 | //time 63 | class time 64 | { 65 | private: 66 | time_t _value; 67 | 68 | private: 69 | string format(const char* format); 70 | 71 | public: 72 | time(); 73 | time(const time& value); 74 | time(integer value); 75 | time& operator=(const time& value); 76 | bool operator==(const time& value); 77 | static time now(); 78 | 79 | public: 80 | double diff(time& value); 81 | time_t get(); 82 | void addValue(integer value); 83 | void addMinutes(integer count); 84 | void addHours(integer count); 85 | void addDays(integer count); 86 | 87 | public: 88 | integer asInteger(); 89 | string asString(); 90 | string asTimeString(); 91 | string asDateString(); 92 | 93 | }; 94 | 95 | 96 | //common exception class 97 | class Exception 98 | { 99 | private: 100 | string _msg; 101 | 102 | public: 103 | Exception(string msg) 104 | : _msg(msg) 105 | { 106 | }; 107 | string msg() 108 | { 109 | return _msg; 110 | } 111 | }; 112 | 113 | 114 | //comment this directive to disable exceptions 115 | //#define USE_EXCEPTIONS 116 | 117 | 118 | #ifndef THROW_EXCEPTION 119 | #ifdef USE_EXCEPTIONS 120 | #define THROW_EXCEPTION(msg) throw Exception(msg); 121 | #else 122 | #define THROW_EXCEPTION(msg) 123 | #endif 124 | #endif 125 | 126 | 127 | //utils 128 | //-------------------------- 129 | class log 130 | { 131 | public: 132 | log(std::string s) 133 | { 134 | std::string text = s; 135 | text += "\r\n"; 136 | printf("%s",text.c_str()); 137 | } 138 | }; 139 | 140 | 141 | string intToStr(int value); 142 | string intToStr(integer value); 143 | 144 | string quoteStr(string value); 145 | 146 | string binToHex(const char* buffer, int size); 147 | 148 | string generateSHA(string& value); 149 | 150 | string& trimleft(string& s); 151 | string& trimright(string& s); 152 | string& trim(string& s); 153 | string trim(const string& s); 154 | 155 | void listToVector(string s, std::vector& vector, const char* sep = ","); 156 | 157 | 158 | //sql eof 159 | }; 160 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/RecordingReporter.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_RECORDINGREPORTER_H 2 | #define UNITTEST_RECORDINGREPORTER_H 3 | 4 | #include "../TestReporter.h" 5 | #include 6 | 7 | #include "../TestDetails.h" 8 | 9 | struct RecordingReporter : public UnitTest::TestReporter 10 | { 11 | private: 12 | enum { kMaxStringLength = 256 }; 13 | 14 | public: 15 | RecordingReporter() 16 | : testRunCount(0) 17 | , testFailedCount(0) 18 | , lastFailedLine(0) 19 | , testFinishedCount(0) 20 | , lastFinishedTestTime(0) 21 | , summaryTotalTestCount(0) 22 | , summaryFailedTestCount(0) 23 | , summaryFailureCount(0) 24 | , summarySecondsElapsed(0) 25 | { 26 | lastStartedSuite[0] = '\0'; 27 | lastStartedTest[0] = '\0'; 28 | lastFailedFile[0] = '\0'; 29 | lastFailedSuite[0] = '\0'; 30 | lastFailedTest[0] = '\0'; 31 | lastFailedMessage[0] = '\0'; 32 | lastFinishedSuite[0] = '\0'; 33 | lastFinishedTest[0] = '\0'; 34 | } 35 | 36 | virtual void ReportTestStart(UnitTest::TestDetails const& test) 37 | { 38 | using namespace std; 39 | 40 | ++testRunCount; 41 | strcpy(lastStartedSuite, test.suiteName); 42 | strcpy(lastStartedTest, test.testName); 43 | } 44 | 45 | virtual void ReportFailure(UnitTest::TestDetails const& test, char const* failure) 46 | { 47 | using namespace std; 48 | 49 | ++testFailedCount; 50 | strcpy(lastFailedFile, test.filename); 51 | lastFailedLine = test.lineNumber; 52 | strcpy(lastFailedSuite, test.suiteName); 53 | strcpy(lastFailedTest, test.testName); 54 | strcpy(lastFailedMessage, failure); 55 | } 56 | 57 | virtual void ReportTestFinish(UnitTest::TestDetails const& test, float testDuration) 58 | { 59 | using namespace std; 60 | 61 | ++testFinishedCount; 62 | strcpy(lastFinishedSuite, test.suiteName); 63 | strcpy(lastFinishedTest, test.testName); 64 | lastFinishedTestTime = testDuration; 65 | } 66 | 67 | virtual void ReportSummary(int totalTestCount, int failedTestCount, int failureCount, float secondsElapsed) 68 | { 69 | summaryTotalTestCount = totalTestCount; 70 | summaryFailedTestCount = failedTestCount; 71 | summaryFailureCount = failureCount; 72 | summarySecondsElapsed = secondsElapsed; 73 | } 74 | 75 | int testRunCount; 76 | char lastStartedSuite[kMaxStringLength]; 77 | char lastStartedTest[kMaxStringLength]; 78 | 79 | int testFailedCount; 80 | char lastFailedFile[kMaxStringLength]; 81 | int lastFailedLine; 82 | char lastFailedSuite[kMaxStringLength]; 83 | char lastFailedTest[kMaxStringLength]; 84 | char lastFailedMessage[kMaxStringLength]; 85 | 86 | int testFinishedCount; 87 | char lastFinishedSuite[kMaxStringLength]; 88 | char lastFinishedTest[kMaxStringLength]; 89 | float lastFinishedTestTime; 90 | 91 | int summaryTotalTestCount; 92 | int summaryFailedTestCount; 93 | int summaryFailureCount; 94 | float summarySecondsElapsed; 95 | }; 96 | 97 | 98 | #endif 99 | -------------------------------------------------------------------------------- /easySQLite/SqlRecordSet.cpp: -------------------------------------------------------------------------------- 1 | #include "SqlRecordSet.h" 2 | 3 | 4 | namespace sql 5 | { 6 | 7 | RecordSet::RecordSet(sqlite3* db) 8 | : _fields(NULL) 9 | { 10 | _db = db; 11 | _err_msg.clear(); 12 | _result_query = SQLITE_ERROR; 13 | _records.clear(); 14 | } 15 | 16 | RecordSet::RecordSet(sqlite3* db, FieldSet* fields) 17 | : _fields(*fields) 18 | { 19 | _db = db; 20 | _err_msg.clear(); 21 | _result_query = SQLITE_ERROR; 22 | _records.clear(); 23 | } 24 | 25 | RecordSet::RecordSet(sqlite3* db, Field* definition) 26 | : _fields(definition) 27 | { 28 | _db = db; 29 | _err_msg.clear(); 30 | _result_query = SQLITE_ERROR; 31 | _records.clear(); 32 | } 33 | 34 | RecordSet::~RecordSet(void) 35 | { 36 | close(); 37 | } 38 | 39 | string RecordSet::errMsg() 40 | { 41 | return _err_msg; 42 | } 43 | 44 | void RecordSet::close() 45 | { 46 | _err_msg.clear(); 47 | _records.clear(); 48 | _result_query = SQLITE_ERROR; 49 | } 50 | 51 | FieldSet* RecordSet::fields() 52 | { 53 | return &_fields; 54 | } 55 | 56 | bool RecordSet::isResult() 57 | { 58 | return (_result_query == SQLITE_OK); 59 | } 60 | 61 | int RecordSet::count() 62 | { 63 | return _records.size(); 64 | } 65 | 66 | int RecordSet::on_next_record(void* param, int column_count, char** values, char** columns) 67 | { 68 | RecordSet* recordset = (RecordSet*)param; 69 | 70 | Record record(recordset->fields()); 71 | 72 | record.initColumnCount(column_count); 73 | 74 | for (int index = 0; index < column_count; index++) 75 | { 76 | char* value = values[index]; 77 | 78 | if (Field* field = recordset->_fields.getByIndex(index)) 79 | { 80 | record.initColumnValue(index, value, field->getType()); 81 | } 82 | } 83 | 84 | recordset->_records.push_back(record); 85 | 86 | return DATASET_ITERATION_CONTINUE; 87 | } 88 | 89 | bool RecordSet::query(string sql) 90 | { 91 | close(); 92 | 93 | char* error = 0; 94 | 95 | _result_query = sqlite3_exec(_db, sql.c_str(), on_next_record, this, &error); 96 | 97 | if (isResult()) 98 | { 99 | return true; 100 | } 101 | 102 | if (*error) 103 | { 104 | _err_msg = error; 105 | sqlite3_free(error); 106 | } 107 | 108 | THROW_EXCEPTION("RecordSet::query: " + errMsg()) 109 | 110 | return false; 111 | } 112 | 113 | Record* RecordSet::getRecord(int record_index) 114 | { 115 | if ((record_index >= 0) && (record_index < (int)_records.size())) 116 | return &_records.at(record_index); 117 | 118 | return NULL; 119 | } 120 | 121 | string RecordSet::toString() 122 | { 123 | string s; 124 | 125 | for (int record_index = 0; record_index < count(); record_index++) 126 | { 127 | if (Record* record = getRecord(record_index)) 128 | { 129 | s += intToStr(record_index + 1) + ". " + record->toString(); 130 | s += "\r\n"; 131 | } 132 | } 133 | 134 | return s; 135 | } 136 | 137 | Record* RecordSet::getTopRecord() 138 | { 139 | if (isResult()) 140 | { 141 | return getRecord(0); 142 | } 143 | return NULL; 144 | } 145 | 146 | Value* RecordSet::getTopRecordFirstValue() 147 | { 148 | if (isResult()) 149 | { 150 | if (Record* record = getRecord(0)) 151 | { 152 | return record->getValue(0); 153 | } 154 | } 155 | return NULL; 156 | } 157 | 158 | 159 | //sql eof 160 | }; 161 | -------------------------------------------------------------------------------- /UnitTest++/src/MemoryOutStream.cpp: -------------------------------------------------------------------------------- 1 | #include "MemoryOutStream.h" 2 | 3 | #ifndef UNITTEST_USE_CUSTOM_STREAMS 4 | 5 | 6 | namespace UnitTest { 7 | 8 | char const* MemoryOutStream::GetText() const 9 | { 10 | m_text = this->str(); 11 | return m_text.c_str(); 12 | } 13 | 14 | 15 | } 16 | 17 | 18 | #else 19 | 20 | 21 | #include 22 | #include 23 | 24 | namespace UnitTest { 25 | 26 | namespace { 27 | 28 | template 29 | void FormatToStream(MemoryOutStream& stream, char const* format, ValueType const& value) 30 | { 31 | using namespace std; 32 | 33 | char txt[32]; 34 | sprintf(txt, format, value); 35 | stream << txt; 36 | } 37 | 38 | int RoundUpToMultipleOfPow2Number (int n, int pow2Number) 39 | { 40 | return (n + (pow2Number - 1)) & ~(pow2Number - 1); 41 | } 42 | 43 | } 44 | 45 | 46 | MemoryOutStream::MemoryOutStream(int const size) 47 | : m_capacity (0) 48 | , m_buffer (0) 49 | 50 | { 51 | GrowBuffer(size); 52 | } 53 | 54 | MemoryOutStream::~MemoryOutStream() 55 | { 56 | delete [] m_buffer; 57 | } 58 | 59 | char const* MemoryOutStream::GetText() const 60 | { 61 | return m_buffer; 62 | } 63 | 64 | MemoryOutStream& MemoryOutStream::operator << (char const* txt) 65 | { 66 | using namespace std; 67 | 68 | int const bytesLeft = m_capacity - (int)strlen(m_buffer); 69 | int const bytesRequired = (int)strlen(txt) + 1; 70 | 71 | if (bytesRequired > bytesLeft) 72 | { 73 | int const requiredCapacity = bytesRequired + m_capacity - bytesLeft; 74 | GrowBuffer(requiredCapacity); 75 | } 76 | 77 | strcat(m_buffer, txt); 78 | return *this; 79 | } 80 | 81 | MemoryOutStream& MemoryOutStream::operator << (int const n) 82 | { 83 | FormatToStream(*this, "%i", n); 84 | return *this; 85 | } 86 | 87 | MemoryOutStream& MemoryOutStream::operator << (long const n) 88 | { 89 | FormatToStream(*this, "%li", n); 90 | return *this; 91 | } 92 | 93 | MemoryOutStream& MemoryOutStream::operator << (unsigned long const n) 94 | { 95 | FormatToStream(*this, "%lu", n); 96 | return *this; 97 | } 98 | 99 | MemoryOutStream& MemoryOutStream::operator << (float const f) 100 | { 101 | FormatToStream(*this, "%ff", f); 102 | return *this; 103 | } 104 | 105 | MemoryOutStream& MemoryOutStream::operator << (void const* p) 106 | { 107 | FormatToStream(*this, "%p", p); 108 | return *this; 109 | } 110 | 111 | MemoryOutStream& MemoryOutStream::operator << (unsigned int const s) 112 | { 113 | FormatToStream(*this, "%u", s); 114 | return *this; 115 | } 116 | 117 | MemoryOutStream& MemoryOutStream::operator <<(double const d) 118 | { 119 | FormatToStream(*this, "%f", d); 120 | return *this; 121 | } 122 | 123 | int MemoryOutStream::GetCapacity() const 124 | { 125 | return m_capacity; 126 | } 127 | 128 | 129 | void MemoryOutStream::GrowBuffer(int const desiredCapacity) 130 | { 131 | int const newCapacity = RoundUpToMultipleOfPow2Number(desiredCapacity, GROW_CHUNK_SIZE); 132 | 133 | using namespace std; 134 | 135 | char* buffer = new char[newCapacity]; 136 | if (m_buffer) 137 | strcpy(buffer, m_buffer); 138 | else 139 | strcpy(buffer, ""); 140 | 141 | delete [] m_buffer; 142 | m_buffer = buffer; 143 | m_capacity = newCapacity; 144 | } 145 | 146 | } 147 | 148 | 149 | #endif 150 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestTestResults.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | #include "../TestResults.h" 3 | #include "RecordingReporter.h" 4 | 5 | using namespace UnitTest; 6 | 7 | namespace { 8 | 9 | TestDetails const details("testname", "suitename", "filename", 123); 10 | 11 | 12 | TEST(StartsWithNoTestsRun) 13 | { 14 | TestResults results; 15 | CHECK_EQUAL (0, results.GetTotalTestCount()); 16 | } 17 | 18 | TEST(RecordsNumbersOfTests) 19 | { 20 | TestResults results; 21 | results.OnTestStart(details); 22 | results.OnTestStart(details); 23 | results.OnTestStart(details); 24 | CHECK_EQUAL(3, results.GetTotalTestCount()); 25 | } 26 | 27 | TEST(StartsWithNoTestsFailing) 28 | { 29 | TestResults results; 30 | CHECK_EQUAL (0, results.GetFailureCount()); 31 | } 32 | 33 | TEST(RecordsNumberOfFailures) 34 | { 35 | TestResults results; 36 | results.OnTestFailure(details, ""); 37 | results.OnTestFailure(details, ""); 38 | CHECK_EQUAL(2, results.GetFailureCount()); 39 | } 40 | 41 | TEST(RecordsNumberOfFailedTests) 42 | { 43 | TestResults results; 44 | 45 | results.OnTestStart(details); 46 | results.OnTestFailure(details, ""); 47 | results.OnTestFinish(details, 0); 48 | 49 | results.OnTestStart(details); 50 | results.OnTestFailure(details, ""); 51 | results.OnTestFailure(details, ""); 52 | results.OnTestFailure(details, ""); 53 | results.OnTestFinish(details, 0); 54 | 55 | CHECK_EQUAL (2, results.GetFailedTestCount()); 56 | } 57 | 58 | TEST(NotifiesReporterOfTestStartWithCorrectInfo) 59 | { 60 | RecordingReporter reporter; 61 | TestResults results(&reporter); 62 | results.OnTestStart(details); 63 | 64 | CHECK_EQUAL (1, reporter.testRunCount); 65 | CHECK_EQUAL ("suitename", reporter.lastStartedSuite); 66 | CHECK_EQUAL ("testname", reporter.lastStartedTest); 67 | } 68 | 69 | TEST(NotifiesReporterOfTestFailureWithCorrectInfo) 70 | { 71 | RecordingReporter reporter; 72 | TestResults results(&reporter); 73 | 74 | results.OnTestFailure(details, "failurestring"); 75 | CHECK_EQUAL (1, reporter.testFailedCount); 76 | CHECK_EQUAL ("filename", reporter.lastFailedFile); 77 | CHECK_EQUAL (123, reporter.lastFailedLine); 78 | CHECK_EQUAL ("suitename", reporter.lastFailedSuite); 79 | CHECK_EQUAL ("testname", reporter.lastFailedTest); 80 | CHECK_EQUAL ("failurestring", reporter.lastFailedMessage); 81 | } 82 | 83 | TEST(NotifiesReporterOfCheckFailureWithCorrectInfo) 84 | { 85 | RecordingReporter reporter; 86 | TestResults results(&reporter); 87 | 88 | results.OnTestFailure(details, "failurestring"); 89 | CHECK_EQUAL (1, reporter.testFailedCount); 90 | 91 | CHECK_EQUAL ("filename", reporter.lastFailedFile); 92 | CHECK_EQUAL (123, reporter.lastFailedLine); 93 | CHECK_EQUAL ("testname", reporter.lastFailedTest); 94 | CHECK_EQUAL ("suitename", reporter.lastFailedSuite); 95 | CHECK_EQUAL ("failurestring", reporter.lastFailedMessage); 96 | } 97 | 98 | TEST(NotifiesReporterOfTestEnd) 99 | { 100 | RecordingReporter reporter; 101 | TestResults results(&reporter); 102 | 103 | results.OnTestFinish(details, 0.1234f); 104 | CHECK_EQUAL (1, reporter.testFinishedCount); 105 | CHECK_EQUAL ("testname", reporter.lastFinishedTest); 106 | CHECK_EQUAL ("suitename", reporter.lastFinishedSuite); 107 | CHECK_CLOSE (0.1234f, reporter.lastFinishedTestTime, 0.0001f); 108 | } 109 | 110 | 111 | } 112 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestUnitTest++.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | #include "../ReportAssert.h" 3 | #include "ScopedCurrentTest.h" 4 | 5 | #include 6 | 7 | // These are sample tests that show the different features of the framework 8 | 9 | namespace { 10 | 11 | TEST(ValidCheckSucceeds) 12 | { 13 | bool const b = true; 14 | CHECK(b); 15 | } 16 | 17 | TEST(CheckWorksWithPointers) 18 | { 19 | void* p = (void *)0x100; 20 | CHECK(p); 21 | CHECK(p != 0); 22 | } 23 | 24 | TEST(ValidCheckEqualSucceeds) 25 | { 26 | int const x = 3; 27 | int const y = 3; 28 | CHECK_EQUAL(x, y); 29 | } 30 | 31 | TEST(CheckEqualWorksWithPointers) 32 | { 33 | void* p = (void *)0; 34 | CHECK_EQUAL((void*)0, p); 35 | } 36 | 37 | TEST(ValidCheckCloseSucceeds) 38 | { 39 | CHECK_CLOSE(2.0f, 2.001f, 0.01f); 40 | CHECK_CLOSE(2.001f, 2.0f, 0.01f); 41 | } 42 | 43 | TEST(ArrayCloseSucceeds) 44 | { 45 | float const a1[] = {1, 2, 3}; 46 | float const a2[] = {1, 2.01f, 3}; 47 | CHECK_ARRAY_CLOSE(a1, a2, 3, 0.1f); 48 | } 49 | 50 | TEST (CheckArrayCloseWorksWithVectors) 51 | { 52 | std::vector< float > a(4); 53 | for (int i = 0; i < 4; ++i) 54 | a[i] = (float)i; 55 | 56 | CHECK_ARRAY_CLOSE(a, a, (int)a.size(), 0.0001f); 57 | } 58 | 59 | TEST(CheckThrowMacroSucceedsOnCorrectException) 60 | { 61 | struct TestException {}; 62 | CHECK_THROW(throw TestException(), TestException); 63 | } 64 | 65 | TEST(CheckAssertSucceeds) 66 | { 67 | CHECK_ASSERT(UnitTest::ReportAssert("desc", "file", 0)); 68 | } 69 | 70 | TEST(CheckThrowMacroFailsOnMissingException) 71 | { 72 | class NoThrowTest : public UnitTest::Test 73 | { 74 | public: 75 | NoThrowTest() : Test("nothrow") {} 76 | void DontThrow() const 77 | { 78 | } 79 | 80 | virtual void RunImpl() const 81 | { 82 | CHECK_THROW(DontThrow(), int); 83 | } 84 | }; 85 | 86 | UnitTest::TestResults results; 87 | { 88 | ScopedCurrentTest scopedResults(results); 89 | 90 | NoThrowTest test; 91 | test.Run(); 92 | } 93 | 94 | CHECK_EQUAL(1, results.GetFailureCount()); 95 | } 96 | 97 | TEST(CheckThrowMacroFailsOnWrongException) 98 | { 99 | class WrongThrowTest : public UnitTest::Test 100 | { 101 | public: 102 | WrongThrowTest() : Test("wrongthrow") {} 103 | virtual void RunImpl() const 104 | { 105 | CHECK_THROW(throw "oops", int); 106 | } 107 | }; 108 | 109 | UnitTest::TestResults results; 110 | { 111 | ScopedCurrentTest scopedResults(results); 112 | 113 | WrongThrowTest test; 114 | test.Run(); 115 | } 116 | 117 | CHECK_EQUAL(1, results.GetFailureCount()); 118 | } 119 | 120 | struct SimpleFixture 121 | { 122 | SimpleFixture() 123 | { 124 | ++instanceCount; 125 | } 126 | ~SimpleFixture() 127 | { 128 | --instanceCount; 129 | } 130 | 131 | static int instanceCount; 132 | }; 133 | 134 | int SimpleFixture::instanceCount = 0; 135 | 136 | TEST_FIXTURE(SimpleFixture, DefaultFixtureCtorIsCalled) 137 | { 138 | CHECK(SimpleFixture::instanceCount > 0); 139 | } 140 | 141 | TEST_FIXTURE(SimpleFixture, OnlyOneFixtureAliveAtATime) 142 | { 143 | CHECK_EQUAL(1, SimpleFixture::instanceCount); 144 | } 145 | 146 | void CheckBool(const bool b) 147 | { 148 | CHECK(b); 149 | } 150 | 151 | TEST(CanCallCHECKOutsideOfTestFunction) 152 | { 153 | CheckBool(true); 154 | } 155 | 156 | } 157 | -------------------------------------------------------------------------------- /easySQLite/SqlField.cpp: -------------------------------------------------------------------------------- 1 | #include "SqlField.h" 2 | 3 | 4 | namespace sql 5 | { 6 | 7 | Field::Field(field_use use) 8 | { 9 | this->_name.clear(); 10 | this->_use = use; 11 | this->_type = type_undefined; 12 | this->_index = -1; 13 | this->_flags = flag_none; 14 | 15 | if (use == FIELD_KEY) 16 | { 17 | this->_name = "_ID"; 18 | this->_type = type_int; 19 | this->_flags = flag_primary_key; 20 | } 21 | } 22 | 23 | Field::Field(string name, field_type type, int flags) 24 | { 25 | this->_name = name; 26 | this->_use = FIELD_DEFAULT; 27 | this->_type = type; 28 | this->_index = -1; 29 | this->_flags = flags; 30 | } 31 | 32 | Field::Field(const Field& value) 33 | { 34 | _name = value._name; 35 | _use = value._use; 36 | _type = value._type; 37 | _index = value._index; 38 | _flags = value._flags; 39 | } 40 | 41 | bool Field::isKeyIdField() 42 | { 43 | return (_use == FIELD_KEY); 44 | } 45 | 46 | bool Field::isEndingField() 47 | { 48 | return (_use == DEFINITION_END); 49 | } 50 | 51 | field_type Field::getType() 52 | { 53 | return _type; 54 | } 55 | 56 | int Field::getIndex() 57 | { 58 | return _index; 59 | } 60 | 61 | string Field::getName() const 62 | { 63 | return _name; 64 | } 65 | 66 | string Field::getTypeStr() 67 | { 68 | switch (_type) 69 | { 70 | case type_int: return "INTEGER"; 71 | case type_text: return "TEXT"; 72 | case type_float: return "REAL"; 73 | case type_bool: return "INTEGER"; 74 | case type_time: return "INTEGER"; 75 | default: 76 | return ""; 77 | } 78 | } 79 | 80 | bool Field::isPrimaryKey() 81 | { 82 | return ((_flags & flag_primary_key) == flag_primary_key); 83 | } 84 | 85 | bool Field::isNotNull() 86 | { 87 | return ((_flags & flag_not_null) == flag_not_null); 88 | } 89 | 90 | string Field::getDefinition() 91 | { 92 | string value = _name + " " + getTypeStr(); 93 | 94 | if (isPrimaryKey()) 95 | value += " PRIMARY KEY"; 96 | 97 | if (isNotNull()) 98 | value += " NOT NULL"; 99 | 100 | return trim(value); 101 | } 102 | 103 | Field* Field::createFromDefinition(string value) 104 | { 105 | std::vector vec; 106 | 107 | listToVector(value, vec, " "); 108 | 109 | const int count = (int)vec.size(); 110 | 111 | string _name; 112 | 113 | field_use _use = FIELD_DEFAULT; 114 | field_type _type = type_undefined; 115 | 116 | int _flags = flag_none; 117 | 118 | //parse name 119 | if (count > 0) 120 | _name = vec[0]; 121 | 122 | //parse type 123 | if (count > 1) 124 | { 125 | std::string& type = vec[1]; 126 | 127 | if (type.compare("INTEGER") == 0) 128 | _type = type_int; 129 | 130 | if (type.compare("TEXT") == 0) 131 | _type = type_text; 132 | 133 | if (type.compare("REAL") == 0) 134 | _type = type_float; 135 | } 136 | 137 | //parse optional flags 138 | if (count > 2) 139 | { 140 | std::string flags = vec[2]; 141 | 142 | if (count > 3) 143 | flags += " " + vec[3]; 144 | 145 | if (flags.find("PRIMARY KEY") != std::string::npos) 146 | { 147 | _use = FIELD_KEY; 148 | _flags = flag_primary_key; 149 | } 150 | 151 | if (flags.find("NOT NULL") != std::string::npos) 152 | { 153 | _flags |= flag_not_null; 154 | } 155 | } 156 | 157 | Field* field = NULL; 158 | 159 | if (!_name.empty()) 160 | { 161 | if (_type != type_undefined) 162 | { 163 | if (_use == FIELD_DEFAULT) 164 | { 165 | field = new Field(_name, _type, _flags); 166 | } else { 167 | field = new Field(_use); 168 | } 169 | } 170 | } 171 | 172 | return field; 173 | } 174 | 175 | 176 | //sql eof 177 | }; 178 | -------------------------------------------------------------------------------- /easySQLite/SqlValue.cpp: -------------------------------------------------------------------------------- 1 | #include "SqlValue.h" 2 | 3 | 4 | namespace sql 5 | { 6 | 7 | Value::Value() 8 | { 9 | setValue(NULL, type_undefined); 10 | } 11 | 12 | Value::Value(const char* value, field_type type) 13 | { 14 | setValue(value, type); 15 | } 16 | 17 | Value::Value(const Value& value) 18 | { 19 | this->_value = value._value; 20 | this->_isNull = value._isNull; 21 | this->_type = value._type; 22 | } 23 | 24 | Value& Value::operator=(const Value& value) 25 | { 26 | if (this != &value) 27 | { 28 | this->_value = value._value; 29 | this->_isNull = value._isNull; 30 | this->_type = value._type; 31 | } 32 | return *this; 33 | } 34 | 35 | bool Value::equals(Value& value) 36 | { 37 | if (isNull() && value.isNull()) 38 | return true; 39 | 40 | switch (_type) 41 | { 42 | case type_int: 43 | return (asInteger() == value.asInteger()); 44 | case type_text: 45 | return (asString().compare(value.asString()) == 0); 46 | case type_float: 47 | return (asDouble() == value.asDouble()); 48 | case type_bool: 49 | return (asBool() == value.asBool()); 50 | case type_time: 51 | return (asTime() == value.asTime()); 52 | default: 53 | return false; 54 | } 55 | } 56 | 57 | void Value::setValue(const char* value, field_type type) 58 | { 59 | _isNull = true; 60 | _value.clear(); 61 | _type = type; 62 | 63 | if (value) 64 | { 65 | _isNull = false; 66 | _value = value; 67 | _type = type; 68 | } 69 | } 70 | 71 | string Value::toSql(field_type type) 72 | { 73 | if (isNull()) 74 | return "null"; 75 | 76 | if (type == type_text) 77 | return "'" + quoteStr(asString()) + "'"; 78 | 79 | if (_type == type_time) 80 | return intToStr(asInteger()); 81 | 82 | return asString(); 83 | } 84 | 85 | string Value::toString() 86 | { 87 | if (isNull()) 88 | return "null"; 89 | 90 | return asString(); 91 | } 92 | 93 | string Value::asString() 94 | { 95 | if (_type == type_time) 96 | { 97 | time t(asInteger()); 98 | return t.asString(); 99 | } 100 | 101 | return _value; 102 | } 103 | 104 | integer Value::asInteger() 105 | { 106 | if (isNull()) 107 | return 0; 108 | 109 | long long value = 0; 110 | sscanf(_value.c_str(),"%lld",&value); 111 | return value; 112 | } 113 | 114 | double Value::asDouble() 115 | { 116 | if (isNull()) 117 | return 0.0; 118 | 119 | double value = 0; 120 | sscanf(_value.c_str(),"%lf",&value); 121 | return value; 122 | } 123 | 124 | bool Value::asBool() 125 | { 126 | if (isNull()) 127 | return false; 128 | 129 | return (_value.compare("1") == 0); 130 | } 131 | 132 | time Value::asTime() 133 | { 134 | time dt(asInteger()); 135 | return dt; 136 | } 137 | 138 | void Value::setNull() 139 | { 140 | _isNull = true; 141 | _value.clear(); 142 | } 143 | 144 | void Value::setString(string value) 145 | { 146 | _isNull = false; 147 | _value = value; 148 | } 149 | 150 | //CRT_SECURE_NO_WARNINGS 151 | 152 | void Value::setInteger(integer value) 153 | { 154 | char buffer[128]; 155 | 156 | sprintf(buffer,"%lld",value); 157 | 158 | _isNull = false; 159 | _value = buffer; 160 | } 161 | 162 | void Value::setDouble(double value) 163 | { 164 | char buffer[128]; 165 | 166 | sprintf(buffer, "%0.8f", value); 167 | 168 | _isNull = false; 169 | _value = buffer; 170 | } 171 | 172 | 173 | 174 | void Value::setBool(bool value) 175 | { 176 | _isNull = false; 177 | _value = (value ? "1" : "0"); 178 | } 179 | 180 | void Value::setTime(time value) 181 | { 182 | time t(value); 183 | _isNull = false; 184 | setInteger(t.asInteger()); 185 | } 186 | 187 | bool Value::isNull() 188 | { 189 | return _isNull; 190 | } 191 | 192 | 193 | //sql eof 194 | }; 195 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestDeferredTestReporter.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | #include "../DeferredTestReporter.h" 3 | #include "../Config.h" 4 | #include 5 | 6 | namespace UnitTest 7 | { 8 | 9 | namespace 10 | { 11 | 12 | #ifdef UNITTEST_USE_CUSTOM_STREAMS 13 | MemoryOutStream& operator <<(MemoryOutStream& lhs, const std::string& rhs) 14 | { 15 | lhs << rhs.c_str(); 16 | return lhs; 17 | } 18 | #endif 19 | 20 | struct MockDeferredTestReporter : public DeferredTestReporter 21 | { 22 | virtual void ReportSummary(int, int, int, float) 23 | { 24 | } 25 | }; 26 | 27 | struct DeferredTestReporterFixture 28 | { 29 | DeferredTestReporterFixture() 30 | : testName("UniqueTestName") 31 | , testSuite("UniqueTestSuite") 32 | , fileName("filename.h") 33 | , lineNumber(12) 34 | , details(testName.c_str(), testSuite.c_str(), fileName.c_str(), lineNumber) 35 | { 36 | } 37 | 38 | MockDeferredTestReporter reporter; 39 | std::string const testName; 40 | std::string const testSuite; 41 | std::string const fileName; 42 | int const lineNumber; 43 | TestDetails const details; 44 | }; 45 | 46 | TEST_FIXTURE(DeferredTestReporterFixture, ReportTestStartCreatesANewDeferredTest) 47 | { 48 | reporter.ReportTestStart(details); 49 | CHECK_EQUAL(1, (int)reporter.GetResults().size()); 50 | } 51 | 52 | TEST_FIXTURE(DeferredTestReporterFixture, ReportTestStartCapturesTestNameAndSuite) 53 | { 54 | reporter.ReportTestStart(details); 55 | 56 | DeferredTestResult const& result = reporter.GetResults().at(0); 57 | CHECK_EQUAL(testName.c_str(), result.testName); 58 | CHECK_EQUAL(testSuite.c_str(), result.suiteName); 59 | } 60 | 61 | TEST_FIXTURE(DeferredTestReporterFixture, ReportTestEndCapturesTestTime) 62 | { 63 | float const elapsed = 123.45f; 64 | reporter.ReportTestStart(details); 65 | reporter.ReportTestFinish(details, elapsed); 66 | 67 | DeferredTestResult const& result = reporter.GetResults().at(0); 68 | CHECK_CLOSE(elapsed, result.timeElapsed, 0.0001f); 69 | } 70 | 71 | TEST_FIXTURE(DeferredTestReporterFixture, ReportFailureSavesFailureDetails) 72 | { 73 | char const* failure = "failure"; 74 | 75 | reporter.ReportTestStart(details); 76 | reporter.ReportFailure(details, failure); 77 | 78 | DeferredTestResult const& result = reporter.GetResults().at(0); 79 | CHECK(result.failed == true); 80 | CHECK_EQUAL(fileName.c_str(), result.failureFile); 81 | } 82 | 83 | TEST_FIXTURE(DeferredTestReporterFixture, ReportFailureSavesFailureDetailsForMultipleFailures) 84 | { 85 | char const* failure1 = "failure 1"; 86 | char const* failure2 = "failure 2"; 87 | 88 | reporter.ReportTestStart(details); 89 | reporter.ReportFailure(details, failure1); 90 | reporter.ReportFailure(details, failure2); 91 | 92 | DeferredTestResult const& result = reporter.GetResults().at(0); 93 | CHECK_EQUAL(2, (int)result.failures.size()); 94 | CHECK_EQUAL(failure1, result.failures[0].second); 95 | CHECK_EQUAL(failure2, result.failures[1].second); 96 | } 97 | 98 | TEST_FIXTURE(DeferredTestReporterFixture, DeferredTestReporterTakesCopyOfFailureMessage) 99 | { 100 | reporter.ReportTestStart(details); 101 | 102 | char failureMessage[128]; 103 | char const* goodStr = "Real failure message"; 104 | char const* badStr = "Bogus failure message"; 105 | 106 | using namespace std; 107 | 108 | strcpy(failureMessage, goodStr); 109 | reporter.ReportFailure(details, failureMessage); 110 | strcpy(failureMessage, badStr); 111 | 112 | DeferredTestResult const& result = reporter.GetResults().at(0); 113 | DeferredTestResult::Failure const& failure = result.failures.at(0); 114 | CHECK_EQUAL(goodStr, failure.second); 115 | } 116 | 117 | }} 118 | -------------------------------------------------------------------------------- /UnitTest++/src/XmlTestReporter.cpp: -------------------------------------------------------------------------------- 1 | #include "XmlTestReporter.h" 2 | #include "Config.h" 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | using std::string; 9 | using std::ostringstream; 10 | using std::ostream; 11 | 12 | namespace { 13 | 14 | void ReplaceChar(string& str, char c, string const& replacement) 15 | { 16 | for (size_t pos = str.find(c); pos != string::npos; pos = str.find(c, pos + 1)) 17 | str.replace(pos, 1, replacement); 18 | } 19 | 20 | string XmlEscape(string const& value) 21 | { 22 | string escaped = value; 23 | 24 | ReplaceChar(escaped, '&', "&"); 25 | ReplaceChar(escaped, '<', "<"); 26 | ReplaceChar(escaped, '>', ">"); 27 | ReplaceChar(escaped, '\'', "'"); 28 | ReplaceChar(escaped, '\"', """); 29 | 30 | return escaped; 31 | } 32 | 33 | string BuildFailureMessage(string const& file, int line, string const& message) 34 | { 35 | ostringstream failureMessage; 36 | failureMessage << file << "(" << line << ") : " << message; 37 | return failureMessage.str(); 38 | } 39 | 40 | } 41 | 42 | namespace UnitTest { 43 | 44 | XmlTestReporter::XmlTestReporter(ostream& ostream) 45 | : m_ostream(ostream) 46 | { 47 | } 48 | 49 | void XmlTestReporter::ReportSummary(int totalTestCount, int failedTestCount, 50 | int failureCount, float secondsElapsed) 51 | { 52 | AddXmlElement(m_ostream, NULL); 53 | 54 | BeginResults(m_ostream, totalTestCount, failedTestCount, failureCount, secondsElapsed); 55 | 56 | DeferredTestResultList const& results = GetResults(); 57 | for (DeferredTestResultList::const_iterator i = results.begin(); i != results.end(); ++i) 58 | { 59 | BeginTest(m_ostream, *i); 60 | 61 | if (i->failed) 62 | AddFailure(m_ostream, *i); 63 | 64 | EndTest(m_ostream, *i); 65 | } 66 | 67 | EndResults(m_ostream); 68 | } 69 | 70 | void XmlTestReporter::AddXmlElement(ostream& os, char const* encoding) 71 | { 72 | os << ""; 78 | } 79 | 80 | void XmlTestReporter::BeginResults(std::ostream& os, int totalTestCount, int failedTestCount, 81 | int failureCount, float secondsElapsed) 82 | { 83 | os << ""; 89 | } 90 | 91 | void XmlTestReporter::EndResults(std::ostream& os) 92 | { 93 | os << ""; 94 | } 95 | 96 | void XmlTestReporter::BeginTest(std::ostream& os, DeferredTestResult const& result) 97 | { 98 | os << ""; 108 | else 109 | os << "/>"; 110 | } 111 | 112 | void XmlTestReporter::AddFailure(std::ostream& os, DeferredTestResult const& result) 113 | { 114 | os << ">"; // close element 115 | 116 | for (DeferredTestResult::FailureVec::const_iterator it = result.failures.begin(); 117 | it != result.failures.end(); 118 | ++it) 119 | { 120 | string const escapedMessage = XmlEscape(it->second); 121 | string const message = BuildFailureMessage(result.failureFile, it->first, escapedMessage); 122 | 123 | os << ""; 124 | } 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /easySQLite.cpp: -------------------------------------------------------------------------------- 1 | // easySQLite.cpp : Defines the entry point for the console application. 2 | // 3 | 4 | #include "stdafx.h" 5 | 6 | #include "easySQLite/SqlCommon.h" 7 | #include "easySQLite/SqlField.h" 8 | #include "easySQLite/SqlDatabase.h" 9 | #include "easySQLite/SqlTable.h" 10 | 11 | #include "UnitTest++/src/UnitTest++.h" 12 | 13 | 14 | using namespace sql; 15 | 16 | 17 | void example1() 18 | { 19 | //define table structure 20 | Field definition_tbPerson[] = 21 | { 22 | Field(FIELD_KEY), 23 | Field("fname", type_text, flag_not_null), 24 | Field("lname", type_text, flag_not_null), 25 | Field("birthdate", type_time), 26 | Field(DEFINITION_END), 27 | }; 28 | 29 | //define database object 30 | sql::Database db; 31 | 32 | try 33 | { 34 | //open database file 35 | db.open("test.db"); 36 | 37 | //define table object 38 | Table tbPerson(db.getHandle(), "person", definition_tbPerson); 39 | 40 | //remove table from database if exists 41 | if (tbPerson.exists()) 42 | tbPerson.remove(); 43 | 44 | //create new table 45 | tbPerson.create(); 46 | 47 | //define new record 48 | Record record(tbPerson.fields()); 49 | 50 | //set record data 51 | record.setString("fname", "Jan"); 52 | record.setString("lname", "Kowalski"); 53 | record.setTime("birthdate", time::now()); 54 | 55 | //add 10 records 56 | for (int index = 0; index < 10; index++) 57 | tbPerson.addRecord(&record); 58 | 59 | //select record to update 60 | if (Record* record = tbPerson.getRecordByKeyId(7)) 61 | { 62 | record->setString("fname", "Frank"); 63 | record->setString("lname", "Sinatra"); 64 | record->setNull("birthdate"); 65 | 66 | tbPerson.updateRecord(record); 67 | } 68 | 69 | //load all records 70 | tbPerson.open(); 71 | 72 | //list loaded records 73 | for (int index = 0; index < tbPerson.recordCount(); index++) 74 | if (Record* record = tbPerson.getRecord(index)) 75 | sql::log(record->toString()); 76 | 77 | sql::log(""); 78 | sql::log("ALL OK"); 79 | 80 | } catch (Exception e) { 81 | printf("ERROR: %s\r\n", e.msg().c_str()); 82 | } 83 | } 84 | 85 | void example2() 86 | { 87 | Field definition_tbTest[] = 88 | { 89 | Field(FIELD_KEY), 90 | Field("name", type_text, flag_not_null), 91 | Field("valueInt", type_int), 92 | Field("valueDbl", type_float), 93 | Field("valueTxt", type_text), 94 | Field("valueBol", type_bool, flag_not_null), 95 | Field("valueTme", type_time), 96 | Field(DEFINITION_END), 97 | }; 98 | 99 | Database db; 100 | 101 | try 102 | { 103 | db.open("test.db"); 104 | 105 | Table source(db.getHandle(), "tbTest", definition_tbTest); 106 | 107 | if (source.exists()) 108 | source.remove(); 109 | 110 | source.create(); 111 | 112 | for (int index = 0; index < 5; index++) 113 | { 114 | Record record(source.fields()); 115 | 116 | record.setString("name", "test_name_1"); 117 | record.setInteger("valueInt", 1234567890); 118 | record.setDouble("valueDbl", 12345.67890); 119 | record.setString("valueTxt", "test'value'1"); 120 | record.setBool("valueBol", true); 121 | record.setTime("valueTme", time::now()); 122 | 123 | source.addRecord(&record); 124 | } 125 | 126 | if (Table* target = Table::createFromDefinition(db.getHandle(), "_backup_" + source.name(), source.fields()->getDefinition())) 127 | { 128 | if (target->backup(source)) 129 | { 130 | sql::log(""); 131 | sql::log("ALL OK"); 132 | } else { 133 | sql::log(source.errMsg()); 134 | sql::log(target->errMsg()); 135 | } 136 | 137 | delete target; 138 | } 139 | } catch (Exception e) { 140 | printf("ERROR: %s\r\n", e.msg().c_str()); 141 | } 142 | } 143 | 144 | 145 | int _tmain(int argc, _TCHAR* argv[]) 146 | { 147 | //example1(); 148 | //example2(); 149 | 150 | sql::log(""); 151 | 152 | return UnitTest::RunAllTests(); 153 | } 154 | -------------------------------------------------------------------------------- /Tests/TestSqlFieldSet.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++/src/UnitTest++.h" 2 | #include "../easySQLite/SqlFieldSet.h" 3 | 4 | 5 | namespace Tests 6 | { 7 | 8 | using namespace sql; 9 | 10 | 11 | static Field* definition() 12 | { 13 | static Field def[] = 14 | { 15 | Field(FIELD_KEY), 16 | Field("name", type_text, flag_not_null), 17 | Field("valueInt", type_int), 18 | Field("valueDbl", type_float), 19 | Field("valueTxt", type_text), 20 | Field("valueBol", type_bool, flag_not_null), 21 | Field("valueTme", type_time), 22 | Field(DEFINITION_END), 23 | }; 24 | 25 | return &def[0]; 26 | } 27 | 28 | TEST(FieldSet) 29 | { 30 | FieldSet fields(definition()); 31 | 32 | CHECK_EQUAL(7, fields.count()); 33 | 34 | if (fields.count() == 7) 35 | { 36 | for (int index = 0; index < 7; index++) 37 | CHECK_EQUAL(index, fields.getByIndex(index)->getIndex()); 38 | } 39 | 40 | CHECK(fields.getByName("name")); 41 | CHECK(fields.getByName("valueInt")); 42 | CHECK(fields.getByName("valueDbl")); 43 | CHECK(fields.getByName("valueTxt")); 44 | CHECK(fields.getByName("valueBol")); 45 | CHECK(fields.getByName("valueTme")); 46 | 47 | CHECK_EQUAL("name", (fields.getByName("name") ? fields.getByName("name")->getName() : "*")); 48 | CHECK_EQUAL("valueInt", (fields.getByName("valueInt") ? fields.getByName("valueInt")->getName() : "*")); 49 | CHECK_EQUAL("valueDbl", (fields.getByName("valueDbl") ? fields.getByName("valueDbl")->getName() : "*")); 50 | CHECK_EQUAL("valueTxt", (fields.getByName("valueTxt") ? fields.getByName("valueTxt")->getName() : "*")); 51 | CHECK_EQUAL("valueBol", (fields.getByName("valueBol") ? fields.getByName("valueBol")->getName() : "*")); 52 | CHECK_EQUAL("valueTme", (fields.getByName("valueTme") ? fields.getByName("valueTme")->getName() : "*")); 53 | } 54 | 55 | TEST(FieldSetCreation) 56 | { 57 | FieldSet fields(definition()); 58 | 59 | sql::string strdef; 60 | strdef += "_ID INTEGER PRIMARY KEY, "; 61 | strdef += "name TEXT NOT NULL, "; 62 | strdef += "valueInt INTEGER, "; 63 | strdef += "valueDbl REAL, "; 64 | strdef += "valueTxt TEXT, "; 65 | strdef += "valueBol INTEGER NOT NULL, "; 66 | strdef += "valueTme INTEGER"; 67 | 68 | CHECK_EQUAL(strdef, fields.getDefinition()); 69 | CHECK_EQUAL("63f1ae61aad81ca9e98eb7c9c643c55197aab28d", fields.definitionHash()); 70 | 71 | FieldSet fields_copy(fields); 72 | 73 | CHECK_EQUAL(fields_copy.definitionHash(), fields.definitionHash()); 74 | } 75 | 76 | TEST(FieldSetCreationFromString) 77 | { 78 | sql::string strdef; 79 | strdef += "_ID INTEGER PRIMARY KEY, "; 80 | strdef += "name TEXT NOT NULL, "; 81 | strdef += "valueInt INTEGER, "; 82 | strdef += "valueDbl REAL, "; 83 | strdef += "valueTxt TEXT, "; 84 | strdef += "valueBol INTEGER NOT NULL, "; 85 | strdef += "valueTme INTEGER"; 86 | 87 | FieldSet* fields = FieldSet::createFromDefinition(strdef); 88 | 89 | CHECK(fields != NULL); 90 | 91 | if (fields) 92 | { 93 | CHECK_EQUAL(strdef, fields->getDefinition()); 94 | CHECK_EQUAL("63f1ae61aad81ca9e98eb7c9c643c55197aab28d", fields->definitionHash()); 95 | delete fields; 96 | } 97 | } 98 | 99 | TEST(FieldSetFieldsOrder) 100 | { 101 | FieldSet fields(definition()); 102 | 103 | CHECK_EQUAL(7, fields.count()); 104 | 105 | //check if fields by name and by index are the same 106 | CHECK_EQUAL(fields.getByName("_ID")->getName(), fields.getByIndex(0)->getName()); 107 | CHECK_EQUAL(fields.getByName("name")->getName(), fields.getByIndex(1)->getName()); 108 | CHECK_EQUAL(fields.getByName("valueInt")->getName(), fields.getByIndex(2)->getName()); 109 | CHECK_EQUAL(fields.getByName("valueDbl")->getName(), fields.getByIndex(3)->getName()); 110 | CHECK_EQUAL(fields.getByName("valueTxt")->getName(), fields.getByIndex(4)->getName()); 111 | CHECK_EQUAL(fields.getByName("valueBol")->getName(), fields.getByIndex(5)->getName()); 112 | CHECK_EQUAL(fields.getByName("valueTme")->getName(), fields.getByIndex(6)->getName()); 113 | } 114 | 115 | //eofn 116 | }; 117 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestMemoryOutStream.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | 3 | #include "../MemoryOutStream.h" 4 | #include 5 | 6 | using namespace UnitTest; 7 | using namespace std; 8 | 9 | namespace { 10 | 11 | TEST(DefaultIsEmptyString) 12 | { 13 | MemoryOutStream const stream; 14 | CHECK(stream.GetText() != 0); 15 | CHECK_EQUAL("", stream.GetText()); 16 | } 17 | 18 | TEST(StreamingTextCopiesCharacters) 19 | { 20 | MemoryOutStream stream; 21 | stream << "Lalala"; 22 | CHECK_EQUAL("Lalala", stream.GetText()); 23 | } 24 | 25 | TEST(StreamingMultipleTimesConcatenatesResult) 26 | { 27 | MemoryOutStream stream; 28 | stream << "Bork" << "Foo" << "Bar"; 29 | CHECK_EQUAL("BorkFooBar", stream.GetText()); 30 | } 31 | 32 | TEST(StreamingIntWritesCorrectCharacters) 33 | { 34 | MemoryOutStream stream; 35 | stream << (int)123; 36 | CHECK_EQUAL("123", stream.GetText()); 37 | } 38 | 39 | TEST(StreamingUnsignedIntWritesCorrectCharacters) 40 | { 41 | MemoryOutStream stream; 42 | stream << (unsigned int)123; 43 | CHECK_EQUAL("123", stream.GetText()); 44 | } 45 | 46 | TEST(StreamingLongWritesCorrectCharacters) 47 | { 48 | MemoryOutStream stream; 49 | stream << (long)(-123); 50 | CHECK_EQUAL("-123", stream.GetText()); 51 | } 52 | 53 | TEST(StreamingUnsignedLongWritesCorrectCharacters) 54 | { 55 | MemoryOutStream stream; 56 | stream << (unsigned long)123; 57 | CHECK_EQUAL("123", stream.GetText()); 58 | } 59 | 60 | TEST(StreamingFloatWritesCorrectCharacters) 61 | { 62 | MemoryOutStream stream; 63 | stream << 3.1415f; 64 | CHECK(strstr(stream.GetText(), "3.1415")); 65 | } 66 | 67 | TEST(StreamingDoubleWritesCorrectCharacters) 68 | { 69 | MemoryOutStream stream; 70 | stream << 3.1415; 71 | CHECK(strstr(stream.GetText(), "3.1415")); 72 | } 73 | 74 | TEST(StreamingPointerWritesCorrectCharacters) 75 | { 76 | MemoryOutStream stream; 77 | int* p = (int*)0x1234; 78 | stream << p; 79 | CHECK(strstr(stream.GetText(), "1234")); 80 | } 81 | 82 | TEST(StreamingSizeTWritesCorrectCharacters) 83 | { 84 | MemoryOutStream stream; 85 | size_t const s = 53124; 86 | stream << s; 87 | CHECK_EQUAL("53124", stream.GetText()); 88 | } 89 | 90 | #ifdef UNITTEST_USE_CUSTOM_STREAMS 91 | 92 | TEST(StreamInitialCapacityIsCorrect) 93 | { 94 | MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE); 95 | CHECK_EQUAL((int)MemoryOutStream::GROW_CHUNK_SIZE, stream.GetCapacity()); 96 | } 97 | 98 | TEST(StreamInitialCapacityIsMultipleOfGrowChunkSize) 99 | { 100 | MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE + 1); 101 | CHECK_EQUAL((int)MemoryOutStream::GROW_CHUNK_SIZE * 2, stream.GetCapacity()); 102 | } 103 | 104 | 105 | TEST(ExceedingCapacityGrowsBuffer) 106 | { 107 | MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE); 108 | stream << "012345678901234567890123456789"; 109 | char const* const oldBuffer = stream.GetText(); 110 | stream << "0123456789"; 111 | CHECK(oldBuffer != stream.GetText()); 112 | } 113 | 114 | TEST(ExceedingCapacityGrowsBufferByGrowChunk) 115 | { 116 | MemoryOutStream stream(MemoryOutStream::GROW_CHUNK_SIZE); 117 | stream << "0123456789012345678901234567890123456789"; 118 | CHECK_EQUAL(MemoryOutStream::GROW_CHUNK_SIZE * 2, stream.GetCapacity()); 119 | } 120 | 121 | TEST(WritingStringLongerThanCapacityFitsInNewBuffer) 122 | { 123 | MemoryOutStream stream(8); 124 | stream << "0123456789ABCDEF"; 125 | CHECK_EQUAL("0123456789ABCDEF", stream.GetText()); 126 | } 127 | 128 | TEST(WritingIntLongerThanCapacityFitsInNewBuffer) 129 | { 130 | MemoryOutStream stream(8); 131 | stream << "aaaa" << 123456;; 132 | CHECK_EQUAL("aaaa123456", stream.GetText()); 133 | } 134 | 135 | TEST(WritingFloatLongerThanCapacityFitsInNewBuffer) 136 | { 137 | MemoryOutStream stream(8); 138 | stream << "aaaa" << 123456.0f;; 139 | CHECK_EQUAL("aaaa123456.000000f", stream.GetText()); 140 | } 141 | 142 | TEST(WritingSizeTLongerThanCapacityFitsInNewBuffer) 143 | { 144 | MemoryOutStream stream(8); 145 | stream << "aaaa" << size_t(32145); 146 | CHECK_EQUAL("aaaa32145", stream.GetText()); 147 | } 148 | 149 | #endif 150 | 151 | } 152 | -------------------------------------------------------------------------------- /easySQLite/SqlCommon.cpp: -------------------------------------------------------------------------------- 1 | #include "SqlCommon.h" 2 | #include "SHA1.h" 3 | #include 4 | 5 | namespace sql 6 | { 7 | time::time() 8 | { 9 | if (::time(&_value) == -1) 10 | _value = -1; 11 | } 12 | 13 | time::time(const time& value) 14 | { 15 | _value = value._value; 16 | } 17 | 18 | time::time(integer value) 19 | { 20 | _value = value; 21 | } 22 | 23 | time& time::operator=(const time& value) 24 | { 25 | if (this != &value) 26 | { 27 | this->_value = value._value; 28 | } 29 | return *this; 30 | } 31 | 32 | bool time::operator==(const time& value) 33 | { 34 | if (this == &value) 35 | return true; 36 | 37 | if (value._value == value._value) 38 | return true; 39 | 40 | return false; 41 | } 42 | 43 | time time::now() 44 | { 45 | time t; 46 | return t; 47 | } 48 | 49 | double time::diff(time& value) 50 | { 51 | return difftime(this->_value, value._value); 52 | } 53 | 54 | integer time::asInteger() 55 | { 56 | return _value; 57 | } 58 | 59 | time_t time::get() 60 | { 61 | return _value; 62 | } 63 | 64 | string time::format(const char* format) 65 | { 66 | string s; 67 | tm* localTime; 68 | char buffer[128]; 69 | 70 | if ((localTime = localtime(&_value)) == 0) 71 | if (strftime(buffer, 128, format, localTime) > 0) 72 | s = buffer; 73 | 74 | return s; 75 | } 76 | 77 | string time::asString() 78 | { 79 | return format("%d-%m-%Y %H:%M, %a"); 80 | } 81 | 82 | string time::asTimeString() 83 | { 84 | return format("%H:%M"); 85 | } 86 | 87 | string time::asDateString() 88 | { 89 | return format("%d-%m-%Y"); 90 | } 91 | 92 | void time::addValue(integer value) 93 | { 94 | _value += value; 95 | } 96 | 97 | void time::addMinutes(integer count) 98 | { 99 | _value += (60 * count); 100 | } 101 | 102 | void time::addHours(integer count) 103 | { 104 | _value += (3600 * count); 105 | } 106 | 107 | void time::addDays(integer count) 108 | { 109 | _value += ((3600 * 24) * count); 110 | } 111 | 112 | 113 | string intToStr(int value) 114 | { 115 | char buffer[32]; 116 | sprintf(buffer,"%d",value); 117 | return buffer; 118 | } 119 | 120 | string intToStr(integer value) 121 | { 122 | char buffer[64]; 123 | sprintf(buffer,"%lld",value); 124 | return buffer; 125 | } 126 | 127 | string quoteStr(string value) 128 | { 129 | string s; 130 | 131 | for (string::iterator it = value.begin(); it != value.end(); it++) 132 | { 133 | char c = *it; 134 | s += c; 135 | if (c == '\'') 136 | s += c; 137 | } 138 | 139 | return s; 140 | } 141 | 142 | string binToHex(const char* buffer, int size) 143 | { 144 | std::string s; 145 | 146 | char digit[4]; 147 | 148 | unsigned char* p = (unsigned char*)buffer; 149 | 150 | for (int index = 0; index < size; index++) 151 | { 152 | sprintf(digit, "%02x", *p++); 153 | s += digit; 154 | } 155 | 156 | return s; 157 | } 158 | 159 | string generateSHA(string& value) 160 | { 161 | CSHA1 sha; 162 | 163 | sha.Update((UINT_8*)value.c_str(), value.length()); 164 | 165 | sha.Final(); 166 | 167 | UINT_8 digest[20]; 168 | if (sha.GetHash(digest)) 169 | { 170 | const int size = sizeof(digest) / sizeof(UINT_8); 171 | return binToHex((char*)digest, size); 172 | } 173 | 174 | return ""; 175 | } 176 | 177 | string& trimleft(string& s) 178 | { 179 | string::iterator it; 180 | 181 | for( it = s.begin(); it != s.end(); it++ ) 182 | if( !isspace( *it ) ) 183 | break; 184 | 185 | s.erase( s.begin(), it ); 186 | return s; 187 | } 188 | 189 | string& trimright(string& s) 190 | { 191 | string::difference_type dt; 192 | string::reverse_iterator it; 193 | 194 | for( it = s.rbegin(); it != s.rend(); it++ ) 195 | if( !isspace( *it ) ) 196 | break; 197 | 198 | dt = s.rend() - it; 199 | 200 | s.erase( s.begin() + dt, s.end() ); 201 | return s; 202 | } 203 | 204 | string& trim(string& s) 205 | { 206 | trimleft( s ); 207 | trimright( s ); 208 | return s; 209 | } 210 | 211 | string trim(const string& s) 212 | { 213 | string t = s; 214 | return trim(t); 215 | } 216 | 217 | 218 | void listToVector(string s, std::vector& vector, const char* sep) 219 | { 220 | vector.clear(); 221 | 222 | char* buffer = new char[s.length() + 1]; 223 | 224 | memcpy(buffer, s.c_str(), s.length()); 225 | buffer[s.length()] = 0; 226 | 227 | char* token = strtok(buffer, sep); 228 | 229 | while (token) 230 | { 231 | string sToken = token; 232 | vector.push_back(trim(sToken)); 233 | token = strtok(NULL, sep); 234 | } 235 | 236 | delete [] buffer; 237 | } 238 | 239 | 240 | //sql eof 241 | }; 242 | -------------------------------------------------------------------------------- /Tests/TestSqlRecord.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++/src/UnitTest++.h" 2 | #include "../easySQLite/SqlRecord.h" 3 | 4 | 5 | namespace Tests 6 | { 7 | 8 | using namespace sql; 9 | 10 | 11 | TEST(RecordSetup) 12 | { 13 | Field def[] = 14 | { 15 | Field(FIELD_KEY), 16 | Field("name", type_text, flag_not_null), 17 | Field("valueInt", type_int), 18 | Field("valueDbl", type_float), 19 | Field("valueTxt", type_text), 20 | Field("valueBol", type_bool, flag_not_null), 21 | Field("valueTme", type_time), 22 | Field(DEFINITION_END), 23 | }; 24 | 25 | FieldSet fields(def); 26 | 27 | Record r(&fields); 28 | 29 | CHECK(r.fieldByName("name")); 30 | CHECK(r.fieldByName("valueInt")); 31 | CHECK(r.fieldByName("valueDbl")); 32 | CHECK(r.fieldByName("valueTxt")); 33 | CHECK(r.fieldByName("valueBol")); 34 | CHECK(r.fieldByName("valueTme")); 35 | 36 | CHECK_EQUAL(7, r.columnCount()); 37 | 38 | if (r.columnCount() == 7) 39 | { 40 | //check if values by name and by index are the same 41 | CHECK_EQUAL(r.getValue("_ID"), r.getValue(0)); 42 | CHECK_EQUAL(r.getValue("name"), r.getValue(1)); 43 | CHECK_EQUAL(r.getValue("valueInt"), r.getValue(2)); 44 | CHECK_EQUAL(r.getValue("valueDbl"), r.getValue(3)); 45 | CHECK_EQUAL(r.getValue("valueTxt"), r.getValue(4)); 46 | CHECK_EQUAL(r.getValue("valueBol"), r.getValue(5)); 47 | CHECK_EQUAL(r.getValue("valueTme"), r.getValue(6)); 48 | } 49 | } 50 | 51 | TEST(RecordQueries) 52 | { 53 | Field def[] = 54 | { 55 | Field(FIELD_KEY), 56 | Field("name", type_text, flag_not_null), 57 | Field("valueInt", type_int), 58 | Field("valueDbl", type_float), 59 | Field("valueTxt", type_text), 60 | Field("valueBol", type_bool, flag_not_null), 61 | Field("valueTme", type_time), 62 | Field(DEFINITION_END), 63 | }; 64 | 65 | FieldSet fields(def); 66 | 67 | Record r(&fields); 68 | 69 | Record rnew(r); 70 | 71 | CHECK_EQUAL(rnew.toSql(), r.toSql()); 72 | 73 | CHECK_EQUAL("insert into test (_ID, name, valueInt, valueDbl, valueTxt, valueBol, valueTme) values (null, null, null, null, null, null, null)", 74 | r.toSqlInsert("test")); 75 | 76 | CHECK_EQUAL("update test set name=null, valueInt=null, valueDbl=null, valueTxt=null, valueBol=null, valueTme=null where _ID = null", 77 | r.toSqlUpdate("test")); 78 | 79 | CHECK_EQUAL("null, null, null, null, null, null, null", 80 | r.toSql()); 81 | 82 | CHECK_EQUAL("null|null|null|null|null|null|null", 83 | r.toString()); 84 | } 85 | 86 | TEST(RecordSetValues) 87 | { 88 | Field def[] = 89 | { 90 | Field(FIELD_KEY), 91 | Field("name", type_text, flag_not_null), 92 | Field("valueInt", type_int), 93 | Field("valueDbl", type_float), 94 | Field("valueTxt", type_text), 95 | Field("valueBol", type_bool, flag_not_null), 96 | Field("valueTme", type_time), 97 | Field(DEFINITION_END), 98 | }; 99 | 100 | FieldSet fields(def); 101 | 102 | Record r(&fields); 103 | 104 | r.setInteger("_ID", 123); 105 | r.setString("name", "test_text1"); 106 | r.setInteger("valueInt", 890); 107 | r.setDouble("valueDbl", 0.345); 108 | r.setString("valueTxt", "test_text2"); 109 | r.setBool("valueBol", true); 110 | r.setTime("valueTme", 0); 111 | 112 | CHECK_EQUAL(123, r.getKeyIdValue()->asInteger()); 113 | 114 | CHECK_EQUAL("123|test_text1|890|0.34500000|test_text2|1|0", r.toString()); 115 | 116 | CHECK_EQUAL("123, 'test_text1', 890, 0.34500000, 'test_text2', 1, 0", r.toSql()); 117 | } 118 | 119 | TEST(RecordGetValues) 120 | { 121 | Field def[] = 122 | { 123 | Field(FIELD_KEY), 124 | Field("name", type_text, flag_not_null), 125 | Field("valueInt", type_int), 126 | Field("valueDbl", type_float), 127 | Field("valueTxt", type_text), 128 | Field("valueBol", type_bool, flag_not_null), 129 | Field("valueTme", type_time), 130 | Field(DEFINITION_END), 131 | }; 132 | 133 | FieldSet fields(def); 134 | 135 | Record r(&fields); 136 | 137 | r.setInteger("_ID", 123); 138 | r.setString("name", "test_text1"); 139 | r.setInteger("valueInt", 890); 140 | r.setDouble("valueDbl", 0.345); 141 | r.setString("valueTxt", "test_text2"); 142 | r.setBool("valueBol", true); 143 | r.setTime("valueTme", 0); 144 | 145 | CHECK_EQUAL(123, r.getValue("_ID")->asInteger()); 146 | CHECK_EQUAL("test_text1", r.getValue("name")->asString()); 147 | CHECK_EQUAL(890, r.getValue("valueInt")->asInteger()); 148 | CHECK_EQUAL(0.345, r.getValue("valueDbl")->asDouble()); 149 | CHECK_EQUAL("test_text2", r.getValue("valueTxt")->asString()); 150 | CHECK_EQUAL(true, r.getValue("valueBol")->asBool()); 151 | CHECK_EQUAL(0, r.getValue("valueTme")->asTime().asInteger()); 152 | } 153 | 154 | 155 | //eofn 156 | }; 157 | -------------------------------------------------------------------------------- /Tests/TestSqlTable.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++/src/UnitTest++.h" 2 | #include "../easySQLite/SqlRecordSet.h" 3 | #include "../easySQLite/SqlDatabase.h" 4 | #include "../easySQLite/SqlTable.h" 5 | 6 | 7 | namespace Tests 8 | { 9 | 10 | using namespace sql; 11 | 12 | 13 | static Field* definition() 14 | { 15 | static Field def[] = 16 | { 17 | Field(FIELD_KEY), 18 | Field("name", type_text, flag_not_null), 19 | Field("valueInt", type_int), 20 | Field("valueDbl", type_float), 21 | Field("valueTxt", type_text), 22 | Field("valueBol", type_bool, flag_not_null), 23 | Field("valueTme", type_time), 24 | Field(DEFINITION_END), 25 | }; 26 | 27 | return &def[0]; 28 | } 29 | 30 | static string strdef() 31 | { 32 | sql::string strdef = "CREATE TABLE test ("; 33 | strdef += "_ID INTEGER PRIMARY KEY, "; 34 | strdef += "name TEXT NOT NULL, "; 35 | strdef += "valueInt INTEGER, "; 36 | strdef += "valueDbl REAL, "; 37 | strdef += "valueTxt TEXT, "; 38 | strdef += "valueBol INTEGER NOT NULL, "; 39 | strdef += "valueTme INTEGER"; 40 | strdef += ")"; 41 | 42 | return strdef; 43 | } 44 | 45 | static void setValues(Record& r) 46 | { 47 | r.setString("name", "test_text1"); 48 | r.setInteger("valueInt", 890); 49 | r.setDouble("valueDbl", 0.345); 50 | r.setString("valueTxt", "test_text2"); 51 | r.setBool("valueBol", true); 52 | r.setTime("valueTme", 0); 53 | } 54 | 55 | TEST(TableSetup) 56 | { 57 | sql::Database db; 58 | 59 | try 60 | { 61 | db.open("UnitTests.db"); 62 | 63 | Table tb(db.getHandle(), "test", definition()); 64 | 65 | CHECK_EQUAL("test", tb.name()); 66 | 67 | CHECK_EQUAL(strdef(), tb.getDefinition()); 68 | 69 | if (tb.exists()) 70 | tb.remove(); 71 | 72 | tb.create(); 73 | 74 | CHECK(tb.exists()); 75 | 76 | Record r(tb.fields()); 77 | 78 | setValues(r); 79 | 80 | //add 10 records 81 | for (int index = 0; index < 10; index++) 82 | tb.addRecord(&r); 83 | 84 | tb.open(); 85 | 86 | CHECK_EQUAL(10, tb.recordCount()); 87 | 88 | //check _ID of record with index = 5 89 | CHECK_EQUAL(6, tb.getRecord(5)->getKeyIdValue()->asInteger()); 90 | 91 | //select 4 records 92 | tb.open("_ID >= 5 AND _ID <= 8"); 93 | 94 | CHECK_EQUAL(4, tb.recordCount()); 95 | 96 | //update data of record with _ID = 5 97 | if (Record* record = tb.getRecordByKeyId(5)) 98 | { 99 | r.setString("name", "new_text5"); 100 | r.setInteger("valueInt", 111); 101 | r.setDouble("valueDbl", 0.222); 102 | r.setString("valueTxt", "new_text8"); 103 | r.setBool("valueBol", false); 104 | r.setTime("valueTme", 1); 105 | 106 | tb.updateRecord(record); 107 | } else { 108 | CHECK_EQUAL("*", "record not found"); 109 | } 110 | 111 | //get updated record 112 | tb.open("_ID = 5"); 113 | 114 | //check returned result 115 | CHECK_EQUAL(1, tb.recordCount()); 116 | 117 | if (Record* record = tb.getRecord(0)) 118 | { 119 | CHECK_EQUAL("new_text5", r.getValue("name")->asString()); 120 | CHECK_EQUAL(111, r.getValue("valueInt")->asInteger()); 121 | CHECK_EQUAL(0.222, r.getValue("valueDbl")->asDouble()); 122 | CHECK_EQUAL("new_text8", r.getValue("valueTxt")->asString()); 123 | CHECK_EQUAL(false, r.getValue("valueBol")->asBool()); 124 | CHECK_EQUAL(1, r.getValue("valueTme")->asTime().asInteger()); 125 | } else { 126 | CHECK_EQUAL("*", "record not found"); 127 | } 128 | 129 | //delete 7 records 130 | tb.deleteRecords("_ID < 4"); 131 | tb.open(); 132 | 133 | CHECK_EQUAL(7, tb.recordCount()); 134 | 135 | //remove all records 136 | tb.truncate(); 137 | tb.open(); 138 | 139 | CHECK_EQUAL(0, tb.recordCount()); 140 | 141 | } catch (Exception e) { 142 | CHECK_EQUAL("*", e.msg()); 143 | } 144 | } 145 | 146 | TEST(TableCreationFromString) 147 | { 148 | sql::Database db; 149 | 150 | try 151 | { 152 | db.open("UnitTests.db"); 153 | 154 | Table tb(db.getHandle(), "test", definition()); 155 | 156 | sql::string strdef; 157 | strdef += "_ID INTEGER PRIMARY KEY, "; 158 | strdef += "name TEXT NOT NULL, "; 159 | strdef += "valueInt INTEGER, "; 160 | strdef += "valueDbl REAL, "; 161 | strdef += "valueTxt TEXT, "; 162 | strdef += "valueBol INTEGER NOT NULL, "; 163 | strdef += "valueTme INTEGER"; 164 | 165 | if (Table* table = Table::createFromDefinition(db.getHandle(), "test1", strdef)) 166 | { 167 | CHECK_EQUAL(table->fields()->definitionHash(), tb.fields()->definitionHash()); 168 | 169 | } else { 170 | CHECK_EQUAL("*", "table not created"); 171 | } 172 | 173 | } catch (Exception e) { 174 | CHECK_EQUAL("*", e.msg()); 175 | } 176 | } 177 | 178 | 179 | //eofn 180 | }; 181 | -------------------------------------------------------------------------------- /UnitTest++/src/CheckMacros.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_CHECKMACROS_H 2 | #define UNITTEST_CHECKMACROS_H 3 | 4 | #include "Checks.h" 5 | #include "AssertException.h" 6 | #include "MemoryOutStream.h" 7 | #include "TestDetails.h" 8 | #include "CurrentTest.h" 9 | 10 | #ifdef CHECK 11 | #error UnitTest++ redefines CHECK 12 | #endif 13 | 14 | #ifdef CHECK_EQUAL 15 | #error UnitTest++ redefines CHECK_EQUAL 16 | #endif 17 | 18 | #ifdef CHECK_CLOSE 19 | #error UnitTest++ redefines CHECK_CLOSE 20 | #endif 21 | 22 | #ifdef CHECK_ARRAY_EQUAL 23 | #error UnitTest++ redefines CHECK_ARRAY_EQUAL 24 | #endif 25 | 26 | #ifdef CHECK_ARRAY_CLOSE 27 | #error UnitTest++ redefines CHECK_ARRAY_CLOSE 28 | #endif 29 | 30 | #ifdef CHECK_ARRAY2D_CLOSE 31 | #error UnitTest++ redefines CHECK_ARRAY2D_CLOSE 32 | #endif 33 | 34 | #define CHECK(value) \ 35 | do \ 36 | { \ 37 | try { \ 38 | if (!UnitTest::Check(value)) \ 39 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \ 40 | } \ 41 | catch (...) { \ 42 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 43 | "Unhandled exception in CHECK(" #value ")"); \ 44 | } \ 45 | } while (0) 46 | 47 | #define CHECK_EQUAL(expected, actual) \ 48 | do \ 49 | { \ 50 | try { \ 51 | UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ 52 | } \ 53 | catch (...) { \ 54 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 55 | "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \ 56 | } \ 57 | } while (0) 58 | 59 | #define CHECK_CLOSE(expected, actual, tolerance) \ 60 | do \ 61 | { \ 62 | try { \ 63 | UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ 64 | } \ 65 | catch (...) { \ 66 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 67 | "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \ 68 | } \ 69 | } while (0) 70 | 71 | #define CHECK_ARRAY_EQUAL(expected, actual, count) \ 72 | do \ 73 | { \ 74 | try { \ 75 | UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ 76 | } \ 77 | catch (...) { \ 78 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 79 | "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \ 80 | } \ 81 | } while (0) 82 | 83 | #define CHECK_ARRAY_CLOSE(expected, actual, count, tolerance) \ 84 | do \ 85 | { \ 86 | try { \ 87 | UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ 88 | } \ 89 | catch (...) { \ 90 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 91 | "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \ 92 | } \ 93 | } while (0) 94 | 95 | #define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, tolerance) \ 96 | do \ 97 | { \ 98 | try { \ 99 | UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \ 100 | } \ 101 | catch (...) { \ 102 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \ 103 | "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \ 104 | } \ 105 | } while (0) 106 | 107 | 108 | #define CHECK_THROW(expression, ExpectedExceptionType) \ 109 | do \ 110 | { \ 111 | bool caught_ = false; \ 112 | try { expression; } \ 113 | catch (ExpectedExceptionType const&) { caught_ = true; } \ 114 | catch (...) {} \ 115 | if (!caught_) \ 116 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \ 117 | } while(0) 118 | 119 | #define CHECK_ASSERT(expression) \ 120 | CHECK_THROW(expression, UnitTest::AssertException); 121 | 122 | #endif 123 | -------------------------------------------------------------------------------- /UnitTest++/src/TestMacros.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_TESTMACROS_H 2 | #define UNITTEST_TESTMACROS_H 3 | 4 | #include "Config.h" 5 | #include "ExecuteTest.h" 6 | #include "AssertException.h" 7 | #include "TestDetails.h" 8 | #include "MemoryOutStream.h" 9 | 10 | #ifndef UNITTEST_POSIX 11 | #define UNITTEST_THROW_SIGNALS 12 | #else 13 | #include "Posix/SignalTranslator.h" 14 | #endif 15 | 16 | #ifdef TEST 17 | #error UnitTest++ redefines TEST 18 | #endif 19 | 20 | #ifdef TEST_EX 21 | #error UnitTest++ redefines TEST_EX 22 | #endif 23 | 24 | #ifdef TEST_FIXTURE_EX 25 | #error UnitTest++ redefines TEST_FIXTURE_EX 26 | #endif 27 | 28 | #define SUITE(Name) \ 29 | namespace Suite##Name { \ 30 | namespace UnitTestSuite { \ 31 | inline char const* GetSuiteName () { \ 32 | return #Name ; \ 33 | } \ 34 | } \ 35 | } \ 36 | namespace Suite##Name 37 | 38 | #define TEST_EX(Name, List) \ 39 | class Test##Name : public UnitTest::Test \ 40 | { \ 41 | public: \ 42 | Test##Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {} \ 43 | private: \ 44 | virtual void RunImpl() const; \ 45 | } test##Name##Instance; \ 46 | \ 47 | UnitTest::ListAdder adder##Name (List, &test##Name##Instance); \ 48 | \ 49 | void Test##Name::RunImpl() const 50 | 51 | 52 | #define TEST(Name) TEST_EX(Name, UnitTest::Test::GetTestList()) 53 | 54 | 55 | #define TEST_FIXTURE_EX(Fixture, Name, List) \ 56 | class Fixture##Name##Helper : public Fixture \ 57 | { \ 58 | public: \ 59 | explicit Fixture##Name##Helper(UnitTest::TestDetails const& details) : m_details(details) {} \ 60 | void RunImpl(); \ 61 | UnitTest::TestDetails const& m_details; \ 62 | private: \ 63 | Fixture##Name##Helper(Fixture##Name##Helper const&); \ 64 | Fixture##Name##Helper& operator =(Fixture##Name##Helper const&); \ 65 | }; \ 66 | \ 67 | class Test##Fixture##Name : public UnitTest::Test \ 68 | { \ 69 | public: \ 70 | Test##Fixture##Name() : Test(#Name, UnitTestSuite::GetSuiteName(), __FILE__, __LINE__) {} \ 71 | private: \ 72 | virtual void RunImpl() const; \ 73 | } test##Fixture##Name##Instance; \ 74 | \ 75 | UnitTest::ListAdder adder##Fixture##Name (List, &test##Fixture##Name##Instance); \ 76 | \ 77 | void Test##Fixture##Name::RunImpl() const \ 78 | { \ 79 | bool ctorOk = false; \ 80 | try { \ 81 | Fixture##Name##Helper fixtureHelper(m_details); \ 82 | ctorOk = true; \ 83 | UnitTest::ExecuteTest(fixtureHelper, m_details); \ 84 | } \ 85 | catch (UnitTest::AssertException const& e) \ 86 | { \ 87 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details.testName, m_details.suiteName, e.Filename(), e.LineNumber()), e.what()); \ 88 | } \ 89 | catch (std::exception const& e) \ 90 | { \ 91 | UnitTest::MemoryOutStream stream; \ 92 | stream << "Unhandled exception: " << e.what(); \ 93 | UnitTest::CurrentTest::Results()->OnTestFailure(m_details, stream.GetText()); \ 94 | } \ 95 | catch (...) { \ 96 | if (ctorOk) \ 97 | { \ 98 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \ 99 | "Unhandled exception while destroying fixture " #Fixture); \ 100 | } \ 101 | else \ 102 | { \ 103 | UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \ 104 | "Unhandled exception while constructing fixture " #Fixture); \ 105 | } \ 106 | } \ 107 | } \ 108 | void Fixture##Name##Helper::RunImpl() 109 | 110 | #define TEST_FIXTURE(Fixture,Name) TEST_FIXTURE_EX(Fixture, Name, UnitTest::Test::GetTestList()) 111 | 112 | 113 | #endif 114 | -------------------------------------------------------------------------------- /UnitTest++/src/Checks.h: -------------------------------------------------------------------------------- 1 | #ifndef UNITTEST_CHECKS_H 2 | #define UNITTEST_CHECKS_H 3 | 4 | #include "Config.h" 5 | #include "TestResults.h" 6 | #include "MemoryOutStream.h" 7 | 8 | namespace UnitTest { 9 | 10 | 11 | template< typename Value > 12 | bool Check(Value const value) 13 | { 14 | return !!value; // doing double negative to avoid silly VS warnings 15 | } 16 | 17 | 18 | template< typename Expected, typename Actual > 19 | void CheckEqual(TestResults& results, Expected const& expected, Actual const& actual, TestDetails const& details) 20 | { 21 | if (!(expected == actual)) 22 | { 23 | UnitTest::MemoryOutStream stream; 24 | stream << "Expected " << expected << " but was " << actual; 25 | 26 | results.OnTestFailure(details, stream.GetText()); 27 | } 28 | } 29 | 30 | void CheckEqual(TestResults& results, char const* expected, char const* actual, TestDetails const& details); 31 | 32 | void CheckEqual(TestResults& results, char* expected, char* actual, TestDetails const& details); 33 | 34 | void CheckEqual(TestResults& results, char* expected, char const* actual, TestDetails const& details); 35 | 36 | void CheckEqual(TestResults& results, char const* expected, char* actual, TestDetails const& details); 37 | 38 | template< typename Expected, typename Actual, typename Tolerance > 39 | bool AreClose(Expected const& expected, Actual const& actual, Tolerance const& tolerance) 40 | { 41 | return (actual >= (expected - tolerance)) && (actual <= (expected + tolerance)); 42 | } 43 | 44 | template< typename Expected, typename Actual, typename Tolerance > 45 | void CheckClose(TestResults& results, Expected const& expected, Actual const& actual, Tolerance const& tolerance, 46 | TestDetails const& details) 47 | { 48 | if (!AreClose(expected, actual, tolerance)) 49 | { 50 | UnitTest::MemoryOutStream stream; 51 | stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; 52 | 53 | results.OnTestFailure(details, stream.GetText()); 54 | } 55 | } 56 | 57 | 58 | template< typename Expected, typename Actual > 59 | void CheckArrayEqual(TestResults& results, Expected const& expected, Actual const& actual, 60 | int const count, TestDetails const& details) 61 | { 62 | bool equal = true; 63 | for (int i = 0; i < count; ++i) 64 | equal &= (expected[i] == actual[i]); 65 | 66 | if (!equal) 67 | { 68 | UnitTest::MemoryOutStream stream; 69 | 70 | stream << "Expected [ "; 71 | 72 | for (int expectedIndex = 0; expectedIndex < count; ++expectedIndex) 73 | stream << expected[expectedIndex] << " "; 74 | 75 | stream << "] but was [ "; 76 | 77 | for (int actualIndex = 0; actualIndex < count; ++actualIndex) 78 | stream << actual[actualIndex] << " "; 79 | 80 | stream << "]"; 81 | 82 | results.OnTestFailure(details, stream.GetText()); 83 | } 84 | } 85 | 86 | template< typename Expected, typename Actual, typename Tolerance > 87 | bool ArrayAreClose(Expected const& expected, Actual const& actual, int const count, Tolerance const& tolerance) 88 | { 89 | bool equal = true; 90 | for (int i = 0; i < count; ++i) 91 | equal &= AreClose(expected[i], actual[i], tolerance); 92 | return equal; 93 | } 94 | 95 | template< typename Expected, typename Actual, typename Tolerance > 96 | void CheckArrayClose(TestResults& results, Expected const& expected, Actual const& actual, 97 | int const count, Tolerance const& tolerance, TestDetails const& details) 98 | { 99 | bool equal = ArrayAreClose(expected, actual, count, tolerance); 100 | 101 | if (!equal) 102 | { 103 | UnitTest::MemoryOutStream stream; 104 | 105 | stream << "Expected [ "; 106 | for (int expectedIndex = 0; expectedIndex < count; ++expectedIndex) 107 | stream << expected[expectedIndex] << " "; 108 | stream << "] +/- " << tolerance << " but was [ "; 109 | 110 | for (int actualIndex = 0; actualIndex < count; ++actualIndex) 111 | stream << actual[actualIndex] << " "; 112 | stream << "]"; 113 | 114 | results.OnTestFailure(details, stream.GetText()); 115 | } 116 | } 117 | 118 | template< typename Expected, typename Actual, typename Tolerance > 119 | void CheckArray2DClose(TestResults& results, Expected const& expected, Actual const& actual, 120 | int const rows, int const columns, Tolerance const& tolerance, TestDetails const& details) 121 | { 122 | bool equal = true; 123 | for (int i = 0; i < rows; ++i) 124 | equal &= ArrayAreClose(expected[i], actual[i], columns, tolerance); 125 | 126 | if (!equal) 127 | { 128 | UnitTest::MemoryOutStream stream; 129 | 130 | stream << "Expected [ "; 131 | 132 | for (int expectedRow = 0; expectedRow < rows; ++expectedRow) 133 | { 134 | stream << "[ "; 135 | for (int expectedColumn = 0; expectedColumn < columns; ++expectedColumn) 136 | stream << expected[expectedRow][expectedColumn] << " "; 137 | stream << "] "; 138 | } 139 | 140 | stream << "] +/- " << tolerance << " but was [ "; 141 | 142 | for (int actualRow = 0; actualRow < rows; ++actualRow) 143 | { 144 | stream << "[ "; 145 | for (int actualColumn = 0; actualColumn < columns; ++actualColumn) 146 | stream << actual[actualRow][actualColumn] << " "; 147 | stream << "] "; 148 | } 149 | 150 | stream << "]"; 151 | 152 | results.OnTestFailure(details, stream.GetText()); 153 | } 154 | } 155 | 156 | } 157 | 158 | #endif 159 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestTestMacros.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | #include "../TestMacros.h" 3 | #include "../TestList.h" 4 | #include "../TestResults.h" 5 | #include "../TestReporter.h" 6 | #include "../ReportAssert.h" 7 | #include "RecordingReporter.h" 8 | #include "ScopedCurrentTest.h" 9 | 10 | using namespace UnitTest; 11 | 12 | namespace { 13 | 14 | TestList list1; 15 | TEST_EX(DummyTest, list1) 16 | { 17 | } 18 | 19 | TEST (TestsAreAddedToTheListThroughMacro) 20 | { 21 | CHECK(list1.GetHead() != 0); 22 | CHECK(list1.GetHead()->next == 0); 23 | } 24 | 25 | struct ThrowingThingie 26 | { 27 | ThrowingThingie() : dummy(false) 28 | { 29 | if (!dummy) 30 | throw "Oops"; 31 | } 32 | 33 | bool dummy; 34 | }; 35 | 36 | TestList list2; 37 | TEST_FIXTURE_EX(ThrowingThingie, DummyTestName, list2) 38 | { 39 | } 40 | 41 | TEST (ExceptionsInFixtureAreReportedAsHappeningInTheFixture) 42 | { 43 | RecordingReporter reporter; 44 | TestResults result(&reporter); 45 | { 46 | ScopedCurrentTest scopedResults(result); 47 | list2.GetHead()->Run(); 48 | } 49 | 50 | CHECK(strstr(reporter.lastFailedMessage, "xception")); 51 | CHECK(strstr(reporter.lastFailedMessage, "fixture")); 52 | CHECK(strstr(reporter.lastFailedMessage, "ThrowingThingie")); 53 | } 54 | 55 | struct DummyFixture 56 | { 57 | int x; 58 | }; 59 | 60 | // We're really testing the macros so we just want them to compile and link 61 | SUITE(TestSuite1) 62 | { 63 | TEST(SimilarlyNamedTestsInDifferentSuitesWork) 64 | { 65 | } 66 | 67 | TEST_FIXTURE(DummyFixture, SimilarlyNamedFixtureTestsInDifferentSuitesWork) 68 | { 69 | } 70 | } 71 | 72 | SUITE(TestSuite2) 73 | { 74 | TEST(SimilarlyNamedTestsInDifferentSuitesWork) 75 | { 76 | } 77 | 78 | TEST_FIXTURE(DummyFixture,SimilarlyNamedFixtureTestsInDifferentSuitesWork) 79 | { 80 | } 81 | } 82 | 83 | TestList macroTestList1; 84 | TEST_EX(MacroTestHelper1, macroTestList1) 85 | { 86 | } 87 | 88 | TEST(TestAddedWithTEST_EXMacroGetsDefaultSuite) 89 | { 90 | CHECK(macroTestList1.GetHead() != NULL); 91 | CHECK_EQUAL ("MacroTestHelper1", macroTestList1.GetHead()->m_details.testName); 92 | CHECK_EQUAL ("DefaultSuite", macroTestList1.GetHead()->m_details.suiteName); 93 | } 94 | 95 | TestList macroTestList2; 96 | TEST_FIXTURE_EX(DummyFixture, MacroTestHelper2, macroTestList2) 97 | { 98 | } 99 | 100 | TEST(TestAddedWithTEST_FIXTURE_EXMacroGetsDefaultSuite) 101 | { 102 | CHECK(macroTestList2.GetHead() != NULL); 103 | CHECK_EQUAL ("MacroTestHelper2", macroTestList2.GetHead()->m_details.testName); 104 | CHECK_EQUAL ("DefaultSuite", macroTestList2.GetHead()->m_details.suiteName); 105 | } 106 | 107 | struct FixtureCtorThrows 108 | { 109 | FixtureCtorThrows() { throw "exception"; } 110 | }; 111 | 112 | TestList throwingFixtureTestList1; 113 | TEST_FIXTURE_EX(FixtureCtorThrows, FixtureCtorThrowsTestName, throwingFixtureTestList1) 114 | { 115 | } 116 | 117 | TEST(FixturesWithThrowingCtorsAreFailures) 118 | { 119 | CHECK(throwingFixtureTestList1.GetHead() != NULL); 120 | RecordingReporter reporter; 121 | TestResults result(&reporter); 122 | { 123 | ScopedCurrentTest scopedResult(result); 124 | throwingFixtureTestList1.GetHead()->Run(); 125 | } 126 | 127 | int const failureCount = result.GetFailedTestCount(); 128 | CHECK_EQUAL(1, failureCount); 129 | CHECK(strstr(reporter.lastFailedMessage, "while constructing fixture")); 130 | } 131 | 132 | struct FixtureDtorThrows 133 | { 134 | ~FixtureDtorThrows() { throw "exception"; } 135 | }; 136 | 137 | TestList throwingFixtureTestList2; 138 | TEST_FIXTURE_EX(FixtureDtorThrows, FixtureDtorThrowsTestName, throwingFixtureTestList2) 139 | { 140 | } 141 | 142 | TEST(FixturesWithThrowingDtorsAreFailures) 143 | { 144 | CHECK(throwingFixtureTestList2.GetHead() != NULL); 145 | 146 | RecordingReporter reporter; 147 | TestResults result(&reporter); 148 | { 149 | ScopedCurrentTest scopedResult(result); 150 | throwingFixtureTestList2.GetHead()->Run(); 151 | } 152 | 153 | int const failureCount = result.GetFailedTestCount(); 154 | CHECK_EQUAL(1, failureCount); 155 | CHECK(strstr(reporter.lastFailedMessage, "while destroying fixture")); 156 | } 157 | 158 | const int FailingLine = 123; 159 | 160 | struct FixtureCtorAsserts 161 | { 162 | FixtureCtorAsserts() 163 | { 164 | UnitTest::ReportAssert("assert failure", "file", FailingLine); 165 | } 166 | }; 167 | 168 | TestList ctorAssertFixtureTestList; 169 | TEST_FIXTURE_EX(FixtureCtorAsserts, CorrectlyReportsAssertFailureInCtor, ctorAssertFixtureTestList) 170 | { 171 | } 172 | 173 | TEST(CorrectlyReportsFixturesWithCtorsThatAssert) 174 | { 175 | RecordingReporter reporter; 176 | TestResults result(&reporter); 177 | { 178 | ScopedCurrentTest scopedResults(result); 179 | ctorAssertFixtureTestList.GetHead()->Run(); 180 | } 181 | 182 | const int failureCount = result.GetFailedTestCount(); 183 | CHECK_EQUAL(1, failureCount); 184 | CHECK_EQUAL(FailingLine, reporter.lastFailedLine); 185 | CHECK(strstr(reporter.lastFailedMessage, "assert failure")); 186 | } 187 | 188 | } 189 | 190 | // We're really testing if it's possible to use the same suite in two files 191 | // to compile and link successfuly (TestTestSuite.cpp has suite with the same name) 192 | // Note: we are outside of the anonymous namespace 193 | SUITE(SameTestSuite) 194 | { 195 | TEST(DummyTest1) 196 | { 197 | } 198 | } 199 | 200 | #define CUR_TEST_NAME CurrentTestDetailsContainCurrentTestInfo 201 | #define INNER_STRINGIFY(X) #X 202 | #define STRINGIFY(X) INNER_STRINGIFY(X) 203 | 204 | TEST(CUR_TEST_NAME) 205 | { 206 | const UnitTest::TestDetails* details = CurrentTest::Details(); 207 | CHECK_EQUAL(STRINGIFY(CUR_TEST_NAME), details->testName); 208 | } 209 | 210 | #undef CUR_TEST_NAME 211 | #undef INNER_STRINGIFY 212 | #undef STRINGIFY 213 | -------------------------------------------------------------------------------- /UnitTest++/UnitTest++.vsnet2003.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 12 | 13 | 14 | 20 | 31 | 33 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 55 | 61 | 69 | 71 | 74 | 76 | 78 | 80 | 82 | 84 | 86 | 88 | 90 | 92 | 93 | 94 | 95 | 96 | 97 | 99 | 101 | 102 | 104 | 105 | 106 | 108 | 109 | 111 | 112 | 114 | 115 | 117 | 118 | 120 | 121 | 123 | 124 | 126 | 127 | 129 | 130 | 132 | 133 | 135 | 136 | 138 | 139 | 141 | 142 | 144 | 145 | 147 | 148 | 150 | 151 | 153 | 154 | 156 | 157 | 159 | 160 | 162 | 163 | 165 | 166 | 168 | 169 | 171 | 172 | 174 | 175 | 177 | 178 | 180 | 181 | 183 | 184 | 186 | 187 | 189 | 190 | 192 | 193 | 195 | 196 | 198 | 199 | 201 | 202 | 204 | 205 | 207 | 208 | 210 | 211 | 213 | 214 | 216 | 217 | 219 | 220 | 222 | 223 | 224 | 225 | 226 | 227 | -------------------------------------------------------------------------------- /UnitTest++/TestUnitTest++.vsnet2005.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 25 | 28 | 31 | 34 | 37 | 40 | 53 | 56 | 59 | 62 | 69 | 72 | 75 | 78 | 81 | 84 | 87 | 90 | 94 | 95 | 103 | 106 | 109 | 112 | 115 | 118 | 129 | 132 | 135 | 138 | 147 | 150 | 153 | 156 | 159 | 162 | 165 | 168 | 172 | 173 | 174 | 175 | 176 | 177 | 180 | 181 | 184 | 185 | 188 | 189 | 192 | 193 | 196 | 197 | 200 | 201 | 204 | 205 | 208 | 209 | 212 | 213 | 216 | 217 | 220 | 221 | 224 | 225 | 228 | 229 | 232 | 233 | 236 | 237 | 240 | 241 | 244 | 245 | 248 | 249 | 252 | 253 | 254 | 255 | 256 | 257 | -------------------------------------------------------------------------------- /UnitTest++/src/tests/TestXmlTestReporter.cpp: -------------------------------------------------------------------------------- 1 | #include "../UnitTest++.h" 2 | #include "../XmlTestReporter.h" 3 | 4 | #include 5 | 6 | using namespace UnitTest; 7 | using std::ostringstream; 8 | 9 | namespace 10 | { 11 | 12 | #ifdef UNITTEST_USE_CUSTOM_STREAMS 13 | 14 | // Overload to let MemoryOutStream accept std::string 15 | MemoryOutStream& operator<<(MemoryOutStream& s, const std::string& value) 16 | { 17 | s << value.c_str(); 18 | return s; 19 | } 20 | 21 | #endif 22 | 23 | struct XmlTestReporterFixture 24 | { 25 | XmlTestReporterFixture() 26 | : reporter(output) 27 | { 28 | } 29 | 30 | ostringstream output; 31 | XmlTestReporter reporter; 32 | }; 33 | 34 | TEST_FIXTURE(XmlTestReporterFixture, MultipleCharactersAreEscaped) 35 | { 36 | TestDetails const details("TestName", "suite", "filename.h", 4321); 37 | 38 | reporter.ReportTestStart(details); 39 | reporter.ReportFailure(details, "\"\"\'\'&&<<>>"); 40 | reporter.ReportTestFinish(details, 0.1f); 41 | reporter.ReportSummary(1, 2, 3, 0.1f); 42 | 43 | char const* expected = 44 | "" 45 | "" 46 | "" 47 | "" 49 | "" 50 | ""; 51 | 52 | CHECK_EQUAL(expected, output.str()); 53 | } 54 | 55 | TEST_FIXTURE(XmlTestReporterFixture, OutputIsCachedUntilReportSummaryIsCalled) 56 | { 57 | TestDetails const details("", "", "", 0); 58 | 59 | reporter.ReportTestStart(details); 60 | reporter.ReportFailure(details, "message"); 61 | reporter.ReportTestFinish(details, 1.0F); 62 | CHECK(output.str().empty()); 63 | 64 | reporter.ReportSummary(1, 1, 1, 1.0f); 65 | CHECK(!output.str().empty()); 66 | } 67 | 68 | TEST_FIXTURE(XmlTestReporterFixture, EmptyReportSummaryFormat) 69 | { 70 | reporter.ReportSummary(0, 0, 0, 0.1f); 71 | 72 | const char *expected = 73 | "" 74 | "" 75 | ""; 76 | 77 | CHECK_EQUAL(expected, output.str()); 78 | } 79 | 80 | TEST_FIXTURE(XmlTestReporterFixture, SingleSuccessfulTestReportSummaryFormat) 81 | { 82 | TestDetails const details("TestName", "DefaultSuite", "", 0); 83 | 84 | reporter.ReportTestStart(details); 85 | reporter.ReportSummary(1, 0, 0, 0.1f); 86 | 87 | const char *expected = 88 | "" 89 | "" 90 | "" 91 | ""; 92 | 93 | CHECK_EQUAL(expected, output.str()); 94 | } 95 | 96 | TEST_FIXTURE(XmlTestReporterFixture, SingleFailedTestReportSummaryFormat) 97 | { 98 | TestDetails const details("A Test", "suite", "A File", 4321); 99 | 100 | reporter.ReportTestStart(details); 101 | reporter.ReportFailure(details, "A Failure"); 102 | reporter.ReportSummary(1, 1, 1, 0.1f); 103 | 104 | const char *expected = 105 | "" 106 | "" 107 | "" 108 | "" 109 | "" 110 | ""; 111 | 112 | CHECK_EQUAL(expected, output.str()); 113 | } 114 | 115 | TEST_FIXTURE(XmlTestReporterFixture, FailureMessageIsXMLEscaped) 116 | { 117 | TestDetails const details("TestName", "suite", "filename.h", 4321); 118 | 119 | reporter.ReportTestStart(details); 120 | reporter.ReportFailure(details, "\"\'&<>"); 121 | reporter.ReportTestFinish(details, 0.1f); 122 | reporter.ReportSummary(1, 1, 1, 0.1f); 123 | 124 | char const* expected = 125 | "" 126 | "" 127 | "" 128 | "" 129 | "" 130 | ""; 131 | 132 | CHECK_EQUAL(expected, output.str()); 133 | } 134 | 135 | TEST_FIXTURE(XmlTestReporterFixture, OneFailureAndOneSuccess) 136 | { 137 | TestDetails const failedDetails("FailedTest", "suite", "fail.h", 1); 138 | reporter.ReportTestStart(failedDetails); 139 | reporter.ReportFailure(failedDetails, "expected 1 but was 2"); 140 | reporter.ReportTestFinish(failedDetails, 0.1f); 141 | 142 | TestDetails const succeededDetails("SucceededTest", "suite", "", 0); 143 | reporter.ReportTestStart(succeededDetails); 144 | reporter.ReportTestFinish(succeededDetails, 1.0f); 145 | reporter.ReportSummary(2, 1, 1, 1.1f); 146 | 147 | char const* expected = 148 | "" 149 | "" 150 | "" 151 | "" 152 | "" 153 | "" 154 | ""; 155 | 156 | CHECK_EQUAL(expected, output.str()); 157 | } 158 | 159 | TEST_FIXTURE(XmlTestReporterFixture, MultipleFailures) 160 | { 161 | TestDetails const failedDetails1("FailedTest", "suite", "fail.h", 1); 162 | TestDetails const failedDetails2("FailedTest", "suite", "fail.h", 31); 163 | 164 | reporter.ReportTestStart(failedDetails1); 165 | reporter.ReportFailure(failedDetails1, "expected 1 but was 2"); 166 | reporter.ReportFailure(failedDetails2, "expected one but was two"); 167 | reporter.ReportTestFinish(failedDetails1, 0.1f); 168 | 169 | reporter.ReportSummary(1, 1, 2, 1.1f); 170 | 171 | char const* expected = 172 | "" 173 | "" 174 | "" 175 | "" 176 | "" 177 | "" 178 | ""; 179 | 180 | CHECK_EQUAL(expected, output.str()); 181 | } 182 | 183 | } 184 | -------------------------------------------------------------------------------- /easySQLite/SqlTable.cpp: -------------------------------------------------------------------------------- 1 | #include "SqlTable.h" 2 | 3 | 4 | namespace sql 5 | { 6 | 7 | Table::Table(sqlite3* db, string tableName, Field* definition) 8 | : _db(db), _tableName(tableName), _recordset(db, definition) 9 | { 10 | } 11 | 12 | Table::Table(sqlite3* db, string tableName, FieldSet* fields) 13 | : _db(db), _tableName(tableName), _recordset(db, fields) 14 | { 15 | } 16 | 17 | string Table::name() 18 | { 19 | return _tableName; 20 | } 21 | 22 | string Table::getDefinition() 23 | { 24 | return "CREATE TABLE " + _tableName + " (" + fields()->getDefinition() + ")"; 25 | } 26 | 27 | FieldSet* Table::fields() 28 | { 29 | return _recordset.fields(); 30 | } 31 | 32 | string Table::toString() 33 | { 34 | string s; 35 | 36 | for (int index = 0; index < fields()->count(); index++) 37 | { 38 | if (Field* f = fields()->getByIndex(index)) 39 | { 40 | s += intToStr(index + 1) + ". " + f->getDefinition(); 41 | 42 | if (index < (fields()->count() - 1)) 43 | s +="\r\n"; 44 | } 45 | } 46 | 47 | return s; 48 | } 49 | 50 | string Table::errMsg() 51 | { 52 | return _recordset.errMsg(); 53 | } 54 | 55 | sqlite3* Table::getHandle() 56 | { 57 | return _db; 58 | } 59 | 60 | bool Table::create() 61 | { 62 | if (exists()) 63 | return true; 64 | 65 | const string sqlDefinition = getDefinition(); 66 | 67 | if (_recordset.query(sqlDefinition)) 68 | { 69 | return true; 70 | } 71 | 72 | return false; 73 | } 74 | 75 | bool Table::query(string queryStr) 76 | { 77 | if (_recordset.query(queryStr)) 78 | { 79 | return true; 80 | } 81 | return false; 82 | } 83 | 84 | bool Table::open() 85 | { 86 | const string queryStr = "select * from " + _tableName; 87 | 88 | if (_recordset.query(queryStr)) 89 | { 90 | return true; 91 | } 92 | 93 | return false; 94 | } 95 | 96 | //example of whereCondition: 97 | //"_ID > 40" 98 | bool Table::open(string whereCondition) 99 | { 100 | const string queryStr = "select * from " + _tableName + (whereCondition.empty() ? "" : " where " + whereCondition); 101 | 102 | if (_recordset.query(queryStr)) 103 | { 104 | return true; 105 | } 106 | 107 | return false; 108 | } 109 | 110 | //example of sortBy: 111 | //"_ID desc" 112 | bool Table::open(string whereCondition, string sortBy) 113 | { 114 | const string queryStr = "select * from " + _tableName 115 | + (whereCondition.empty() ? "" : " where " + whereCondition) 116 | + (sortBy.empty() ? "" : " order by " + sortBy); 117 | 118 | if (_recordset.query(queryStr)) 119 | { 120 | return true; 121 | } 122 | 123 | return false; 124 | } 125 | 126 | bool Table::truncate() 127 | { 128 | const string queryStr = "delete from " + _tableName; 129 | 130 | if (_recordset.query(queryStr)) 131 | { 132 | return true; 133 | } 134 | 135 | return false; 136 | } 137 | 138 | bool Table::remove() 139 | { 140 | const string queryStr = "drop table " + _tableName; 141 | 142 | if (_recordset.query(queryStr)) 143 | { 144 | return true; 145 | } 146 | 147 | return false; 148 | } 149 | 150 | bool Table::deleteRecords(string whereCondition) 151 | { 152 | const string sql = "delete from " + _tableName + (whereCondition.empty() ? "" : " where " + whereCondition); 153 | 154 | RecordSet rs(_db, _recordset.fields()); 155 | 156 | if (rs.query(sql)) 157 | { 158 | return true; 159 | } 160 | 161 | return false; 162 | } 163 | 164 | int Table::recordCount() 165 | { 166 | return _recordset.count(); 167 | } 168 | 169 | int Table::totalRecordCount() 170 | { 171 | const string queryStr = "select count(*) from " + _tableName; 172 | 173 | RecordSet rs(_db, _recordset.fields()); 174 | 175 | if (rs.query(queryStr)) 176 | { 177 | if (Value* value = rs.getTopRecordFirstValue()) 178 | { 179 | return (int)value->asInteger(); 180 | } 181 | } 182 | 183 | return -1; 184 | } 185 | 186 | bool Table::exists() 187 | { 188 | const string queryStr = "select count(*) from sqlite_master where type = 'table' and name = '" + _tableName + "'"; 189 | 190 | RecordSet rs(_db, _recordset.fields()); 191 | 192 | if (rs.query(queryStr)) 193 | { 194 | if (Value* value = rs.getTopRecordFirstValue()) 195 | { 196 | return (value->asInteger() > 0); 197 | } 198 | } 199 | 200 | return false; 201 | } 202 | 203 | Record* Table::getRecord(int record_index) 204 | { 205 | return _recordset.getRecord(record_index); 206 | } 207 | 208 | Record* Table::getTopRecord() 209 | { 210 | return getRecord(0); 211 | } 212 | 213 | Record* Table::getRecordByKeyId(integer keyId) 214 | { 215 | const string queryStr = "select * from " + _tableName + " where _ID = " + intToStr(keyId); 216 | 217 | if (_recordset.query(queryStr)) 218 | { 219 | if (_recordset.count() > 0) 220 | { 221 | return _recordset.getRecord(0); 222 | } 223 | } 224 | 225 | return NULL; 226 | } 227 | 228 | bool Table::addRecord(Record* record) 229 | { 230 | if (record) 231 | { 232 | const string sql = record->toSqlInsert(name()); 233 | 234 | RecordSet rs(_db, _recordset.fields()); 235 | 236 | if (rs.query(sql)) 237 | { 238 | return true; 239 | } 240 | } 241 | return false; 242 | } 243 | 244 | bool Table::updateRecord(Record* record) 245 | { 246 | if (record) 247 | { 248 | const string sql = record->toSqlUpdate(name()); 249 | 250 | RecordSet rs(_db, _recordset.fields()); 251 | 252 | if (rs.query(sql)) 253 | { 254 | return true; 255 | } 256 | } 257 | return false; 258 | } 259 | 260 | bool Table::copyRecords(Table& source) 261 | { 262 | for (int index = 0; index < source.recordCount(); index++) 263 | { 264 | if (Record* record = source.getRecord(index)) 265 | { 266 | if (!addRecord(record)) 267 | return false; 268 | } 269 | } 270 | return true; 271 | } 272 | 273 | Table* Table::createFromDefinition(sqlite3* db, string tableName, string fieldsDefinition) 274 | { 275 | if (FieldSet* fields = FieldSet::createFromDefinition(fieldsDefinition)) 276 | { 277 | Table* table = new Table(db, tableName, fields); 278 | 279 | delete fields; 280 | 281 | return table; 282 | } 283 | 284 | return NULL; 285 | } 286 | 287 | bool Table::backup(Table& source) 288 | { 289 | bool bResult = false; 290 | 291 | if (exists()) 292 | remove(); 293 | 294 | if (create()) 295 | { 296 | if (source.open()) 297 | { 298 | if (copyRecords(source)) 299 | { 300 | if (source.totalRecordCount() == totalRecordCount()) 301 | bResult = true; 302 | } 303 | } 304 | } 305 | 306 | return bResult; 307 | } 308 | 309 | 310 | //sql eof 311 | }; 312 | -------------------------------------------------------------------------------- /_ReadMe.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | easySQLite short help 10 | 68 | 69 | 70 | 71 |

easySQLite
a C++ SQLite wrapper

72 | 73 |

This is C++ API wrapper for SQLite C API database engine. It was written to simplify and speedup coding 74 | of local database access.

75 | 76 |

easySQLite is released under BSD License.

77 | 78 |

easySQLite was written and tested with Visual C++ 2010 Express, but is not 79 | using any nonstandard extensions, so should compile with any other C++ compiler.

80 | 81 |

How To Start ?

82 | 83 |

First, include "SqlCommon.h" and other headers if required to your project.

84 | 85 |

easySQLite uses sql namespace.

86 | 87 |

At begin, you should create database table structure as Field's objects array:

88 | 89 |
 90 | 
 91 | Field definition_tbPerson[] =
 92 | {
 93 | 	Field(FIELD_KEY),
 94 | 	Field("fname", type_text, flag_not_null),
 95 | 	Field("lname", type_text, flag_not_null),
 96 | 	Field("birthdate", type_time),
 97 | 	Field(DEFINITION_END),
 98 | };
 99 | 
100 | 	
101 | 102 | Default Field object constructor parameters: 103 |
    104 |
  • name
    (sql::string)
  • 105 |
  • type
    (sql::field_type)
  • 106 |
  • flags
    (sql::field_flags). Can be OR'ed
  • 107 |
108 | 109 |

Table structure definition should begin with Field(FIELD_KEY) and end with Field(DEFINITION_END).

110 | 111 |

Field(FIELD_KEY) is like Field("_ID", type_int, flag_primary_key).

112 | 113 |

Now, you are ready to open/create database file. Define database object and then open database file.

114 | 115 | 116 |
117 | sql::Database db;
118 | 
119 | try
120 | {
121 | 	db.open("test.db");
122 | 	//...
123 | 
124 | } catch (Exception e) {
125 | 	//...
126 | }	
127 | 	
128 |
129 | 130 |

TIP: you can check for errors by using return bool value of any method and ::errMsg() of used object. 131 | Default behaviour for most objects is to use exceptions.

132 | 133 |

Disable USE_EXCEPTIONS directive in SqlCommon.h to "manually" check for result values, for example:

134 | 135 | 136 |
137 | if (!db.open("test.db"))
138 | {
139 | 	log(db.errMsg());
140 | }
141 | 	
142 |
143 | 144 |

When database file is up and ready, you can define new Table object for it:

145 | 146 | 147 |

148 | sql::Table tbPerson(db.getHandle(), "person", definition_tbPerson);	
149 | 	
150 | 151 | 152 | Default Table object constructor parameters: 153 |
    154 |
  • db handle
    (sqlite3*)
  • 155 |
  • tableName
    (sql::string)
  • 156 |
  • fields definition
    (sql::Field*). Field* array or FieldSet* of another table.
  • 157 |
158 | 159 |

Now, the simplest way to interact with data is to use Table object methods, for example:

160 | 161 | 162 |
163 | //remove table from database if exists
164 | if (tbPerson.exists())
165 | 	tbPerson.remove();
166 | 
167 | //create new table
168 | tbPerson.create();
169 | 
170 | //removes all records
171 | tbPerson.truncate();
172 | 
173 | //loads all records to internal recordset
174 | tbPerson.open();
175 | 	
176 | //loads one record
177 | tbPerson.open("_ID == 5");
178 | 
179 | //returns loaded records count
180 | tbPerson.recordCount();
181 | 	
182 |
183 | 184 |

Modify Or Query Data

185 | 186 |

To make modifications of your data or query them, you must use Record object.

187 | 188 |

To add (insert) new record to table, define Record with table FieldSet definition. 189 | Then you can set some data to fields and just insert new record to table:

190 | 191 | 192 |
193 | Record record(tbPerson.fields());
194 | 
195 | record.setString("fname", "Jan");
196 | record.setString("lname", "Kowalski");
197 | record.setTime("birthdate", time::now());
198 | 
199 | tbPerson.addRecord(&record);
200 | 	
201 |
202 | 203 |

Query records and update at once:

204 | 205 | 206 |
207 | tbPerson.open("_ID >= 10 and _ID <= 15");
208 | 
209 | for (int index = 0; index < tbPerson.recordCount(); index++)
210 | {
211 | 	if (Record* record = tbPerson.getRecord(index))
212 | 	{
213 | 		record->setString("fname", "");
214 | 		record->setString("lname", "Nowak");
215 | 		record->setNull("birthdate");
216 | 
217 | 		tbPerson.updateRecord(record);
218 | 	}
219 | }	
220 | 	
221 |
222 | 223 |

List all records:

224 | 225 | 226 |
227 | tbPerson.open();
228 | 
229 | for (int index = 0; index < tbPerson.recordCount(); index++)
230 | 	if (Record* record = tbPerson.getRecord(index))
231 | 		log(record->toString());
232 | 	
233 |
234 | 235 |

Get one record by key field (_ID):

236 | 237 | 238 |
239 | if (Record* record = tbPerson.getRecordByKeyId(7))
240 | {
241 | 	//...
242 | }
243 | 	
244 |
245 | 246 |

You can find more usage examples in easySQLite.cpp project file.

247 | 248 |

Author

249 | 250 |

Copyright (c) 2010, Piotr Zagawa

251 | 252 |

All rights reserved.

253 | 254 |

contact 255 | website

256 | 257 | 258 | 259 | 260 | -------------------------------------------------------------------------------- /easySQLite/SqlRecord.cpp: -------------------------------------------------------------------------------- 1 | #include "SqlRecord.h" 2 | #include "SqlFieldSet.h" 3 | 4 | namespace sql 5 | { 6 | 7 | Record::Record(FieldSet* fields) 8 | : _fields(fields) 9 | { 10 | initColumnCount(_fields->count()); 11 | } 12 | 13 | Record::Record(Record* record) 14 | : _fields(record->_fields) 15 | { 16 | initColumnCount(_fields->count()); 17 | _values = record->_values; 18 | } 19 | 20 | Record::Record(const Record& record) 21 | : _fields(record._fields) 22 | { 23 | initColumnCount(_fields->count()); 24 | _values = record._values; 25 | } 26 | 27 | FieldSet* Record::fields() 28 | { 29 | return _fields; 30 | } 31 | 32 | void Record::initColumnCount(int columns) 33 | { 34 | _values.resize(columns); 35 | } 36 | 37 | void Record::initColumnValue(int column_index, char* value, field_type type) 38 | { 39 | _values[column_index].setValue(value, type); 40 | } 41 | 42 | int Record::columnCount() 43 | { 44 | return _values.size(); 45 | } 46 | 47 | Value* Record::getValue(int column_index) 48 | { 49 | if ((column_index >= 0) && (column_index < (int)_values.size())) 50 | return &_values.at(column_index); 51 | 52 | return NULL; 53 | } 54 | 55 | Value* Record::getValue(string fieldName) 56 | { 57 | if (Field* field = _fields->getByName(fieldName)) 58 | return getValue(field->getIndex()); 59 | 60 | return NULL; 61 | } 62 | 63 | Value* Record::getKeyIdValue() 64 | { 65 | for (int index = 0; index < _fields->count(); index++) 66 | { 67 | if (Field* field = _fields->getByIndex(index)) 68 | { 69 | if (field->isKeyIdField()) 70 | return getValue(field->getIndex()); 71 | } 72 | } 73 | 74 | return NULL; 75 | } 76 | 77 | string Record::toString() 78 | { 79 | string s; 80 | 81 | for (int column = 0; column < columnCount(); column++) 82 | if (Value* value = getValue(column)) 83 | { 84 | s += value->toString(); 85 | if (column < (columnCount() - 1)) 86 | s += "|"; 87 | } 88 | 89 | return s; 90 | } 91 | 92 | string Record::toSql() 93 | { 94 | string s; 95 | 96 | for (int index = 0; index < _fields->count(); index++) 97 | { 98 | if (Field* field = _fields->getByIndex(index)) 99 | { 100 | if (Value* value = getValue(field->getName())) 101 | { 102 | s += value->toSql(field->getType()); 103 | 104 | if (index < (_fields->count() - 1)) 105 | s += ", "; 106 | } 107 | } 108 | } 109 | 110 | return s; 111 | } 112 | 113 | void Record::setNull(int index) 114 | { 115 | if (Value* v = getValue(index)) 116 | v->setNull(); 117 | } 118 | 119 | void Record::setString(int index, string value) 120 | { 121 | if (Value* v = getValue(index)) 122 | v->setString(value); 123 | } 124 | 125 | void Record::setInteger(int index, integer value) 126 | { 127 | if (Value* v = getValue(index)) 128 | v->setInteger(value); 129 | } 130 | 131 | void Record::setDouble(int index, double value) 132 | { 133 | if (Value* v = getValue(index)) 134 | v->setDouble(value); 135 | } 136 | 137 | void Record::setBool(int index, bool value) 138 | { 139 | if (Value* v = getValue(index)) 140 | v->setBool(value); 141 | } 142 | 143 | void Record::setTime(int index, time value) 144 | { 145 | if (Value* v = getValue(index)) 146 | v->setTime(value); 147 | } 148 | 149 | Field* Record::fieldByName(string fieldName) 150 | { 151 | if (Field* field = _fields->getByName(fieldName)) 152 | { 153 | return field; 154 | } else { 155 | THROW_EXCEPTION("Record::fieldByName: field '" + fieldName + "' not found") 156 | return NULL; 157 | } 158 | } 159 | 160 | void Record::setNull(string fieldName) 161 | { 162 | if (Field* field = fieldByName(fieldName)) 163 | setNull(field->getIndex()); 164 | } 165 | 166 | void Record::setString(string fieldName, string value) 167 | { 168 | if (Field* field = fieldByName(fieldName)) 169 | setString(field->getIndex(), value); 170 | } 171 | 172 | void Record::setInteger(string fieldName, integer value) 173 | { 174 | if (Field* field = fieldByName(fieldName)) 175 | setInteger(field->getIndex(), value); 176 | } 177 | 178 | void Record::setDouble(string fieldName, double value) 179 | { 180 | if (Field* field = fieldByName(fieldName)) 181 | setDouble(field->getIndex(), value); 182 | } 183 | 184 | void Record::setBool(string fieldName, bool value) 185 | { 186 | if (Field* field = fieldByName(fieldName)) 187 | setBool(field->getIndex(), value); 188 | } 189 | 190 | void Record::setTime(string fieldName, time value) 191 | { 192 | if (Field* field = fieldByName(fieldName)) 193 | setTime(field->getIndex(), value); 194 | } 195 | 196 | string Record::toSqlInsert(string tableName) 197 | { 198 | string s = "insert into " + tableName + " "; 199 | 200 | s += "(" + _fields->toString() + ")"; 201 | 202 | s += " values "; 203 | 204 | s += "(" + toSql() + ")"; 205 | 206 | return s; 207 | } 208 | 209 | string Record::toSqlUpdate(string tableName) 210 | { 211 | string s = "update " + tableName + " set "; 212 | 213 | for (int index = 0; index < _fields->count(); index++) 214 | { 215 | if (Field* field = _fields->getByIndex(index)) 216 | { 217 | if (field->isKeyIdField()) 218 | continue; 219 | 220 | if (Value* value = getValue(field->getName())) 221 | { 222 | s += field->getName() + "=" + value->toSql(field->getType()); 223 | 224 | if (index < (_fields->count() - 1)) 225 | s += ", "; 226 | } 227 | } 228 | } 229 | 230 | if (Value* value = getKeyIdValue()) 231 | s += " where _ID = " + value->toSql(type_int); 232 | 233 | return s; 234 | } 235 | 236 | bool Record::equalsColumnValue(Record* record, string fieldName) 237 | { 238 | if (record) 239 | { 240 | if (Value* value1 = getValue(fieldName)) 241 | if (Value* value2 = record->getValue(fieldName)) 242 | return value1->equals(*value2); 243 | } 244 | return false; 245 | } 246 | 247 | bool Record::equalsValues(Record* record) 248 | { 249 | if (record) 250 | { 251 | for (int index = 0; index < _fields->count(); index++) 252 | { 253 | if (Field* field = _fields->getByIndex(index)) 254 | { 255 | if (field->isKeyIdField()) 256 | continue; 257 | 258 | if (Value* value1 = getValue(field->getName())) 259 | if (Value* value2 = record->getValue(field->getName())) 260 | if (!value1->equals(*value2)) 261 | return false; 262 | } 263 | } 264 | return true; 265 | } 266 | return false; 267 | } 268 | 269 | 270 | //sql eof 271 | 272 | void Record::setNull(Field& field) 273 | { 274 | setNull(field.getName()); 275 | } 276 | 277 | void Record::setString(Field& field, string value) 278 | { 279 | setString(field.getName(),value); 280 | } 281 | 282 | void Record::setInteger(Field& field, integer value) 283 | { 284 | setInteger(field.getName(),value); 285 | } 286 | 287 | void Record::setDouble(Field& field, double value) 288 | { 289 | setDouble(field.getName(),value); 290 | } 291 | 292 | Value* Record::getValue(const Field& field) 293 | { 294 | return getValue(field.getName()); 295 | } 296 | 297 | void Record::setBool(Field& field, bool value) 298 | { 299 | setBool(field.getName(),value); 300 | } 301 | 302 | void Record::setTime(Field& field, time value) 303 | { 304 | setTime(field.getName(),value); 305 | } 306 | 307 | } 308 | ; 309 | -------------------------------------------------------------------------------- /UnitTest++/UnitTest++.vsnet2005.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 25 | 28 | 31 | 34 | 37 | 40 | 53 | 56 | 59 | 62 | 65 | 68 | 71 | 74 | 77 | 80 | 81 | 89 | 92 | 95 | 98 | 101 | 104 | 115 | 118 | 121 | 124 | 127 | 130 | 133 | 136 | 139 | 142 | 143 | 144 | 145 | 146 | 147 | 150 | 153 | 154 | 157 | 158 | 159 | 162 | 163 | 166 | 167 | 170 | 171 | 174 | 175 | 178 | 179 | 182 | 183 | 186 | 187 | 190 | 191 | 194 | 195 | 198 | 199 | 202 | 203 | 206 | 207 | 210 | 211 | 214 | 215 | 218 | 219 | 222 | 223 | 226 | 227 | 230 | 231 | 234 | 235 | 238 | 239 | 242 | 243 | 246 | 247 | 250 | 251 | 254 | 255 | 258 | 259 | 262 | 263 | 266 | 267 | 270 | 271 | 274 | 275 | 278 | 279 | 282 | 283 | 286 | 287 | 290 | 291 | 294 | 295 | 298 | 299 | 302 | 303 | 306 | 307 | 310 | 311 | 314 | 315 | 316 | 317 | 318 | 319 | --------------------------------------------------------------------------------