diff options
author | Constantin Michael <constantin@codesynthesis.com> | 2011-07-14 17:11:01 +0200 |
---|---|---|
committer | Constantin Michael <constantin@codesynthesis.com> | 2011-07-14 23:12:28 +0200 |
commit | e67830fa25b5cf42428d32b544b115fda44383a3 (patch) | |
tree | d8082a82f9fd05fdfa8d5d7a1353e9091e68eec2 /pgsql/types/test.hxx | |
parent | 92c6c45e7d3d95ef6241502824239a28a38dd7c2 (diff) |
Test VARBIT bit length is handled correctly
Diffstat (limited to 'pgsql/types/test.hxx')
-rw-r--r-- | pgsql/types/test.hxx | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/pgsql/types/test.hxx b/pgsql/types/test.hxx index 39de674..8b42aa1 100644 --- a/pgsql/types/test.hxx +++ b/pgsql/types/test.hxx @@ -10,6 +10,7 @@ #include <string> #include <memory> // std::auto_ptr #include <cstring> // std::memcmp +#include <cstddef> // std::size_t #include <odb/core.hxx> @@ -33,6 +34,42 @@ operator== (bitfield x, bitfield y) x.d == y.d; } +struct varbit +{ + std::size_t size; + ubuffer ubuffer_; + + bool + compare (const varbit& x) const + { + if (size != x.size) + return false; + + std::size_t byte_len = size / 8; + + if (std::memcmp (ubuffer_.data (), x.ubuffer_.data (), byte_len != 0)) + return false; + + std::size_t trailing_bits = size % 8; + + if (trailing_bits != 0) + { + unsigned char mask (0xFFU << (8 - trailing_bits)); + + return (ubuffer_.data ()[byte_len] & mask) == + (x.ubuffer_.data ()[byte_len] & mask); + } + + return true; + } +}; + +inline bool +operator== (const varbit& x, const varbit& y) +{ + return x.compare (y); +} + #pragma db value(bitfield) type ("BIT(4) NOT NULL") typedef std::auto_ptr<std::string> string_ptr; @@ -108,7 +145,7 @@ struct object buffer bytea_; #pragma db type ("VARBIT(1024) NOT NULL") - buffer varbit_; + varbit varbit_; // #pragma db type ("BIT(4) NOT NULL") - assigned by #pragma db value bitfield bit_; |