dump_test.cpp
Go to the documentation of this file.
1 #include <limits>
2 #include <stan/io/dump.hpp>
3 #include <gtest/gtest.h>
4 #include <boost/math/special_functions/fpclassify.hpp>
5 
6 
8  const std::vector<double>& vals) {
9  std::vector<double> vals2 = reader.double_values();
10  EXPECT_EQ(vals.size(),vals2.size());
11  for (size_t i = 0; i < vals.size(); ++i) {
12  if (boost::math::isnan(vals[i]))
13  EXPECT_TRUE(boost::math::isnan(vals2[i]));
14  else
15  EXPECT_FLOAT_EQ(vals[i],vals2[i]);
16  }
17 }
19  const std::vector<int>& vals) {
20  std::vector<int> vals2 = reader.int_values();
21  EXPECT_EQ(vals.size(),vals2.size());
22  for (size_t i = 0; i < vals.size(); ++i)
23  EXPECT_EQ(vals[i],vals2[i]);
24 }
25 template <typename T>
27  const std::string& name,
28  const std::vector<T>& vals,
29  const std::vector<size_t>& dims) {
30  bool has_next = reader.next();
31  EXPECT_EQ(true,has_next);
32  EXPECT_EQ(name,reader.name());
33  EXPECT_EQ(dims.size(), reader.dims().size());
34  for (size_t i = 0; i < dims.size(); ++i)
35  EXPECT_EQ(dims[i],reader.dims()[i]);
36  test_list3(reader,vals);
37 }
38 
39 
40 template <typename T>
41 void test_list(const std::string& name,
42  const std::vector<T>& vals,
43  const std::string& s) {
44  std::stringstream in(s);
45  stan::io::dump_reader reader(in);
46  std::vector<size_t> expected_dims;
47  expected_dims.push_back(vals.size());
48  test_list2(reader,name,vals,expected_dims);
49 }
50 
51 
52 template <typename T>
54  std::stringstream in(s);
55  stan::io::dump_reader reader(in);
56  std::vector<T> vals;
57  vals.push_back(val);
58  std::vector<size_t> expected_dims;
59  test_list2(reader,name,vals,expected_dims);
60 }
61 
62 void test_exception(const std::string& input) {
63  try {
64  std::stringstream in(input);
65  stan::io::dump_reader reader(in);
66  bool has_next = reader.next();
67  while (has_next) {
68  has_next = reader.next();
69  }
70  } catch (const std::exception& e) {
71  return;
72  }
73  FAIL(); // didn't throw an exception as expected.
74 }
75 
76 
77 
78 bool hasEnding(std::string const &fullString, std::string const &ending) {
79  if (fullString.length() >= ending.length()) {
80  return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
81  } else {
82  return false;
83  }
84 }
85 
86 void test_exception(const std::string& input,
87  const std::string& exception_text) {
88  try {
89  std::stringstream in(input);
90  stan::io::dump_reader reader(in);
91  std::vector<int> vals = reader.int_values();
92  } catch (const std::exception& e) {
93  EXPECT_TRUE(hasEnding(e.what(), exception_text));
94  return;
95  }
96  FAIL(); // didn't throw an exception as expected.
97 }
98 
99 
100 TEST(ioDump, sciNotationDouble) {
101  test_val("a", 5.0, "a <- 5e0");
102  test_val("a", 0.0, "a <- 0e5");
103 }
104 
105 TEST(io_dump, reader_double) {
106  test_val("a",-5.0,"a <- -5.0");
107  test_val("a",5.0,"a <- 5.0");
108  test_val("a",0.0,"a <- 0.0");
109 }
110 
111 TEST(io_dump, reader_int) {
112  test_val("a",5,"a <- 5");
113  test_val("a",-1,"a <- -1");
114  test_val("a",8,"a <- 8L");
115 }
116 
117 TEST(io_dump, reader_doubles) {
118  std::vector<double> vs;
119  test_list("a",vs,"a <- c()");
120 
121  vs.clear();
122  vs.push_back(5.0);
123  vs.push_back(-6.0);
124  test_list("a",vs,"a <- c(5.0,-6.0)");
125 
126  vs.clear();
127  vs.push_back(0.0001);
128  test_list("xYz",vs,"xYz <- c(.0001)");
129 
130  vs.clear();
131  vs.push_back(-0.0001);
132  test_list("xYz",vs,"xYz <- c(-.0001)");
133 
134  vs.clear();
135  vs.push_back(-5);
136  vs.push_back(-2.12);
137  vs.push_back(3.0);
138  vs.push_back(0.0);
139  test_list("b12",vs,"b12 <- c(-5.0, -2.12, 3.0, 0.0)");
140 }
141 
142 TEST(io_dump, read_zero_ints) {
143  std::vector<int> vs;
144  test_list("a",vs,"a <- integer()");
145  test_list("a",vs,"a <- integer(0)");
146 
147  vs.clear();
148  for (int i = 0; i < 4; i++) vs.push_back(0);
149  test_list("a",vs,"a <- integer(4)");
150  test_list("a",vs,"a <- integer(4 )");
151 }
152 
153 TEST(io_dump, read_zero_doubles) {
154  std::vector<double> vs;
155  test_list("a",vs,"a <- double()");
156  test_list("a",vs,"a <- double(0)");
157 
158  vs.clear();
159  for (int i = 0; i < 4; i++) vs.push_back(0);
160  test_list("a",vs,"a <- double(4)");
161  test_list("a",vs,"a <- double(4 )");
162 }
163 
164 TEST(io_dump, integer_zero_ints_in_structure) {
165  std::vector<int> expected_vals;
166  for (int i = 1; i <= 4; ++i)
167  expected_vals.push_back(0);
168  std::vector<size_t> expected_dims;
169  expected_dims.push_back(2U);
170  expected_dims.push_back(2U);
171 
172  std::string txt = "foo <- structure(integer(4), .Dim = c(2, 2))";
173  std::stringstream in(txt);
174  stan::io::dump_reader reader(in);
175  test_list2(reader,"foo",expected_vals,expected_dims);
176 
177  std::string txt2 = "foo <- structure(integer(0), .Dim = c(2, 2, 0))";
178  std::vector<size_t> expected_dims2(expected_dims);
179  std::vector<int> expected_vals2;
180  expected_dims2.push_back(0U);
181  std::stringstream in2(txt2);
182  stan::io::dump_reader reader2(in2);
183  test_list2(reader2,"foo",expected_vals2,expected_dims2);
184 }
185 
186 TEST(io_dump, integer_zero_doubles_in_structure) {
187  std::vector<double> expected_vals;
188  for (int i = 1; i <= 4; ++i)
189  expected_vals.push_back(0);
190  std::vector<size_t> expected_dims;
191  expected_dims.push_back(2U);
192  expected_dims.push_back(2U);
193 
194  std::string txt = "foo <- structure(double(4), .Dim = c(2, 2))";
195  std::stringstream in(txt);
196  stan::io::dump_reader reader(in);
197  test_list2(reader,"foo",expected_vals,expected_dims);
198 
199  std::string txt2 = "foo <- structure(double(0), .Dim = c(2, 2, 0))";
200  std::vector<size_t> expected_dims2(expected_dims);
201  std::vector<int> expected_vals2;
202  expected_dims2.push_back(0U);
203  std::stringstream in2(txt2);
204  stan::io::dump_reader reader2(in2);
205  test_list2(reader2,"foo",expected_vals2,expected_dims2);
206 }
207 
208 
209 TEST(io_dump, reader_ints) {
210  std::vector<int> vs;
211  test_list("a",vs,"a <- c()");
212 
213  vs.clear();
214  vs.push_back(5);
215  vs.push_back(-6);
216  test_list("a",vs,"a <- c(5,-6)");
217 
218  vs.clear();
219  vs.push_back(0);
220  test_list("xYz",vs,"xYz <- c(0)");
221 
222  vs.clear();
223  vs.push_back(-5);
224  vs.push_back(-2);
225  vs.push_back(3);
226  vs.push_back(0);
227  test_list("b12",vs,"b12 <- c(-5, -2L, 3, 0l)");
228 
229  vs.clear();
230  vs.push_back(1);
231  vs.push_back(2);
232  vs.push_back(3);
233  vs.push_back(4);
234  vs.push_back(5);
235  test_list("z98",vs,"z98 <- 1:5");
236 
237  vs.clear();
238  vs.push_back(9);
239  vs.push_back(8);
240  test_list("iroc",vs,"iroc <- 9:8");
241 }
242 
243 
244 TEST(io_dump, reader_vec_data) {
245  std::vector<int> expected_vals;
246  for (int i = 1; i <= 6; ++i)
247  expected_vals.push_back(i);
248  std::vector<size_t> expected_dims;
249  expected_dims.push_back(2U);
250  expected_dims.push_back(3U);
251 
252  std::string txt = "foo <- structure(1:6, .Dim = c(2,3))";
253  std::stringstream in(txt);
254  stan::io::dump_reader reader(in);
255  test_list2(reader,"foo",expected_vals,expected_dims);
256 }
257 TEST(io_dump, reader_vec_data_backward) {
258  std::vector<int> expected_vals;
259  for (int i = 20; i >= 1; --i)
260  expected_vals.push_back(i);
261  std::vector<size_t> expected_dims;
262  expected_dims.push_back(5U);
263  expected_dims.push_back(4U);
264 
265  std::string txt = "foo <- structure(20:1, .Dim = c(5,4))";
266  std::stringstream in(txt);
267  stan::io::dump_reader reader(in);
268  test_list2(reader,"foo",expected_vals,expected_dims);
269 }
270 
271 
272 TEST(io_dump, reader_vec_data_long_suffix) {
273  std::vector<int> expected_vals;
274  for (int i = 10; i >= -9; --i)
275  expected_vals.push_back(i);
276  std::vector<size_t> expected_dims;
277  expected_dims.push_back(20U);
278  std::string txt = "a <-\nc(10L, 9L, 8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 0L, -1L, -2L, -3L, \n-4L, -5L, -6L, -7L, -8L, -9L)";
279  std::stringstream in(txt);
280  stan::io::dump_reader reader(in);
281  test_list2(reader,"a",expected_vals,expected_dims);
282 }
283 TEST(io_dump, reader_nan_inf) {
284  std::string txt = "a <- c(-1.0, Inf, -Inf, 0, Infinity, 129, NaN, -4)";
285  std::vector<double> expected_vals;
286  expected_vals.push_back(-1.0);
287  expected_vals.push_back(std::numeric_limits<double>::infinity());
288  expected_vals.push_back(-std::numeric_limits<double>::infinity());
289  expected_vals.push_back(0.0);
290  expected_vals.push_back(std::numeric_limits<double>::infinity());
291  expected_vals.push_back(129.0);
292  expected_vals.push_back(std::numeric_limits<double>::quiet_NaN());
293  expected_vals.push_back(-4);
294 
295  std::vector<size_t> expected_dims;
296  expected_dims.push_back(expected_vals.size());
297  std::stringstream in(txt);
298  stan::io::dump_reader reader(in);
299  test_list2(reader,"a",expected_vals,expected_dims);
300 }
301 
302 TEST(io_dump, reader_vec_double) {
303  std::vector<double> expected_vals;
304  expected_vals.push_back(1.0);
305  expected_vals.push_back(4.0);
306  expected_vals.push_back(2.0);
307  expected_vals.push_back(5.0);
308  expected_vals.push_back(3.0);
309  expected_vals.push_back(6.0);
310  std::vector<size_t> expected_dims;
311  expected_dims.push_back(2U);
312  expected_dims.push_back(3U);
313  std::string txt = "foo <- structure(c(1.0,4.0,2.0,5.0,3.0,6.0), .Dim = c(2,3))";
314  std::stringstream in(txt);
315  stan::io::dump_reader reader(in);
316  test_list2(reader,"foo",expected_vals,expected_dims);
317 }
318 TEST(io_dump, reader_vec_double_dots) {
319  std::vector<double> expected_vals;
320  expected_vals.push_back(1.0);
321  expected_vals.push_back(4.0);
322  expected_vals.push_back(2.0);
323  expected_vals.push_back(5.0);
324  expected_vals.push_back(3.0);
325  expected_vals.push_back(6.0);
326  std::vector<size_t> expected_dims;
327  expected_dims.push_back(2U);
328  expected_dims.push_back(3U);
329  std::string txt = "foo <- structure(c(1.0,4.0,2.0,5.0,3.0,6.0), .Dim = 2:3))";
330  std::stringstream in(txt);
331  stan::io::dump_reader reader(in);
332  test_list2(reader,"foo",expected_vals,expected_dims);
333 }
334 TEST(io_dump, reader_vec_double_dots_rev) {
335  std::vector<double> expected_vals;
336  expected_vals.push_back(1.0);
337  expected_vals.push_back(4.0);
338  expected_vals.push_back(2.0);
339  expected_vals.push_back(5.0);
340  expected_vals.push_back(3.0);
341  expected_vals.push_back(6.0);
342  std::vector<size_t> expected_dims;
343  expected_dims.push_back(3U);
344  expected_dims.push_back(2U);
345  std::string txt = "foo <- structure(c(1.0,4.0,2.0,5.0,3.0,6.0), .Dim = 3:2))";
346  std::stringstream in(txt);
347  stan::io::dump_reader reader(in);
348  test_list2(reader,"foo",expected_vals,expected_dims);
349 }
350 
351 
352 
353 TEST(io_dump, reader_vec_int) {
354  std::vector<int> expected_vals;
355  expected_vals.push_back(1);
356  expected_vals.push_back(4);
357  expected_vals.push_back(2);
358  expected_vals.push_back(5);
359  expected_vals.push_back(3);
360  expected_vals.push_back(6);
361  std::vector<size_t> expected_dims;
362  expected_dims.push_back(2U);
363  expected_dims.push_back(3U);
364  std::string txt = "foo <- structure(c(1,4,2,5,3,6), .Dim = c(2,3))";
365  std::stringstream in(txt);
366  stan::io::dump_reader reader(in);
367  test_list2(reader,"foo",expected_vals,expected_dims);
368 }
369 
370 TEST(io_dump, reader_sequence) {
371  std::string txt = "foo <- 1\nbar <- 2";
372  std::stringstream in(txt);
373  stan::io::dump_reader reader(in);
374  EXPECT_TRUE(reader.next());
375  EXPECT_EQ("foo",reader.name());
376  EXPECT_TRUE(reader.next());
377  EXPECT_EQ("bar",reader.name());
378  EXPECT_FALSE(reader.next());
379 }
380 
381 TEST(io_dump,two_lines) {
382  std::string txt = "foo <- 3\nbar <- 4";
383  std::stringstream in(txt);
384  stan::io::dump dump(in);
385  EXPECT_TRUE(dump.contains_i("foo"));
386  EXPECT_TRUE(dump.contains_i("bar"));
387 
388  std::string txt2 = "foo <- 3\nloo <- 4";
389  std::stringstream in2(txt2);
390  stan::io::dump dump2(in2);
391  EXPECT_TRUE(dump2.contains_i("foo"));
392  EXPECT_FALSE(dump2.contains_i("oo"));
393  EXPECT_TRUE(dump2.contains_i("loo"));
394 }
395 
396 TEST(io_dump,dump) {
397  std::string txt = "foo <- c(1,2)\nbar<-1.0\n\"bing\"<-\nstructure(c(1.0,4.0,2.0,5.0,3.0,6.0), .Dim = c(2,3))\nqux <- 2.0\nquux<-structure(c(1.0,2.0,3.0,4.0), .Dim = c(2L, 2L))";
398  std::stringstream in(txt);
399  stan::io::dump dump(in);
400  EXPECT_TRUE(dump.contains_i("foo"));
401  EXPECT_TRUE(dump.contains_r("foo"));
402  EXPECT_TRUE(dump.contains_r("bar"));
403  EXPECT_TRUE(dump.contains_r("qux"));
404  EXPECT_TRUE(dump.contains_r("quux"));
405  EXPECT_FALSE(dump.contains_r("baz"));
406  EXPECT_FALSE(dump.contains_i("bingz"));
407 
408  EXPECT_EQ(2U,dump.vals_i("foo").size());
409  EXPECT_EQ(1U,dump.vals_r("bar").size());
410  EXPECT_EQ(1,dump.vals_i("foo")[0]);
411  EXPECT_EQ(2,dump.vals_i("foo")[1]);
412  EXPECT_FLOAT_EQ(1.0,dump.vals_r("foo")[0]);
413  EXPECT_FLOAT_EQ(2.0,dump.vals_r("foo")[1]);
414  EXPECT_FLOAT_EQ(1.0,dump.vals_r("bar")[0]);
415  EXPECT_EQ(6U,dump.vals_r("bing").size());
416  EXPECT_FLOAT_EQ(2.0,dump.vals_r("bing")[2]);
417  EXPECT_EQ(1U,dump.vals_r("qux").size());
418  EXPECT_EQ(4U,dump.vals_r("quux").size());
419  EXPECT_FLOAT_EQ(4.0,dump.vals_r("quux")[3]);
420 
421  EXPECT_EQ(2U, dump.dims_r("bing").size());
422  EXPECT_EQ(2U, dump.dims_r("bing")[0]);
423  EXPECT_EQ(3U, dump.dims_r("bing")[1]);
424 
425  EXPECT_EQ(2U, dump.dims_r("quux").size());
426  EXPECT_EQ(2U, dump.dims_r("quux")[0]);
427  EXPECT_EQ(2U, dump.dims_r("quux")[1]);
428 
429  EXPECT_TRUE(dump.remove("bing"));
430  EXPECT_FALSE(dump.remove("bing"));
431 }
432 
433 TEST(io_dump,dump_contains_i) {
434  std::string txt = "foo <- c(1,2)";
435  std::stringstream in(txt);
436  stan::io::dump dump(in);
437  EXPECT_TRUE(dump.contains_i("foo"));
438  EXPECT_TRUE(dump.contains_r("foo"));
439 
440  EXPECT_EQ(2U,dump.vals_i("foo").size());
441 
442  EXPECT_EQ(1,dump.vals_i("foo")[0]);
443  EXPECT_EQ(2,dump.vals_i("foo")[1]);
444  EXPECT_FLOAT_EQ(1.0,dump.vals_r("foo")[0]);
445  EXPECT_FLOAT_EQ(2.0,dump.vals_r("foo")[1]);
446 
447  EXPECT_TRUE(dump.remove("foo"));
448  EXPECT_FALSE(dump.remove("foo"));
449 
450  EXPECT_FALSE(dump.contains_i("foo"));
451  EXPECT_FALSE(dump.contains_r("foo"));
452 }
453 
454 TEST(io_dump, dump_safety) {
455  std::string txt = "foo <- c(1,2)\nbar<-1.0";
456  //std::string txt = "bar<-1.0\nfoo <- c(1,2)\n";
457  std::stringstream in(txt);
458  stan::io::dump dump(in);
459 
460  EXPECT_TRUE(dump.contains_i("foo"));
461  EXPECT_TRUE(dump.contains_r("foo"));
462 
463  EXPECT_FALSE(dump.contains_i("bar"));
464  EXPECT_TRUE(dump.contains_r("bar"));
465 
466  EXPECT_FALSE(dump.contains_i("bing"));
467  EXPECT_FALSE(dump.contains_r("bing"));
468 
469 
470  EXPECT_EQ(2U,dump.vals_i("foo").size());
471  EXPECT_EQ(1U,dump.dims_i("foo").size());
472  EXPECT_EQ(2U,dump.dims_i("foo")[0]);
473  EXPECT_EQ(1,dump.vals_i("foo")[0]);
474  EXPECT_EQ(2,dump.vals_i("foo")[1]);
475 
476  EXPECT_EQ(2U,dump.vals_r("foo").size());
477  EXPECT_EQ(1U,dump.dims_r("foo").size());
478  EXPECT_EQ(2U,dump.dims_r("foo")[0]);
479  EXPECT_FLOAT_EQ(1.0,dump.vals_r("foo")[0]);
480  EXPECT_FLOAT_EQ(2.0,dump.vals_r("foo")[1]);
481 
482  EXPECT_EQ(1U,dump.vals_r("bar").size());
483  EXPECT_EQ(0U,dump.dims_r("bar").size());
484  EXPECT_FLOAT_EQ(1.0,dump.vals_r("bar")[0]);
485 
486  EXPECT_EQ(0U,dump.vals_i("bar").size());
487  EXPECT_EQ(0U,dump.dims_i("bar").size());
488 
489  EXPECT_EQ(0U,dump.vals_r("bing").size());
490  EXPECT_FALSE(dump.contains_r("bing"));
491  EXPECT_EQ(0U,dump.vals_i("bing").size());
492  EXPECT_FALSE(dump.contains_i("bing"));
493 
494  EXPECT_EQ(0U,dump.dims_r("bing").size());
495  EXPECT_FALSE(dump.contains_r("bing"));
496  EXPECT_EQ(0U,dump.dims_i("bing").size());
497  EXPECT_FALSE(dump.contains_i("bing"));
498 }
499 
500 TEST(io_dump, dump_abs_ref) {
501  std::string txt = "\"N\" <-\n5\n\"y\" <-\n c(1.0, 2.0, 1.2, -0.2, 2.7)";
502  std::stringstream in(txt);
503  stan::io::dump dump(in);
504  EXPECT_TRUE(dump.contains_i("N"));
506  EXPECT_TRUE(context.contains_i("N"));
507 }
508 
509 // thanks to ksvanhorn for pointing out this test case
510 // which failed in Stan 1.3
511 TEST(io_dump, it_sign_ksvanhorn) {
512  using std::vector;
513  std::string txt
514  = "N <- 5\ny <- c(2, 1, 1, 2, -3.4)\nsigma <- 3\n";
515  vector<size_t> dims;
516  std::stringstream in(txt);
517  stan::io::dump dump(in);
518  EXPECT_TRUE(dump.contains_i("N"));
519  EXPECT_TRUE(dump.contains_r("N"));
520  EXPECT_TRUE(dump.contains_r("y"));
521  EXPECT_TRUE(dump.contains_i("sigma"));
522  EXPECT_TRUE(dump.contains_r("sigma"));
523 
524  vector<int> N_values = dump.vals_i("N");
525  EXPECT_EQ(1U,N_values.size());
526  EXPECT_EQ(5,N_values[0]);
527 
528  vector<double> y_values = dump.vals_r("y");
529  EXPECT_EQ(5U,y_values.size());
530  EXPECT_FLOAT_EQ(2,y_values[0]);
531  EXPECT_FLOAT_EQ(1,y_values[1]);
532  EXPECT_FLOAT_EQ(1,y_values[2]);
533  EXPECT_FLOAT_EQ(2,y_values[3]);
534  EXPECT_FLOAT_EQ(-3.4,y_values[4]);
535 
536  vector<double> sigma_values = dump.vals_r("sigma");
537  EXPECT_EQ(1U,sigma_values.size());
538  EXPECT_FLOAT_EQ(3,sigma_values[0]);
539 
540 }
541 
542 /* tests for numbers on boundary and outside of range
543  * use string representations for values above/below limits
544  */
545 
546 TEST(io_dump, reader_max_int) {
547  int imax = INT_MAX - 1;
548  int imin = INT_MIN + 1;
549 
550  std::stringstream sa;
551  sa << "a <- " << imax ;
552  test_val("a",imax,sa.str());
553 
554  std::stringstream sb;
555  sb << "b <- " << imax << "L";
556  test_val("b",imax,sb.str());
557 
558  std::stringstream sc;
559  sc << "c <- " << imin ;
560  test_val("c",imin,sc.str());
561 
562  std::stringstream sd;
563  sd << "d <- " << imin << "L";
564  test_val("d",imin,sd.str());
565 
566 }
567 
568 TEST(io_dump, reader_max_ints) {
569  int imax = INT_MAX - 1;
570  int imin = INT_MIN + 1;
571  std::vector<int> vs;
572  vs.push_back(imax);
573  vs.push_back(imin);
574  vs.push_back(imax);
575  vs.push_back(imin);
576  std::stringstream se;
577  se << "e <- c(" << imax << "," << imin << "," << imax << "L," << imin << "L)";
578  test_list("e",vs,se.str());
579 }
580 
581 TEST(ioDump, zeroLengthArray) {
582  std::vector<int> expected_vals;
583  std::vector<size_t> expected_dims;
584  expected_dims.push_back(0);
585 
586  std::string txt = "y <- c()\n";
587  std::stringstream in(txt);
588  stan::io::dump_reader reader(in);
589 
590  test_list2(reader,"y",expected_vals,expected_dims);
591 }
592 TEST(ioDump, zeroLengthArrayDump) {
593  std::string txt = "M <- 0\ny <- c()\nN <- 0";
594  std::stringstream in(txt);
595  stan::io::dump d(in);
596  EXPECT_TRUE(d.contains_i("N"));
597  EXPECT_TRUE(d.contains_i("y"));
598 }
599 
600 
601 
602 TEST(io_dump, reader_vec_data_max_dims) {
603  std::vector<int> expected_vals;
604  for (int i = 1; i <= 65535; ++i)
605  expected_vals.push_back(i);
606  std::vector<size_t> expected_dims;
607  expected_dims.push_back(65535U);
608 
609  std::string txt = "foo <- structure(1:65535, .Dim = c(65535))";
610  std::stringstream in(txt);
611  stan::io::dump_reader reader(in);
612  test_list2(reader,"foo",expected_vals,expected_dims);
613 }
614 
615 
616 TEST(io_dump, reader_big_doubles) {
617  double dmax = std::numeric_limits<double>::max();
618 
619  // TODO(carpenter): in C++11, min() should change to lowest()
620  // test as written in Stan 2.6 fails on Intel's icpc compiler
621  // min = std::numeric_limits<double>::min();
622  double dmin = -dmax;
623  std::stringstream sa;
624  sa << "a <- " << dmax ;
625  test_val("a",dmax,sa.str());
626  std::stringstream sb;
627  sb << "b <- " << dmin ;
628  test_val("b",dmin,sb.str());
629 
630 }
631 
632 TEST(io_dump, very_large_pos_int) {
633  test_exception("k <- 999918446744073709551616");
634 }
635 
636 TEST(io_dump, very_large_neg_int) {
637  test_exception("k <- -999918446744073709551616");
638 }
639 
640 TEST(io_dump, very_large_pos_intL) {
641  test_exception("k <- 999918446744073709551616L");
642 }
643 
644 TEST(io_dump, very_large_neg_intL) {
645  test_exception("k <- -999918446744073709551616L");
646 }
647 
648 TEST(io_dump, int_too_large_v) {
649  test_exception("k <- c(999918446744073709551616L)");
650 }
651 
652 TEST(io_dump, int_neg_too_large_v) {
653  test_exception("k <- c(-999918446744073709551616L)");
654 }
655 
656 TEST(io_dump, dim_too_large_v) {
657  test_exception("foo <- structure(1:2, .Dim = c(184467440737095516159))");
658 }
659 
660 TEST(io_dump, double_too_large) {
661  test_exception("a <- 2.797693134862316991999E+309912");
662 }
663 
664 TEST(io_dump, double_too_small) {
665  test_exception("a <- 4.940656458412465E-324994079409");
666 }
667 
668 
669 
670 /* syntax errors */
671 
672 TEST(io_dump, bad_syntax_seq) {
673  test_exception("a <- c(1,2,3, ");
674 }
675 
676 TEST(io_dump, bad_syntax_seq2) {
677  test_exception("a <- c(1:2 ");
678 }
679 
680 TEST(io_dump, bad_syntax_struct) {
681  test_exception("a <- structure(1:2, .Dim = c(2,3) ");
682 }
683 
684 TEST(io_dump, bad_syntax_zero_array) {
685  test_exception("a <- integer(-3)");
686  test_exception("a <- integer(-3.2)");
687  test_exception("a <- double(-3)");
688  test_exception("a <- double(-3.2)");
689  test_exception("a <- structure(integer(-3), .Dim = c(2,0))");
690  test_exception("a <- structure(integer(-3], .Dim = c(2,0))");
691  test_exception("a <- structure(integer(3.2), .Dim = c(2,0))");
692  test_exception("a <- structure(double(-3), .Dim = c(2,0))");
693  test_exception("a <- structure(double(-3.2), .Dim = c(2,0))");
694 }
695 
696 TEST(io_dump, too_large_zero_array) {
697  test_exception("a <- integer(999918446744073709551616L)");
698  test_exception("a <- double(999918446744073709551616L)");
699  test_exception("a <- structure(integer(999918446744073709551616L), .Dim = c(2,3))");
700  test_exception("a <- structure(double(999918446744073709551616L), .Dim = c(2,3))");
701 }
const XML_Char * name
Definition: expat.h:151
void test_exception(const std::string &input)
Definition: dump_test.cpp:62
void test_val(std::string name, T val, std::string s)
Definition: dump_test.cpp:53
::xsd::cxx::tree::exception< char > exception
Definition: Database.h:225
bool contains_r(const std::string &name) const
Definition: dump.hpp:657
bool hasEnding(std::string const &fullString, std::string const &ending)
Definition: dump_test.cpp:78
std::vector< double > double_values()
Definition: dump.hpp:544
TEST(ioDump, sciNotationDouble)
Definition: dump_test.cpp:100
bool isnan(const stan::math::var &v)
Definition: boost_isnan.hpp:20
double sd(Eigen::VectorXd x)
const XML_Char * s
Definition: expat.h:262
bool remove(const std::string &name)
Definition: dump.hpp:777
bool contains_i(const std::string &name) const
Definition: dump.hpp:669
Float_t d
Definition: plot.C:236
std::vector< size_t > dims_r(const std::string &name) const
Definition: dump.hpp:701
void test_list3(stan::io::dump_reader &reader, const std::vector< double > &vals)
Definition: dump_test.cpp:7
std::vector< int > int_values()
Definition: dump.hpp:533
const XML_Char * context
Definition: expat.h:434
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
ifstream in
Definition: comparison.C:7
std::vector< double > vals_r(const std::string &name) const
Definition: dump.hpp:680
std::string name()
Definition: dump.hpp:501
std::vector< size_t > dims()
Definition: dump.hpp:511
std::vector< int > vals_i(const std::string &name) const
Definition: dump.hpp:717
double T
Definition: Xdiff_gwt.C:5
Float_t e
Definition: plot.C:35
T max(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:68
void test_list(const std::string &name, const std::vector< T > &vals, const std::string &s)
Definition: dump_test.cpp:41
#define FAIL
std::vector< size_t > dims_i(const std::string &name) const
Definition: dump.hpp:731
void test_list2(stan::io::dump_reader &reader, const std::string &name, const std::vector< T > &vals, const std::vector< size_t > &dims)
Definition: dump_test.cpp:26