libsequence  1.9.5
FastaExplicitIO.cc
1 
2 #include <Sequence/FastaExplicit.hpp>
3 #include <fstream>
4 #include <boost/test/unit_test.hpp>
5 #include <unistd.h>
6 #include <iterator>
7 
8 std::string name("seqname is a seq"),seq("AGCGTAGACAGTAGAGTGAT"),seq_left("AGCGTAGAC"),seq_right("AGTAGAGTGAT");
9 
10 
11 BOOST_AUTO_TEST_CASE( ostream_test )
12 {
13  const char * filename = "fasta_exp_ostream_test_out.fasta";
14  Sequence::Fasta f(name,seq),f2;
15  std::ofstream o(filename);
16  o << f << std::endl;
17  o.close();
18  std::ifstream in(filename);
19  in >> f2 >> std::ws;
20  BOOST_REQUIRE_EQUAL(f,f2);
21  unlink(filename);
22 }
23 
24 BOOST_AUTO_TEST_CASE( ostream_test_3recs )
25 {
26  const char * filename = "fasta_exp_ostream_test_out.fasta";
27  Sequence::Fasta f(name,seq),f2;
28  std::ofstream o(filename);
29  o << f << '\n'
30  << f << '\n'
31  << f << '\n';
32  o.close();
33  std::ifstream in(filename);
34  unsigned count = 0;
35  while(! in.eof() )
36  {
37  in >> f2 >> std::ws;
38  BOOST_REQUIRE_EQUAL(f,f2);
39  ++count;
40  }
41  BOOST_REQUIRE_EQUAL( count, 3 );
42  //in >> f2 >> std::ws;
43 
44  unlink(filename);
45 }
46 
47 //No newline @ end of data
48 BOOST_AUTO_TEST_CASE( ostream_test_no_newline_end_of_file )
49 {
50  const char * filename = "fasta_exp_ostream_test_out.fasta";
51  Sequence::Fasta f(name,seq),f2;
52  std::ofstream o(filename);
53  o << f << '\n'
54  << f << '\n'
55  << f;
56  o.close();
57  std::ifstream in(filename);
58  unsigned count = 0;
59  while(! in.eof() )
60  {
61  in >> f2 >> std::ws;
62  BOOST_REQUIRE_EQUAL(f,f2);
63  ++count;
64  }
65  BOOST_REQUIRE_EQUAL( count, 3 );
66  unlink(filename);
67 }
68 
69 BOOST_AUTO_TEST_CASE( ostream_test_newline_within_seq )
70 {
71  const char * filename = "fasta_exp_ostream_test_out.fasta";
72  Sequence::Fasta f(name,seq),f2;
73  std::ofstream o(filename);
74  for ( unsigned i = 0 ; i < 3 ; ++i )
75  {
76  o << '>' << name << '\n'
77  << seq_left << '\n'
78  << seq_right << '\n';
79  }
80  o.close();
81  std::ifstream in(filename);
82  unsigned count = 0;
83  while(! in.eof() )
84  {
85  in >> f2 >> std::ws;
86  BOOST_REQUIRE_EQUAL(f,f2);
87  ++count;
88  }
89  BOOST_REQUIRE_EQUAL( count, 3 );
90  unlink(filename);
91 }
92 
93 BOOST_AUTO_TEST_CASE( ostream_test_really_bad_input )
94 {
95  const char * filename = "fasta_exp_ostream_test_out.fasta";
96  Sequence::Fasta f(name,seq),f2;
97  std::ofstream o(filename);
98  for ( unsigned i = 0 ; i < 3 ; ++i )
99  {
100  o << '>' << name << '\n'
101  << seq_left << "\n\n\n\n\n" //Bad bad bad
102  << seq_right << '\n';
103  }
104  o.close();
105  std::ifstream in(filename);
106  unsigned count = 0;
107  while(! in.eof() )
108  {
109  in >> f2 >> std::ws;
110  BOOST_REQUIRE_EQUAL(f,f2);
111  ++count;
112  }
113  BOOST_REQUIRE_EQUAL( count, 3 );
114  unlink(filename);
115 }
116 
117 BOOST_AUTO_TEST_CASE( ostream_test_really_bad_input_istream_iterator )
118 {
119  const char * filename = "fasta_exp_ostream_test_out.fasta";
120  Sequence::Fasta f(name,seq),f2;
121  std::ofstream o(filename);
122  for ( unsigned i = 0 ; i < 3 ; ++i )
123  {
124  o << '>' << name << '\n'
125  << seq_left << "\n\n\n\n\n" //Bad bad bad
126  << seq_right << '\n';
127  }
128  o.close();
129  std::ifstream in(filename);
130  unsigned count = 0;
131  std::istream_iterator<Sequence::Fasta> i(in);
132  for( ; i != std::istream_iterator<Sequence::Fasta>() ; ++i )
133  {
134  ++count;
135  }
136  BOOST_REQUIRE_EQUAL( count, 3 );
137  unlink(filename);
138 }
139 
140 BOOST_AUTO_TEST_CASE( exception_test )
141 {
142  const char * filename = "fasta_exp_ostream_test_out.fasta";
143  std::ofstream out(filename);
144  //Write a badly-formatted FASTA record (we forgot the >)
145  out << name << '\n' << seq;
146  out.close();
147 
148  std::ifstream in(filename);
149  Sequence::Fasta f;
150  BOOST_CHECK_THROW( in >> f >> std::ws, Sequence::badFormat );
151  unlink(filename);
152 }
153 
154 BOOST_AUTO_TEST_SUITE_END()
155 //EOF
FASTA sequence stream.
Definition: Fasta.hpp:49