Skip to content
Snippets Groups Projects
Commit 5a10461f authored by Sam Yates's avatar Sam Yates
Browse files

Coding standards, unit test for mask_stream.

parent f98898b2
No related branches found
No related tags found
No related merge requests found
...@@ -171,7 +171,7 @@ private: ...@@ -171,7 +171,7 @@ private:
/// the global id of the first target (e.g. a synapse) in this group /// the global id of the first target (e.g. a synapse) in this group
index_type first_target_gid_; index_type first_target_gid_;
/// the global id of the first probe in this group /// the global id of the first probe in this group
index_type first_probe_gid_; index_type first_probe_gid_;
......
...@@ -24,10 +24,10 @@ private: ...@@ -24,10 +24,10 @@ private:
}; };
template <typename charT,typename traitsT=std::char_traits<charT> > template <typename charT, typename traitsT = std::char_traits<charT> >
struct basic_null_streambuf: std::basic_streambuf<charT,traitsT> { class basic_null_streambuf: public std::basic_streambuf<charT, traitsT> {
private: private:
typedef typename std::basic_streambuf<charT,traitsT> streambuf_type; typedef typename std::basic_streambuf<charT, traitsT> streambuf_type;
public: public:
typedef typename streambuf_type::char_type char_type; typedef typename streambuf_type::char_type char_type;
...@@ -39,7 +39,7 @@ public: ...@@ -39,7 +39,7 @@ public:
virtual ~basic_null_streambuf() {} virtual ~basic_null_streambuf() {}
protected: protected:
std::streamsize xsputn(const char_type *s,std::streamsize count) override { std::streamsize xsputn(const char_type* s, std::streamsize count) override {
return count; return count;
} }
...@@ -48,28 +48,29 @@ protected: ...@@ -48,28 +48,29 @@ protected:
} }
}; };
struct mask_stream { class mask_stream {
explicit mask_stream(bool mask_): mask(mask_) {} public:
explicit mask_stream(bool mask): mask_(mask) {}
operator bool() const { return mask; } operator bool() const { return mask_; }
template <typename charT,typename traitsT> template <typename charT, typename traitsT>
friend std::basic_ostream<charT,traitsT> & friend std::basic_ostream<charT, traitsT>&
operator<<(std::basic_ostream<charT,traitsT> &O,const mask_stream &F) { operator<<(std::basic_ostream<charT, traitsT>& O, const mask_stream& F) {
int xindex=get_xindex(); int xindex = get_xindex();
std::basic_streambuf<charT,traitsT> *saved_streambuf= std::basic_streambuf<charT, traitsT>* saved_streambuf =
static_cast<std::basic_streambuf<charT,traitsT> *>(O.pword(xindex)); static_cast<std::basic_streambuf<charT, traitsT>*>(O.pword(xindex));
if (F.mask && saved_streambuf) { if (F.mask_ && saved_streambuf) {
// re-enable by restoring saved streambuf // re-enable by restoring saved streambuf
O.pword(xindex)=0; O.pword(xindex) = 0;
O.rdbuf(saved_streambuf); O.rdbuf(saved_streambuf);
} }
else if (!F.mask && !saved_streambuf) { else if (!F.mask_ && !saved_streambuf) {
// disable stream but save old streambuf // disable stream but save old streambuf
O.pword(xindex)=O.rdbuf(); O.pword(xindex) = O.rdbuf();
O.rdbuf(get_null_streambuf<charT,traitsT>()); O.rdbuf(get_null_streambuf<charT, traitsT>());
} }
return O; return O;
...@@ -78,18 +79,18 @@ struct mask_stream { ...@@ -78,18 +79,18 @@ struct mask_stream {
private: private:
// our key for retrieve saved streambufs. // our key for retrieve saved streambufs.
static int get_xindex() { static int get_xindex() {
static int xindex=std::ios_base::xalloc(); static int xindex = std::ios_base::xalloc();
return xindex; return xindex;
} }
template <typename charT,typename traitsT> template <typename charT, typename traitsT>
static std::basic_streambuf<charT,traitsT> *get_null_streambuf() { static std::basic_streambuf<charT, traitsT>* get_null_streambuf() {
static basic_null_streambuf<charT,traitsT> the_null_streambuf; static basic_null_streambuf<charT, traitsT> the_null_streambuf;
return &the_null_streambuf; return &the_null_streambuf;
} }
// true => do not filter // true => do not filter
bool mask; bool mask_;
}; };
} // namespace util } // namespace util
......
...@@ -17,6 +17,7 @@ set(TEST_SOURCES ...@@ -17,6 +17,7 @@ set(TEST_SOURCES
test_fvm.cpp test_fvm.cpp
test_cell_group.cpp test_cell_group.cpp
test_lexcmp.cpp test_lexcmp.cpp
test_mask_stream.cpp
test_matrix.cpp test_matrix.cpp
test_mechanisms.cpp test_mechanisms.cpp
test_optional.cpp test_optional.cpp
......
#include "gtest.h"
#include <sstream>
#include <util/ioutil.hpp>
using namespace nest::mc::util;
TEST(mask_stream,nomask) {
// expect mask_stream(true) on a new stream not to change rdbuf.
std::ostringstream s;
auto sbuf = s.rdbuf();
s << mask_stream(true);
EXPECT_EQ(sbuf, s.rdbuf());
}
TEST(mask_stream,mask) {
// masked stream should produce no ouptut
std::ostringstream s;
s << "one";
s << mask_stream(false);
s << "two";
EXPECT_EQ(s.str(), "one");
s << mask_stream(true);
s << "three";
EXPECT_EQ(s.str(), "onethree");
}
TEST(mask_stream,mask_multi) {
// mark_stream(false) should be idempotent
std::ostringstream s;
auto sbuf1 = s.rdbuf();
s << "foo";
s << mask_stream(false);
auto sbuf2 = s.rdbuf();
s << "bar";
s << mask_stream(false);
auto sbuf3 = s.rdbuf();
EXPECT_EQ(sbuf2, sbuf3);
s << "baz";
s << mask_stream(true);
auto sbuf4 = s.rdbuf();
EXPECT_EQ(sbuf1, sbuf4);
s << "xyzzy";
EXPECT_EQ(s.str(), "fooxyzzy");
}
TEST(mask_stream,fmt) {
// expect formatting to be preserved across masks.
std::ostringstream s;
s.precision(1);
s << mask_stream(false);
EXPECT_EQ(s.precision(), 1);
s.precision(2);
s << mask_stream(true);
EXPECT_EQ(s.precision(), 2);
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment