1 /* Test operator<<(std::ostream& s, Variable v)
2    and the related machinery.
3    Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
4    Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
5 
6 This file is part of the Parma Polyhedra Library (PPL).
7 
8 The PPL is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 The PPL is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
21 
22 For the most up-to-date information see the Parma Polyhedra Library
23 site: http://bugseng.com/products/ppl/ . */
24 
25 #include "ppl_test.hh"
26 #include "files.hh"
27 #include <string>
28 #include <fstream>
29 
30 using std::string;
31 using std::fstream;
32 using std::ios_base;
33 
34 using namespace Parma_Polyhedra_Library::IO_Operators;
35 
36 namespace {
37 
38 const char* my_file = "writevariable1.dat";
39 
40 void
write_variables()41 write_variables() {
42   fstream f;
43   open(f, my_file, ios_base::out);
44   for (dimension_type id = 0; id <= 100; id += 5)
45     f << Variable(id) << " ";
46   f << endl;
47   close(f);
48 }
49 
50 void
read_variables_and_check(const string & s)51 read_variables_and_check(const string& s) {
52   fstream f;
53   open(f, my_file, ios_base::in);
54   string t;
55   getline(f, t);
56   close(f);
57   if (s != t)
58     exit(1);
59 }
60 
61 void
my_output_function(std::ostream & s,const Variable v)62 my_output_function(std::ostream& s, const Variable v) {
63   s << "x" << v.id();
64 }
65 
66 } // namespace
67 
68 int
main()69 main() TRY {
70   set_handlers();
71 
72   // Default output function: write...
73   write_variables();
74   // ... read back.
75   read_variables_and_check("A F K P U Z E1 J1 O1 T1 Y1 "
76                            "D2 I2 N2 S2 X2 C3 H3 M3 R3 W3 ");
77 
78   // Save the default output function.
79   Variable::output_function_type* p_default_output_function
80     = Variable::get_output_function();
81 
82   // Install an alternate output function.
83   Variable::set_output_function(my_output_function);
84 
85   // Check that the installation worked as expected.
86   if (Variable::get_output_function() != my_output_function)
87     return 1;
88 
89   // Alternate output function: write...
90   write_variables();
91   // ... read back.
92   read_variables_and_check("x0 x5 x10 x15 x20 x25 x30 x35 x40 x45 "
93                            "x50 x55 x60 x65 x70 x75 x80 x85 x90 x95 x100 ");
94 
95   // Restore the default output function.
96   Variable::set_output_function(p_default_output_function);
97 
98   // Check that the restoration worked as expected.
99   if (Variable::get_output_function() != p_default_output_function)
100     return 1;
101 
102   return 0;
103 }
104 CATCH
105