Skip to content

Commit 1309a0d

Browse files
author
Edward Rosten
committed
Fix missing inlines and add first unit test.
Note: To make a unit test, drop a .cc file in the tests directory. If all is OK, exit with status 0 and print the line OK. Otherwise, exit with an error message and nonzero code.
1 parent ca117be commit 1309a0d

File tree

3 files changed

+144
-12
lines changed

3 files changed

+144
-12
lines changed

Makefile.in

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,77 @@ install:
3131

3232
clean:
3333
rm -f src/*.o src/test
34+
rm -f tests/*.result tests/*.test tests/*.result_ prog *.o
3435

3536
src/test: src/test.o
3637
$(CXX) -o $@ $^ $(LDFLAGS)
38+
39+
#Every .cc file in the tests directory is a test
40+
TESTS=$(notdir $(basename $(wildcard tests/*.cc)))
41+
42+
43+
#Get the intermediate file names from the list of tests.
44+
TEST_RESULT=$(TESTS:%=tests/%.result)
45+
46+
47+
# Don't delete the intermediate files, since these can take a
48+
# long time to regenerate
49+
.PRECIOUS: tests/%.result_ tests/%.test
50+
51+
#Add the rule "test" so make test works. It's not a real file, so
52+
#mark it as phony
53+
.PHONY: test
54+
test:tests/results
55+
56+
57+
#We don't want this file hanging around on failure since we
58+
#want the build depend on it. If we leave it behing then typing make
59+
#twice in a row will suceed, since make will find the file and not try
60+
#to rebuild it.
61+
.DELETE_ON_ERROR: tests/results
62+
63+
tests/results:$(TEST_RESULT)
64+
cat $(TEST_RESULT) > tests/results
65+
@echo -------------- Test Results ---------------
66+
@cat tests/results
67+
@echo -------------------------------------------
68+
@ ! grep -qv OK tests/results
69+
70+
71+
#Build a test executable from a test program. On compile error,
72+
#create an executable which declares the error.
73+
tests/%.test: tests/%.cc
74+
$(CXX) $(CXXFLAGS) $< -o $@ -I . $(LDFLAGS) ||\
75+
{ \
76+
echo "echo 'Compile error!' ; return 126" > $@ ; \
77+
chmod +x $@; \
78+
}
79+
80+
#Run the program and either use it's output (it should just say OK)
81+
#or a failure message
82+
tests/%.result_: tests/%.test
83+
$< > $@ ; \
84+
a=$$? ;\
85+
if [ $$a != 0 ]; \
86+
then \
87+
if [ $$a -ge 128 and ] ; \
88+
then \
89+
echo Crash!! > $@ ; \
90+
elif [ $$a -ne 126 ] ;\
91+
then \
92+
echo Failed > $@ ; \
93+
fi;\
94+
else\
95+
echo OK >> $@;\
96+
fi
97+
98+
tests/%.result: tests/%.result_
99+
echo $*: `tail -1 $<` > $@
100+
101+
#Get the C style dependencies working. Note we need to massage the test dependencies
102+
#to make the filenames correct
103+
.deps:
104+
rm -f .deps .sourcefiles
105+
find . -name "*.cc" | xargs -IQQQ $(CXX) $(CXXFLAGS) -MM -MG QQQ | sed -e'/test/s!\(.*\)\.o:!tests/\1.test:!' > .deps
106+
107+
include .deps

hdr/sqlite_modern_cpp.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,15 @@ class binder {
376376
};
377377

378378
// int
379-
template<> database_binder&& operator <<(database_binder&& db,int const&& val) {
379+
template<> inline database_binder&& operator <<(database_binder&& db,int const&& val) {
380380
int hresult;
381381
if((hresult = sqlite3_bind_int(db._stmt, db._inx, val)) != SQLITE_OK) {
382382
db.throw_sqlite_error(hresult);
383383
}
384384
++db._inx;
385385
return std::move(db);
386386
}
387-
template<> void get_col_from_db(database_binder& db, int inx, int& val) {
387+
template<> inline void get_col_from_db(database_binder& db, int inx, int& val) {
388388
if(sqlite3_column_type(db._stmt, inx) == SQLITE_NULL) {
389389
val = 0;
390390
} else {
@@ -393,7 +393,7 @@ template<> void get_col_from_db(database_binder& db, int inx, int& val) {
393393
}
394394

395395
// sqlite_int64
396-
template<> database_binder&& operator <<(database_binder&& db, sqlite_int64 const&& val) {
396+
template<> inline database_binder&& operator <<(database_binder&& db, sqlite_int64 const&& val) {
397397
int hresult;
398398
if((hresult = sqlite3_bind_int64(db._stmt, db._inx, val)) != SQLITE_OK) {
399399
db.throw_sqlite_error(hresult);
@@ -402,7 +402,7 @@ template<> database_binder&& operator <<(database_binder&& db, sqlite_int64 cons
402402
++db._inx;
403403
return std::move(db);
404404
}
405-
template<> void get_col_from_db(database_binder& db,int inx, sqlite3_int64& i) {
405+
template<> inline void get_col_from_db(database_binder& db,int inx, sqlite3_int64& i) {
406406
if(sqlite3_column_type(db._stmt, inx) == SQLITE_NULL) {
407407
i = 0;
408408
} else {
@@ -411,7 +411,7 @@ template<> void get_col_from_db(database_binder& db,int inx, sqlite3_int64& i) {
411411
}
412412

413413
// float
414-
template<> database_binder&& operator <<(database_binder&& db,float const&& val) {
414+
template<> inline database_binder&& operator <<(database_binder&& db,float const&& val) {
415415
int hresult;
416416
if((hresult = sqlite3_bind_double(db._stmt, db._inx, double(val))) != SQLITE_OK) {
417417
db.throw_sqlite_error(hresult);
@@ -420,7 +420,7 @@ template<> database_binder&& operator <<(database_binder&& db,float const&& val)
420420
++db._inx;
421421
return std::move(db);
422422
}
423-
template<> void get_col_from_db(database_binder& db, int inx, float& f) {
423+
template<> inline void get_col_from_db(database_binder& db, int inx, float& f) {
424424
if(sqlite3_column_type(db._stmt, inx) == SQLITE_NULL) {
425425
f = 0;
426426
} else {
@@ -429,7 +429,7 @@ template<> void get_col_from_db(database_binder& db, int inx, float& f) {
429429
}
430430

431431
// double
432-
template<> database_binder&& operator <<(database_binder&& db,double const&& val) {
432+
template<> inline database_binder&& operator <<(database_binder&& db,double const&& val) {
433433
int hresult;
434434
if((hresult = sqlite3_bind_double(db._stmt, db._inx, val)) != SQLITE_OK) {
435435
db.throw_sqlite_error(hresult);
@@ -438,7 +438,7 @@ template<> database_binder&& operator <<(database_binder&& db,double const&& val
438438
++db._inx;
439439
return std::move(db);
440440
}
441-
template<> void get_col_from_db(database_binder& db, int inx, double& d) {
441+
template<> inline void get_col_from_db(database_binder& db, int inx, double& d) {
442442
if(sqlite3_column_type(db._stmt, inx) == SQLITE_NULL) {
443443
d = 0;
444444
} else {
@@ -447,15 +447,15 @@ template<> void get_col_from_db(database_binder& db, int inx, double& d) {
447447
}
448448

449449
// std::string
450-
template<> void get_col_from_db(database_binder& db, int inx,std::string & s) {
450+
template<> inline void get_col_from_db(database_binder& db, int inx,std::string & s) {
451451
if(sqlite3_column_type(db._stmt, inx) == SQLITE_NULL) {
452452
s = std::string();
453453
} else {
454454
sqlite3_column_bytes(db._stmt, inx);
455455
s = std::string((char*)sqlite3_column_text(db._stmt, inx));
456456
}
457457
}
458-
template<> database_binder&& operator <<(database_binder&& db, std::string const&& txt) {
458+
template<> inline database_binder&& operator <<(database_binder&& db, std::string const&& txt) {
459459
int hresult;
460460
if((hresult = sqlite3_bind_text(db._stmt, db._inx, txt.data(), -1, SQLITE_TRANSIENT)) != SQLITE_OK) {
461461
db.throw_sqlite_error(hresult);
@@ -465,15 +465,15 @@ template<> database_binder&& operator <<(database_binder&& db, std::string const
465465
return std::move(db);
466466
}
467467
// std::u16string
468-
template<> void get_col_from_db(database_binder& db, int inx, std::u16string & w) {
468+
template<> inline void get_col_from_db(database_binder& db, int inx, std::u16string & w) {
469469
if(sqlite3_column_type(db._stmt, inx) == SQLITE_NULL) {
470470
w = std::u16string();
471471
} else {
472472
sqlite3_column_bytes16(db._stmt, inx);
473473
w = std::u16string((char16_t *)sqlite3_column_text16(db._stmt, inx));
474474
}
475475
}
476-
template<> database_binder&& operator <<(database_binder&& db, std::u16string const&& txt) {
476+
template<> inline database_binder&& operator <<(database_binder&& db, std::u16string const&& txt) {
477477
int hresult;
478478
if((hresult = sqlite3_bind_text16(db._stmt, db._inx, txt.data(), -1, SQLITE_TRANSIENT)) != SQLITE_OK) {
479479
db.throw_sqlite_error(hresult);

tests/simple_examples.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
#include <unistd.h>
4+
#include <sqlite_modern_cpp.h>
5+
using namespace sqlite;
6+
using namespace std;
7+
8+
struct TmpFile
9+
{
10+
string fname;
11+
12+
TmpFile()
13+
{
14+
char f[]="/tmp/sqlite_modern_cpp_test_XXXXXX";
15+
int fid = mkstemp(f);
16+
close(fid);
17+
18+
fname = f;
19+
}
20+
21+
~TmpFile()
22+
{
23+
unlink(fname.c_str());
24+
}
25+
};
26+
27+
int main()
28+
{
29+
try
30+
{
31+
TmpFile file;
32+
database db(file.fname);
33+
34+
db << "CREATE TABLE foo (a integer, b string);";
35+
db << "INSERT INTO foo VALUES (?, ?)" << 1 << "hello";
36+
db << "INSERT INTO foo VALUES (?, ?)" << 2 << "world";
37+
38+
string str;
39+
db << "SELECT b from FOO where a=?;" << 2 >> str;
40+
41+
if(str != "world")
42+
{
43+
cout << "Bad result on line " << __LINE__ << endl;
44+
exit(1);
45+
}
46+
47+
48+
}
49+
catch(sqlite_exception e)
50+
{
51+
cout << "Unexpected error " << e.what() << endl;
52+
exit(1);
53+
}
54+
catch(...)
55+
{
56+
cout << "Unknown error\n";
57+
exit(1);
58+
}
59+
60+
cout << "OK\n";
61+
}

0 commit comments

Comments
 (0)