1 /* ----------------------------------------------------------------------
2 LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
3 https://www.lammps.org/ Sandia National Laboratories
4 Steve Plimpton, sjplimp@sandia.gov
5
6 Copyright (2003) Sandia Corporation. Under the terms of Contract
7 DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
8 certain rights in this software. This software is distributed under
9 the GNU General Public License.
10
11 See the README file in the top-level LAMMPS directory.
12 ------------------------------------------------------------------------- */
13 #ifndef LMP_TESTING_UTILS_H
14 #define LMP_TESTING_UTILS_H
15
16 #include <fstream>
17 #include <iostream>
18 #include <string>
19 #include <vector>
20
delete_file(const std::string & filename)21 static void delete_file(const std::string &filename)
22 {
23 remove(filename.c_str());
24 }
25
count_lines(const std::string & filename)26 static size_t count_lines(const std::string &filename)
27 {
28 std::ifstream infile(filename);
29 std::string line;
30 size_t nlines = 0;
31
32 while (std::getline(infile, line))
33 ++nlines;
34
35 return nlines;
36 }
37
equal_lines(const std::string & fileA,const std::string & fileB)38 static bool equal_lines(const std::string &fileA, const std::string &fileB)
39 {
40 std::ifstream afile(fileA);
41 std::ifstream bfile(fileB);
42 std::string lineA, lineB;
43
44 while (std::getline(afile, lineA)) {
45 if (!std::getline(bfile, lineB)) return false;
46 if (lineA != lineB) return false;
47 }
48
49 return true;
50 }
51
read_lines(const std::string & filename)52 static std::vector<std::string> read_lines(const std::string &filename)
53 {
54 std::vector<std::string> lines;
55 std::ifstream infile(filename);
56 std::string line;
57
58 while (std::getline(infile, line))
59 lines.push_back(line);
60
61 return lines;
62 }
63
file_exists(const std::string & filename)64 static bool file_exists(const std::string &filename)
65 {
66 std::ifstream infile(filename);
67 return infile.good();
68 }
69
70 #define ASSERT_FILE_EXISTS(NAME) ASSERT_TRUE(file_exists(NAME))
71 #define ASSERT_FILE_NOT_EXISTS(NAME) ASSERT_FALSE(file_exists(NAME))
72 #define ASSERT_FILE_EQUAL(FILE_A, FILE_B) ASSERT_TRUE(equal_lines(FILE_A, FILE_B))
73
74 #endif
75