Skip to content

Commit

Permalink
Unit-test encoding handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jtv committed Jan 27, 2019
1 parent 1ac9662 commit 5e12316
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 5 deletions.
1 change: 1 addition & 0 deletions test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ file(
test_binarystring.cxx
test_cancel_query.cxx
test_cursor.cxx
test_encodings.cxx
test_error_verbosity.cxx
test_errorhandler.cxx
test_escape.cxx
Expand Down
1 change: 1 addition & 0 deletions test/unit/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ runner_SOURCES = \
test_binarystring.cxx \
test_cancel_query.cxx \
test_cursor.cxx \
test_encodings.cxx \
test_error_verbosity.cxx \
test_errorhandler.cxx \
test_escape.cxx \
Expand Down
12 changes: 7 additions & 5 deletions test/unit/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ CONFIG_CLEAN_VPATH_FILES =
am__EXEEXT_1 = runner$(EXEEXT)
am_runner_OBJECTS = test_array.$(OBJEXT) test_binarystring.$(OBJEXT) \
test_cancel_query.$(OBJEXT) test_cursor.$(OBJEXT) \
test_error_verbosity.$(OBJEXT) test_errorhandler.$(OBJEXT) \
test_escape.$(OBJEXT) test_exceptions.$(OBJEXT) \
test_float.$(OBJEXT) test_notification.$(OBJEXT) \
test_parameterized.$(OBJEXT) test_pipeline.$(OBJEXT) \
test_prepared_statement.$(OBJEXT) \
test_encodings.$(OBJEXT) test_error_verbosity.$(OBJEXT) \
test_errorhandler.$(OBJEXT) test_escape.$(OBJEXT) \
test_exceptions.$(OBJEXT) test_float.$(OBJEXT) \
test_notification.$(OBJEXT) test_parameterized.$(OBJEXT) \
test_pipeline.$(OBJEXT) test_prepared_statement.$(OBJEXT) \
test_read_transaction.$(OBJEXT) \
test_result_iteration.$(OBJEXT) test_result_slicing.$(OBJEXT) \
test_row.$(OBJEXT) test_simultaneous_transactions.$(OBJEXT) \
Expand Down Expand Up @@ -377,6 +377,7 @@ runner_SOURCES = \
test_binarystring.cxx \
test_cancel_query.cxx \
test_cursor.cxx \
test_encodings.cxx \
test_error_verbosity.cxx \
test_errorhandler.cxx \
test_escape.cxx \
Expand Down Expand Up @@ -462,6 +463,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_binarystring.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_cancel_query.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_cursor.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_encodings.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_error_verbosity.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_errorhandler.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/test_escape.Po@am__quote@
Expand Down
106 changes: 106 additions & 0 deletions test/unit/test_encodings.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include "../test_helpers.hxx"

#include "pqxx/internal/encodings.hxx"


namespace
{
void test_scan_ascii()
{
const auto scan = pqxx::internal::get_glyph_scanner(
pqxx::internal::encoding_group::MONOBYTE);
const std::string text{"hello"};

PQXX_CHECK_EQUAL(
scan(text.c_str(), text.size(), 0),
1ul,
"Monobyte scanner acting up.");
PQXX_CHECK_EQUAL(
scan(text.c_str(), text.size(), 1),
2ul,
"Monobyte scanner is inconsistent.");
}


void test_scan_utf8()
{
const auto scan = pqxx::internal::get_glyph_scanner(
pqxx::internal::encoding_group::UTF8);

// Thai: "Khrab".
const std::string text{
"\xe0\xb8\x95\xe0\xb8\xa3\xe0\xb8\xb1\xe0\xb8\x9a"};
PQXX_CHECK_EQUAL(
scan(text.c_str(), text.size(), 0),
3ul,
"UTF-8 scanner mis-scanned Thai khor khwai.");
PQXX_CHECK_EQUAL(
scan(text.c_str(), text.size(), 3),
6ul,
"UTF-8 scanner mis-scanned Thai ror reua.");
}


void test_for_glyphs_empty()
{
bool iterated = false;
pqxx::internal::for_glyphs(
pqxx::internal::encoding_group::MONOBYTE,
[&iterated](const char *, const char *){ iterated = true; },
"",
0);
PQXX_CHECK(!iterated, "Empty string went through an iteration.");
}


void test_for_glyphs_ascii()
{
const std::string text{"hi"};
std::vector<std::size_t> points;

pqxx::internal::for_glyphs(
pqxx::internal::encoding_group::UTF8,
[&points](const char *gbegin, const char *gend){
points.push_back(gend - gbegin);
},
text.c_str(),
text.size());

PQXX_CHECK_EQUAL(points.size(), 2ul, "Wrong number of ASCII iterations.");
PQXX_CHECK_EQUAL(points[0], 1u, "ASCII iteration started off wrong.");
PQXX_CHECK_EQUAL(points[1], 1u, "ASCII iteration was inconsistent.");
}


void test_for_glyphs_utf8()
{
// Greek: alpha omega.
const std::string text{"\xce\x91\xce\xa9"};
std::vector<std::size_t> points;

pqxx::internal::for_glyphs(
pqxx::internal::encoding_group::UTF8,
[&points](const char *gbegin, const char *gend){
points.push_back(gend - gbegin);
},
text.c_str(),
text.size());

PQXX_CHECK_EQUAL(points.size(), 2u, "Wrong number of UTF-8 iterations.");
PQXX_CHECK_EQUAL(points[0], 2u, "UTF-8 iteration started off wrong.");
PQXX_CHECK_EQUAL(points[1], 2u, "ASCII iteration was inconsistent.");
}


void test_encodings()
{
test_scan_ascii();
test_scan_utf8();
test_for_glyphs_empty();
test_for_glyphs_ascii();
test_for_glyphs_utf8();
}


PQXX_REGISTER_TEST(test_encodings);
} // namespace
3 changes: 3 additions & 0 deletions win32/vc-test-unit.mak
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ OBJS= \
$(INTDIR)\test_binarystring.obj \
$(INTDIR)\test_cancel_query.obj \
$(INTDIR)\test_cursor.obj \
$(INTDIR)\test_encodings.obj \
$(INTDIR)\test_error_verbosity.obj \
$(INTDIR)\test_errorhandler.obj \
$(INTDIR)\test_escape.obj \
Expand Down Expand Up @@ -150,6 +151,8 @@ $(INTDIR)\test_cancel_query.obj:
@$(CXX) $(CXX_FLAGS) test/unit/test_cancel_query.cxx /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\"
$(INTDIR)\test_cursor.obj:
@$(CXX) $(CXX_FLAGS) test/unit/test_cursor.cxx /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\"
$(INTDIR)\test_encodings.obj:
@$(CXX) $(CXX_FLAGS) test/unit/test_encodings.cxx /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\"
$(INTDIR)\test_error_verbosity.obj:
@$(CXX) $(CXX_FLAGS) test/unit/test_error_verbosity.cxx /Fo"$(INTDIR)\\" /Fd"$(INTDIR)\\"
$(INTDIR)\test_errorhandler.obj:
Expand Down

0 comments on commit 5e12316

Please sign in to comment.