File: utils.h

package info (click to toggle)
lammps 20250204%2Bdfsg.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 474,368 kB
  • sloc: cpp: 1,060,070; python: 27,785; ansic: 8,956; f90: 7,254; sh: 6,044; perl: 4,171; fortran: 2,442; xml: 1,714; makefile: 1,352; objc: 238; lisp: 188; yacc: 58; csh: 16; awk: 14; tcl: 6; javascript: 2
file content (74 lines) | stat: -rw-r--r-- 2,020 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* ----------------------------------------------------------------------
   LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
   https://www.lammps.org/ Sandia National Laboratories
   LAMMPS Development team: developers@lammps.org

   Copyright (2003) Sandia Corporation.  Under the terms of Contract
   DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
   certain rights in this software.  This software is distributed under
   the GNU General Public License.

   See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifndef LMP_TESTING_UTILS_H
#define LMP_TESTING_UTILS_H

#include <fstream>
#include <iostream>
#include <string>
#include <vector>

static void delete_file(const std::string &filename)
{
    remove(filename.c_str());
}

static size_t count_lines(const std::string &filename)
{
    std::ifstream infile(filename);
    std::string line;
    size_t nlines = 0;

    while (std::getline(infile, line))
        ++nlines;

    return nlines;
}

static bool equal_lines(const std::string &fileA, const std::string &fileB)
{
    std::ifstream afile(fileA);
    std::ifstream bfile(fileB);
    std::string lineA, lineB;

    while (std::getline(afile, lineA)) {
        if (!std::getline(bfile, lineB)) return false;
        if (lineA != lineB) return false;
    }

    return true;
}

static std::vector<std::string> read_lines(const std::string &filename)
{
    std::vector<std::string> lines;
    std::ifstream infile(filename);
    std::string line;

    while (std::getline(infile, line))
        lines.push_back(line);

    return lines;
}

static bool file_exists(const std::string &filename)
{
    std::ifstream infile(filename);
    return infile.good();
}

#define ASSERT_FILE_EXISTS(NAME) ASSERT_TRUE(file_exists(NAME))
#define ASSERT_FILE_NOT_EXISTS(NAME) ASSERT_FALSE(file_exists(NAME))
#define ASSERT_FILE_EQUAL(FILE_A, FILE_B) ASSERT_TRUE(equal_lines(FILE_A, FILE_B))

#endif