Skip to content

Commit a9d3e31

Browse files
committed
support/test more scalar types
1 parent 3fb9e89 commit a9d3e31

35 files changed

+195
-28
lines changed

Makefile

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ example-data-2d: example-data-2d.o
5353
example-interactive: example-interactive.o
5454
$(CXX) -o $@ $^ $(LDFLAGS)
5555

56+
test-noncopyable: test-noncopyable.o
57+
$(CXX) -o $@ $^ $(LDFLAGS)
58+
59+
test-outputs: test-outputs.o
60+
$(CXX) -o $@ $^ $(LDFLAGS)
61+
5662
test-asserts: test-assert-depth.error.txt test-assert-depth-colmajor.error.txt
5763

5864
%.error.txt: %.cc gnuplot-iostream.h

gnuplot-iostream.h

+19-12
Original file line numberDiff line numberDiff line change
@@ -490,20 +490,27 @@ template<> struct BinarySender<boost::uint32_t> : public FlatBinarySender<boost:
490490
template<> struct BinarySender<boost:: int64_t> : public FlatBinarySender<boost:: int64_t> { };
491491
template<> struct BinarySender<boost::uint64_t> : public FlatBinarySender<boost::uint64_t> { };
492492

493-
// Make int8_t and char print as integers, not as characters.
494-
// Are there any other types that need to be handled in this way? I don't know.
495-
template<> struct TextSender<boost::int8_t> {
496-
static void send(std::ostream &stream, const boost::int8_t &v) { stream << int(v); } };
497-
template<> struct TextSender<char> {
498-
static void send(std::ostream &stream, const char &v) { stream << int(v); } };
493+
// Make char types print as integers, not as characters.
494+
template <typename T>
495+
struct CastIntTextSender {
496+
static void send(std::ostream &stream, const T &v) {
497+
stream << int(v);
498+
}
499+
};
500+
template<> struct TextSender< char> : public CastIntTextSender< char> { };
501+
template<> struct TextSender< signed char> : public CastIntTextSender< signed char> { };
502+
template<> struct TextSender< unsigned char> : public CastIntTextSender< unsigned char> { };
499503

500504
// Make sure that the same not-a-number string is printed on all platforms.
501-
template<> struct TextSender<float> {
502-
static void send(std::ostream &stream, const float &v) {
503-
if(GNUPLOT_ISNAN(v)) { stream << "nan"; } else { stream << v; } } };
504-
template<> struct TextSender<double> {
505-
static void send(std::ostream &stream, const double &v) {
506-
if(GNUPLOT_ISNAN(v)) { stream << "nan"; } else { stream << v; } } };
505+
template <typename T>
506+
struct FloatTextSender {
507+
static void send(std::ostream &stream, const T &v) {
508+
if(GNUPLOT_ISNAN(v)) { stream << "nan"; } else { stream << v; }
509+
}
510+
};
511+
template<> struct TextSender< float> : FloatTextSender< float> { };
512+
template<> struct TextSender< double> : FloatTextSender< double> { };
513+
template<> struct TextSender<long double> : FloatTextSender<long double> { };
507514

508515
/// }}}2
509516

test-outputs.cc

+71-16
Original file line numberDiff line numberDiff line change
@@ -51,47 +51,88 @@ Gnuplot gp;
5151
std::string basedir = "unittest-output";
5252

5353
template <typename T, typename ArrayMode>
54-
void test_given_mode(std::ostream &log_fh, std::string header, const T &arg, ArrayMode) {
54+
void test_given_mode(
55+
std::ostream &log_fh, std::string header, const T &arg,
56+
ArrayMode, boost::mpl::true_
57+
) {
5558
std::string modename = ArrayMode::class_name();
5659
std::string fn_prefix = basedir+"/"+header+"-"+modename;
5760
log_fh << "* " << modename << " -> "
5861
<< gp.binaryFile(arg, fn_prefix+".bin", "record", ArrayMode()) << std::endl;
5962
gp.file(arg, fn_prefix+".txt", ArrayMode());
6063
}
6164

62-
template <typename T>
65+
template <typename T, typename ArrayMode>
66+
void test_given_mode(
67+
std::ostream &log_fh, std::string header, const T &arg,
68+
ArrayMode, boost::mpl::false_
69+
) {
70+
std::string modename = ArrayMode::class_name();
71+
std::string fn_prefix = basedir+"/"+header+"-"+modename;
72+
log_fh << "* " << modename << " (skipped binary) " << std::endl;
73+
gp.file(arg, fn_prefix+".txt", ArrayMode());
74+
}
75+
76+
template <typename T, typename DoBinary>
6377
typename boost::enable_if_c<(ArrayTraits<T>::depth == 1)>::type
6478
runtest_inner(std::ostream &log_fh, std::string header, const T &arg) {
65-
test_given_mode(log_fh, header, arg, Mode1D());
79+
test_given_mode<T>(log_fh, header, arg, Mode1D(), DoBinary());
6680
}
6781

68-
template <typename T>
82+
template <typename T, typename DoBinary>
6983
typename boost::enable_if_c<(ArrayTraits<T>::depth == 2)>::type
7084
runtest_inner(std::ostream &log_fh, std::string header, const T &arg) {
71-
test_given_mode(log_fh, header, arg, Mode2D());
72-
test_given_mode(log_fh, header, arg, Mode1DUnwrap());
85+
test_given_mode<T>(log_fh, header, arg, Mode2D(), DoBinary());
86+
test_given_mode<T>(log_fh, header, arg, Mode1DUnwrap(), DoBinary());
7387
}
7488

75-
template <typename T>
89+
template <typename T, typename DoBinary>
7690
typename boost::enable_if_c<(ArrayTraits<T>::depth >= 3)>::type
7791
runtest_inner(std::ostream &log_fh, std::string header, const T &arg) {
78-
test_given_mode(log_fh, header, arg, Mode2D());
79-
test_given_mode(log_fh, header, arg, Mode2DUnwrap());
92+
test_given_mode<T>(log_fh, header, arg, Mode2D(), DoBinary());
93+
test_given_mode<T>(log_fh, header, arg, Mode2DUnwrap(), DoBinary());
8094
}
8195

82-
template <typename T>
83-
void runtest(std::string header, const T &arg) {
96+
template <typename T, typename DoBinary>
97+
void runtest_maybe_dobin(std::string header, const T &arg) {
8498
std::ofstream log_fh((basedir+"/"+header+"-log.txt").c_str());
8599
log_fh << "--- " << header << " -------------------------------------" << std::endl;
86100
log_fh << "depth=" << ArrayTraits<T>::depth << std::endl;
87101
log_fh << "ModeAutoDecoder=" << ModeAutoDecoder<T>::mode::class_name() << std::endl;
88-
runtest_inner(log_fh, header, arg);
102+
runtest_inner<T, DoBinary>(log_fh, header, arg);
103+
}
104+
105+
template <typename T>
106+
void runtest(std::string header, const T &arg) {
107+
runtest_maybe_dobin<T, boost::mpl::true_>(header, arg);
108+
}
109+
110+
template <typename T>
111+
void runtest_nobin(std::string header, const T &arg) {
112+
runtest_maybe_dobin<T, boost::mpl::false_>(header, arg);
113+
}
114+
115+
template <typename T, typename DoBinary>
116+
void basic_datatype_test_integral(std::string name) {
117+
std::vector<T> v;
118+
for(int i=0; i<4; i++) {
119+
v.push_back(i);
120+
}
121+
runtest_maybe_dobin<std::vector<T>, DoBinary>(name, v);
122+
}
123+
124+
template <typename T, typename DoBinary>
125+
void basic_datatype_test_float(std::string name) {
126+
std::vector<T> v;
127+
for(int i=0; i<4; i++) {
128+
v.push_back(i + T(0.1234));
129+
}
130+
v.push_back(std::numeric_limits<T>::quiet_NaN());
131+
runtest_maybe_dobin<std::vector<T>, DoBinary>(name, v);
89132
}
90133

91134
int main() {
92135
const int NX=3, NY=4, NZ=2;
93-
// FIXME - why doesn't it compile with std::vector<char>?
94-
std::vector<int8_t> vc; // should print as integers
95136
std::vector<double> vd;
96137
std::vector<int> vi;
97138
std::vector<float> vf;
@@ -109,7 +150,6 @@ int main() {
109150

110151
for(int x=0; x<NX; x++) {
111152
vd.push_back(x+7.5);
112-
vc.push_back(x+7);
113153
vi.push_back(x+7);
114154
vf.push_back(x+7.2F);
115155
v_bt.push_back(boost::make_tuple(x+0.123, 100+x, 200+x));
@@ -137,8 +177,23 @@ int main() {
137177
}
138178
}
139179

180+
basic_datatype_test_integral<boost:: int8_t, boost::mpl::true_>("vi8");
181+
basic_datatype_test_integral<boost:: uint8_t, boost::mpl::true_>("vu8");
182+
basic_datatype_test_integral<boost:: int16_t, boost::mpl::true_>("vi16");
183+
basic_datatype_test_integral<boost::uint16_t, boost::mpl::true_>("vu16");
184+
basic_datatype_test_integral<boost:: int32_t, boost::mpl::true_>("vi32");
185+
basic_datatype_test_integral<boost::uint32_t, boost::mpl::true_>("vu32");
186+
187+
// these should all print as integers
188+
basic_datatype_test_integral<char, boost::mpl::false_>("vpc");
189+
basic_datatype_test_integral<signed char, boost::mpl::false_>("vsc");
190+
basic_datatype_test_integral<unsigned char, boost::mpl::false_>("vuc");
191+
192+
basic_datatype_test_float<float, boost::mpl::true_>("vf");
193+
basic_datatype_test_float<double, boost::mpl::true_>("vd");
194+
basic_datatype_test_float<long double, boost::mpl::false_>("vld");
195+
140196
runtest("vd,vi,bi", std::make_pair(vd, std::make_pair(vi, bi)));
141-
runtest("vc", vc);
142197
runtest("vvd", vvd);
143198
runtest("vvd,vvi", std::make_pair(vvd, vvi));
144199
runtest("ai", ai);

unittest-output-good/vd-Mode1D.bin

40 Bytes
Binary file not shown.

unittest-output-good/vd-Mode1D.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
0.1234
2+
1.1234
3+
2.1234
4+
3.1234
5+
nan

unittest-output-good/vd-log.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- vd -------------------------------------
2+
depth=1
3+
ModeAutoDecoder=Mode1D
4+
* Mode1D -> 'unittest-output/vd-Mode1D.bin' binary format='%double' record=(5)

unittest-output-good/vf-Mode1D.bin

20 Bytes
Binary file not shown.

unittest-output-good/vf-Mode1D.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
0.1234
2+
1.1234
3+
2.1234
4+
3.1234
5+
nan

unittest-output-good/vf-log.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- vf -------------------------------------
2+
depth=1
3+
ModeAutoDecoder=Mode1D
4+
* Mode1D -> 'unittest-output/vf-Mode1D.bin' binary format='%float' record=(5)

unittest-output-good/vi16-Mode1D.bin

8 Bytes
Binary file not shown.

unittest-output-good/vi16-Mode1D.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
1
3+
2
4+
3

unittest-output-good/vi16-log.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- vi16 -------------------------------------
2+
depth=1
3+
ModeAutoDecoder=Mode1D
4+
* Mode1D -> 'unittest-output/vi16-Mode1D.bin' binary format='%int16' record=(4)

unittest-output-good/vi32-Mode1D.bin

16 Bytes
Binary file not shown.

unittest-output-good/vi32-Mode1D.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
1
3+
2
4+
3

unittest-output-good/vi32-log.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- vi32 -------------------------------------
2+
depth=1
3+
ModeAutoDecoder=Mode1D
4+
* Mode1D -> 'unittest-output/vi32-Mode1D.bin' binary format='%int32' record=(4)

unittest-output-good/vi8-Mode1D.bin

4 Bytes
Binary file not shown.

unittest-output-good/vi8-Mode1D.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
1
3+
2
4+
3

unittest-output-good/vi8-log.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- vi8 -------------------------------------
2+
depth=1
3+
ModeAutoDecoder=Mode1D
4+
* Mode1D -> 'unittest-output/vi8-Mode1D.bin' binary format='%int8' record=(4)

unittest-output-good/vld-Mode1D.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
0.1234
2+
1.1234
3+
2.1234
4+
3.1234
5+
nan

unittest-output-good/vld-log.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- vld -------------------------------------
2+
depth=1
3+
ModeAutoDecoder=Mode1D
4+
* Mode1D (skipped binary)

unittest-output-good/vpc-Mode1D.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
1
3+
2
4+
3

unittest-output-good/vpc-log.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- vpc -------------------------------------
2+
depth=1
3+
ModeAutoDecoder=Mode1D
4+
* Mode1D (skipped binary)

unittest-output-good/vsc-Mode1D.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
1
3+
2
4+
3

unittest-output-good/vsc-log.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- vsc -------------------------------------
2+
depth=1
3+
ModeAutoDecoder=Mode1D
4+
* Mode1D (skipped binary)

unittest-output-good/vu16-Mode1D.bin

8 Bytes
Binary file not shown.

unittest-output-good/vu16-Mode1D.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
1
3+
2
4+
3

unittest-output-good/vu16-log.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- vu16 -------------------------------------
2+
depth=1
3+
ModeAutoDecoder=Mode1D
4+
* Mode1D -> 'unittest-output/vu16-Mode1D.bin' binary format='%uint16' record=(4)

unittest-output-good/vu32-Mode1D.bin

16 Bytes
Binary file not shown.

unittest-output-good/vu32-Mode1D.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
1
3+
2
4+
3

unittest-output-good/vu32-log.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- vu32 -------------------------------------
2+
depth=1
3+
ModeAutoDecoder=Mode1D
4+
* Mode1D -> 'unittest-output/vu32-Mode1D.bin' binary format='%uint32' record=(4)

unittest-output-good/vu8-Mode1D.bin

4 Bytes
Binary file not shown.

unittest-output-good/vu8-Mode1D.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
1
3+
2
4+
3

unittest-output-good/vu8-log.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- vu8 -------------------------------------
2+
depth=1
3+
ModeAutoDecoder=Mode1D
4+
* Mode1D -> 'unittest-output/vu8-Mode1D.bin' binary format='%uint8' record=(4)

unittest-output-good/vuc-Mode1D.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
0
2+
1
3+
2
4+
3

unittest-output-good/vuc-log.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
--- vuc -------------------------------------
2+
depth=1
3+
ModeAutoDecoder=Mode1D
4+
* Mode1D (skipped binary)

0 commit comments

Comments
 (0)