string: add tests for character_position

This commit is contained in:
Danny Robson 2018-10-17 11:48:03 +11:00
parent 98e935d4d7
commit e2eaa1a7af
2 changed files with 40 additions and 0 deletions

View File

@ -149,6 +149,12 @@ namespace cruft {
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
/// Calculate the line and column of an iterator within a view. /// Calculate the line and column of an iterator within a view.
///
/// Returns an anonymous struct containing the line and column number.
///
/// The column number for newline is undefined. However it should never
/// return the value {0,-1} and so it should not result in underruns when
/// offsetting a pointer using the column index.
template <typename IteratorT> template <typename IteratorT>
auto auto
character_position ( character_position (

View File

@ -6,6 +6,39 @@
#include <vector> #include <vector>
///////////////////////////////////////////////////////////////////////////////
void
test_position (cruft::TAP::logger &tap)
{
char const *string = "a\nb\nc\n";
struct {
int offset;
int line;
int column;
} TESTS[] = {
{ 0, 0, 0 },
{ 1, 1, -1 },
{ 2, 1, 0 },
{ 3, 2, -1 },
{ 4, 2, 0 },
{ 5, 3, -1 },
};
for (auto const &t: TESTS) {
auto const pos = cruft::character_position ({string, strlen(string) }, string + t.offset);
tap.expect (
pos.line == t.line && pos.column == t.column,
"character_position %!:%!",
t.line,
t.column
);
}
}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void void
test_tokeniser (cruft::TAP::logger &tap) test_tokeniser (cruft::TAP::logger &tap)
@ -43,6 +76,7 @@ main (int, char**)
cruft::TAP::logger tap; cruft::TAP::logger tap;
test_tokeniser (tap); test_tokeniser (tap);
test_position (tap);
return tap.status (); return tap.status ();
} }