File: test_text_file_reader.cpp

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 (193 lines) | stat: -rw-r--r-- 6,167 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/* ----------------------------------------------------------------------
   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.
------------------------------------------------------------------------- */

#include "info.h"
#include "input.h"
#include "text_file_reader.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

#include "../testing/core.h"

#include <cstring>
#include <iostream>
#include <mpi.h>
#include <vector>

using namespace LAMMPS_NS;
using LAMMPS_NS::utils::split_words;

// whether to print verbose output (i.e. not capturing LAMMPS screen output).
bool verbose = false;

class TextFileReaderTest : public ::testing::Test {

protected:
    void TearDown() override
    {
        platform::unlink("text_reader_one.file");
        platform::unlink("text_reader_two.file");
    }

    void test_files()
    {
        FILE *fp = fopen("text_reader_one.file", "w");
        fputs("# test file 1 for text file reader\n\n\none\n  two  \n\n"
              "three  # with comment\nfour   ! with non-comment\n"
              "# comments only\n        five\n#END\n",
              fp);
        fclose(fp);

        fp = fopen("text_reader_two.file", "w");
        fputs("# test file for atomfile style variable\n\n"
              "4  # four lines\n4 0.5   #with comment\n"
              "2 -0.5         \n3 1.5\n1 -1.5\n\n"
              "2\n10 1.0 # test\n13 1.0\n\n######\n"
              "4\n1 4.0 # test\n2 3.0\n3 2.0\n4 1.0\n#END\n",
              fp);
        fclose(fp);
    }
};

TEST_F(TextFileReaderTest, nofile)
{
    ASSERT_THROW(
        { TextFileReader reader("text_reader_noexist.file", "test"); }, FileReaderException);
}

// this test cannot work on windows due to its non unix-like permission system

#if !defined(_WIN32)
TEST_F(TextFileReaderTest, permissions)
{
    platform::unlink("text_reader_noperms.file");
    FILE *fp = fopen("text_reader_noperms.file", "w");
    ASSERT_NE(fp, nullptr);
    fputs("word\n", fp);
    fclose(fp);
    chmod("text_reader_noperms.file", 0);
    ASSERT_THROW(
        { TextFileReader reader("text_reader_noperms.file", "test"); }, FileReaderException);
    platform::unlink("text_reader_noperms.file");
}
#endif

TEST_F(TextFileReaderTest, nofp)
{
    ASSERT_THROW({ TextFileReader reader(nullptr, "test"); }, FileReaderException);
}

TEST_F(TextFileReaderTest, buffer)
{
    test_files();
    auto *reader = new TextFileReader("text_reader_two.file", "test");
    reader->set_bufsize(4096);
    reader->next_line();
    ASSERT_THROW({ reader->set_bufsize(20); }, FileReaderException);
    delete reader;
}

TEST_F(TextFileReaderTest, usefp)
{
    test_files();
    FILE *fp = fopen("text_reader_two.file", "r");
    ASSERT_NE(fp, nullptr);

    auto *reader = new TextFileReader(fp, "test");
    auto *line   = reader->next_line();
    ASSERT_STREQ(line, "4  ");
    line = reader->next_line(1);
    ASSERT_STREQ(line, "4 0.5   ");
    ASSERT_NO_THROW({ reader->skip_line(); });
    auto values = reader->next_values(1);
    ASSERT_EQ(values.count(), 2);
    ASSERT_EQ(values.next_int(), 3);
    ASSERT_STREQ(values.next_string().c_str(), "1.5");
    ASSERT_NE(reader->next_line(), nullptr);
    double data[20];
    ASSERT_THROW({ reader->next_dvector(data, 20); }, FileReaderException);
    ASSERT_THROW({ reader->skip_line(); }, EOFException);
    ASSERT_EQ(reader->next_line(), nullptr);
    delete reader;

    // check that we reached EOF and the destructor didn't close the file.
    ASSERT_NE(feof(fp), 0);
    ASSERT_EQ(fclose(fp), 0);
}

TEST_F(TextFileReaderTest, comments)
{
    test_files();
    TextFileReader reader("text_reader_two.file", "test");
    reader.ignore_comments = true;
    auto *line             = reader.next_line();
    ASSERT_STREQ(line, "4  ");
    line = reader.next_line(1);
    ASSERT_STREQ(line, "4 0.5   ");
    ASSERT_NO_THROW({ reader.skip_line(); });
    auto values = reader.next_values(1);
    ASSERT_EQ(values.count(), 2);
    ASSERT_EQ(values.next_int(), 3);
    ASSERT_STREQ(values.next_string().c_str(), "1.5");
    ASSERT_NE(reader.next_line(), nullptr);
    double data[20];
    ASSERT_THROW({ reader.next_dvector(data, 20); }, FileReaderException);
    ASSERT_THROW({ reader.skip_line(); }, EOFException);
    ASSERT_EQ(reader.next_line(), nullptr);
}

TEST_F(TextFileReaderTest, nocomments)
{
    test_files();
    TextFileReader reader("text_reader_one.file", "test");
    reader.ignore_comments = false;
    auto *line             = reader.next_line();
    ASSERT_STREQ(line, "# test file 1 for text file reader\n");
    line = reader.next_line(1);
    ASSERT_STREQ(line, "one\n");
    ASSERT_NO_THROW({ reader.skip_line(); });
    auto values = reader.next_values(4);
    ASSERT_EQ(values.count(), 4);
    ASSERT_STREQ(values.next_string().c_str(), "three");
    ASSERT_STREQ(values.next_string().c_str(), "#");
    ASSERT_STREQ(values.next_string().c_str(), "with");
    try {
        reader.next_values(100);
        FAIL() << "No exception thrown\n";
    } catch (EOFException &e) {
        ASSERT_STREQ(e.what(), "Incorrect format in test file! 9/100 parameters");
    }
    ASSERT_THROW({ reader.skip_line(); }, EOFException);
    ASSERT_EQ(reader.next_line(), nullptr);
}

int main(int argc, char **argv)
{
    MPI_Init(&argc, &argv);
    ::testing::InitGoogleMock(&argc, argv);

    // handle arguments passed via environment variable
    if (const char *var = getenv("TEST_ARGS")) {
        std::vector<std::string> env = split_words(var);
        for (auto arg : env) {
            if (arg == "-v") {
                verbose = true;
            }
        }
    }
    if ((argc > 1) && (strcmp(argv[1], "-v") == 0)) verbose = true;

    int rv = RUN_ALL_TESTS();
    MPI_Finalize();
    return rv;
}