Improve sgstream_test.cxx

- Create "testfile" in a temporary directory (and make sure the temp dir
  and the file it contains are both removed when the test program exits).

- Use test macros such as SG_CHECK_EQUAL() instead of by-hand checks and
  "return EXIT_FAILURE" statements.

- Use sg_ofstream instead of plain std::ostream. This simplifies things
  a tiny bit, because one SGPath instance is enough to create both the
  sg_ofstream instance and the sg_gzifstream instance used afterwards to
  reread the created file.

- Don't call (sg_)ofstream::close() at the end of the block the instance
  is declared in: this is entirely unnecessary, since sg_ofstream
  derives from std::ofstream, which is automatically close()d when
  destroyed (RAII behavior).
This commit is contained in:
Florent Rougon
2017-01-26 16:46:43 +01:00
parent edec5bbc01
commit d9cc3738b9

View File

@@ -1,52 +1,53 @@
#include <iostream>
#include <fstream>
#include <cstdlib> // for EXIT_SUCCESS
#include <cstdlib> // for EXIT_FAILURE
using std::ofstream;
using std::string;
using std::cout;
using std::endl;
#include <simgear/misc/test_macros.hxx>
#include <simgear/misc/sgstream.hxx>
#include <simgear/misc/sg_path.hxx>
#include <simgear/misc/sg_dir.hxx>
int main()
{
const char* fileName = "testfile";
const string fileName = "testfile";
simgear::Dir tmpDir = simgear::Dir::tempDir("FlightGear");
tmpDir.setRemoveOnDestroy();
SGPath p(tmpDir.path() / fileName);
{
ofstream f;
f.open(fileName, std::ios::binary | std::ios::trunc | std::ios::out);
sg_ofstream f(p, std::ios::binary | std::ios::trunc | std::ios::out);
f.write("first line ends with line-feed\n"
"second line ends with just a cr\r"
"third line ends with both\r\n"
"fourth line as well\r\n"
"fifth line is another CR/LF line\r\n"
"end of test\r\n", 158);
f.close();
}
}
SGPath p(fileName);
sg_gzifstream sg(p);
std::string stuff;
sg >> skipeol;
sg >> stuff;
if (stuff != "second") return EXIT_FAILURE;
SG_CHECK_EQUAL(stuff, "second");
cout << "Detection of LF works." << endl;
sg >> skipeol;
sg >> stuff;
if (stuff != "third") return EXIT_FAILURE;
SG_CHECK_EQUAL(stuff, "third");
cout << "Detection of CR works." << endl;
sg >> skipeol;
sg >> stuff;
if (stuff != "fourth") return EXIT_FAILURE;
SG_CHECK_EQUAL(stuff, "fourth");
cout << "Detection of CR/LF works." << endl;
sg >> skipeol;
sg >> skipeol;
sg >> stuff;
if (stuff != "end") return EXIT_FAILURE;
SG_CHECK_EQUAL(stuff, "end");
cout << "Detection of 2 following CR/LF lines works." << endl;
return EXIT_SUCCESS;