2020-06-02 07:08:59 +08:00
|
|
|
/* ----------------------------------------------------------------------
|
|
|
|
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
|
|
|
|
http://lammps.sandia.gov, Sandia National Laboratories
|
|
|
|
Steve Plimpton, sjplimp@sandia.gov
|
|
|
|
|
|
|
|
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.
|
|
|
|
------------------------------------------------------------------------- */
|
|
|
|
|
2020-05-16 03:46:38 +08:00
|
|
|
#include "utils.h"
|
2020-06-13 13:54:58 +08:00
|
|
|
#include "gmock/gmock.h"
|
|
|
|
#include "gtest/gtest.h"
|
2020-06-12 13:29:42 +08:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2020-06-13 13:54:58 +08:00
|
|
|
#include <string>
|
2020-06-24 21:32:59 +08:00
|
|
|
#include <vector>
|
2020-05-16 03:46:38 +08:00
|
|
|
|
|
|
|
using namespace LAMMPS_NS;
|
|
|
|
using ::testing::Eq;
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, trim_comment)
|
|
|
|
{
|
2020-05-16 03:46:38 +08:00
|
|
|
auto trimmed = utils::trim_comment("some text # comment");
|
|
|
|
ASSERT_THAT(trimmed, Eq("some text "));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, count_words)
|
|
|
|
{
|
2020-06-04 22:35:36 +08:00
|
|
|
ASSERT_EQ(utils::count_words("some text # comment"), 4);
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, count_words_non_default)
|
|
|
|
{
|
2020-06-12 01:37:22 +08:00
|
|
|
ASSERT_EQ(utils::count_words("some text # comment", " #"), 3);
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, trim_and_count_words)
|
|
|
|
{
|
2020-06-04 22:35:36 +08:00
|
|
|
ASSERT_EQ(utils::trim_and_count_words("some text # comment"), 2);
|
2020-05-16 05:05:28 +08:00
|
|
|
}
|
2020-05-19 04:35:33 +08:00
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, count_words_with_extra_spaces)
|
|
|
|
{
|
2020-06-11 12:16:46 +08:00
|
|
|
ASSERT_EQ(utils::count_words(" some text # comment "), 4);
|
|
|
|
}
|
|
|
|
|
2020-06-24 21:32:59 +08:00
|
|
|
TEST(Utils, split_words_simple)
|
|
|
|
{
|
|
|
|
std::vector<std::string> list = utils::split_words("one two three");
|
|
|
|
ASSERT_EQ(list.size(), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Utils, split_words_quoted)
|
|
|
|
{
|
|
|
|
std::vector<std::string> list = utils::split_words("one 'two' \"three\"");
|
|
|
|
ASSERT_EQ(list.size(), 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Utils, split_words_escaped)
|
|
|
|
{
|
|
|
|
std::vector<std::string> list = utils::split_words("1\\' '\"two\"' 3\\\"");
|
|
|
|
ASSERT_EQ(list.size(), 3);
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, valid_integer1)
|
|
|
|
{
|
2020-05-19 04:35:33 +08:00
|
|
|
ASSERT_TRUE(utils::is_integer("10"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, valid_integer2)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_integer("-10"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, valid_integer3)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_integer("+10"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, valid_double1)
|
|
|
|
{
|
2020-05-19 04:35:33 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("10.0"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, valid_double2)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("1."));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, valid_double3)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_double(".0"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, valid_double4)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("-10.0"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, valid_double5)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("-1."));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, valid_double6)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("-.0"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, valid_double7)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("+10.0"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, valid_double8)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("+1."));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, valid_double9)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("+.0"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, empty_not_an_integer)
|
|
|
|
{
|
2020-05-19 04:35:33 +08:00
|
|
|
ASSERT_FALSE(utils::is_integer(""));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, empty_not_a_double)
|
|
|
|
{
|
2020-05-19 04:35:33 +08:00
|
|
|
ASSERT_FALSE(utils::is_double(""));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, text_not_an_integer)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_FALSE(utils::is_integer("one"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, text_not_a_double)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_FALSE(utils::is_double("half"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, double_not_an_integer1)
|
|
|
|
{
|
2020-05-19 04:35:33 +08:00
|
|
|
ASSERT_FALSE(utils::is_integer("10.0"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, double_not_an_integer2)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_FALSE(utils::is_integer(".0"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, double_not_an_integer3)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_FALSE(utils::is_integer("1."));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, integer_is_double1)
|
|
|
|
{
|
2020-05-19 04:35:33 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("10"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, integer_is_double2)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("-10"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, is_double_with_exponential)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("+1e02"));
|
2020-05-19 04:35:33 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, is_double_with_neg_exponential)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("1.0e-22"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, is_double_with_pos_exponential)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_TRUE(utils::is_double(".1e+22"));
|
2020-05-19 04:35:33 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, signed_double_and_exponential)
|
|
|
|
{
|
2020-05-19 04:35:33 +08:00
|
|
|
ASSERT_TRUE(utils::is_double("-10E-22"));
|
|
|
|
}
|
2020-05-31 22:14:09 +08:00
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, is_double_with_d_exponential)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_FALSE(utils::is_double("10d22"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, is_double_with_neg_d_exponential)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_FALSE(utils::is_double("10d-22"));
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, signed_double_and_d_exponential)
|
|
|
|
{
|
2020-05-31 22:14:09 +08:00
|
|
|
ASSERT_FALSE(utils::is_double("-10D-22"));
|
|
|
|
}
|
2020-05-31 22:36:44 +08:00
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, strmatch_beg)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(utils::strmatch("rigid/small/omp", "^rigid"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, strmatch_mid1)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(utils::strmatch("rigid/small/omp", "small"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, strmatch_mid2)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(utils::strmatch("rigid/small/omp", "omp"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, strmatch_end)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(utils::strmatch("rigid/small/omp", "/omp$"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, no_strmatch_beg)
|
|
|
|
{
|
|
|
|
ASSERT_FALSE(utils::strmatch("rigid/small/omp", "^small"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, no_strmatch_mid)
|
|
|
|
{
|
|
|
|
ASSERT_FALSE(utils::strmatch("rigid/small/omp", "none"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, no_strmatch_end)
|
|
|
|
{
|
|
|
|
ASSERT_FALSE(utils::strmatch("rigid/small/omp", "/opt$"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, strmatch_whole_line)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(utils::strmatch("ITEM: UNITS\n", "^\\s*ITEM: UNITS\\s*$"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, no_strmatch_whole_line)
|
|
|
|
{
|
|
|
|
ASSERT_FALSE(utils::strmatch("ITEM: UNITS\n", "^\\s*ITEM: UNIT\\s*$"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, strmatch_integer_in_line)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(utils::strmatch(" 5 angles\n", "^\\s*\\d+\\s+angles\\s"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, strmatch_float_in_line)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(utils::strmatch(" 5.0 angles\n", "^\\s*\\f+\\s+angles\\s"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, strmatch_int_as_float_in_line)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(utils::strmatch(" 5 angles\n", "^\\s*\\f+\\s+angles\\s"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, strmatch_char_range)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(utils::strmatch("rigid", "^[ip-s]+gid"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, strmatch_opt_range)
|
|
|
|
{
|
|
|
|
ASSERT_TRUE(utils::strmatch("rigid", "^[0-9]*[p-s]igid"));
|
2020-05-31 22:36:44 +08:00
|
|
|
}
|
2020-06-03 01:48:31 +08:00
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, path_join)
|
|
|
|
{
|
2020-06-03 01:48:31 +08:00
|
|
|
#if defined(_WIN32)
|
2020-06-13 13:54:58 +08:00
|
|
|
ASSERT_THAT(utils::path_join("c:\\parent\\folder", "filename"),
|
|
|
|
Eq("c:\\parent\\folder\\filename"));
|
2020-06-03 01:48:31 +08:00
|
|
|
#else
|
|
|
|
ASSERT_THAT(utils::path_join("/parent/folder", "filename"), Eq("/parent/folder/filename"));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, path_basename)
|
|
|
|
{
|
2020-06-03 01:48:31 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
ASSERT_THAT(utils::path_basename("c:\\parent\\folder\\filename"), Eq("filename"));
|
|
|
|
#else
|
|
|
|
ASSERT_THAT(utils::path_basename("/parent/folder/filename"), Eq("filename"));
|
|
|
|
#endif
|
|
|
|
}
|
2020-06-12 13:29:42 +08:00
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, getsyserror)
|
|
|
|
{
|
2020-06-12 13:29:42 +08:00
|
|
|
#if defined(__linux__)
|
2020-06-13 14:05:13 +08:00
|
|
|
errno = ENOENT;
|
2020-06-12 13:29:42 +08:00
|
|
|
std::string errmesg = utils::getsyserror();
|
|
|
|
ASSERT_THAT(errmesg, Eq("No such file or directory"));
|
|
|
|
#else
|
|
|
|
GTEST_SKIP();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-06-13 13:54:58 +08:00
|
|
|
TEST(Utils, potential_file)
|
|
|
|
{
|
2020-06-12 13:29:42 +08:00
|
|
|
FILE *fp;
|
2020-06-16 04:14:13 +08:00
|
|
|
fp = fopen("ctest1.txt", "w");
|
2020-06-13 13:54:58 +08:00
|
|
|
ASSERT_NE(fp, nullptr);
|
2020-06-16 04:14:13 +08:00
|
|
|
fputs("# DATE: 2020-02-20 UNITS: real CONTRIBUTOR: Nessuno\n", fp);
|
2020-06-12 13:29:42 +08:00
|
|
|
fclose(fp);
|
2020-06-16 04:14:13 +08:00
|
|
|
fp = fopen("ctest2.txt", "w");
|
|
|
|
ASSERT_NE(fp, nullptr);
|
|
|
|
fputs("# CONTRIBUTOR: Pippo\n", fp);
|
2020-06-14 18:22:47 +08:00
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
ASSERT_TRUE(utils::file_is_readable("ctest1.txt"));
|
|
|
|
ASSERT_TRUE(utils::file_is_readable("ctest2.txt"));
|
|
|
|
ASSERT_FALSE(utils::file_is_readable("no_such_file.txt"));
|
2020-06-12 13:29:42 +08:00
|
|
|
|
2020-06-16 04:14:13 +08:00
|
|
|
ASSERT_THAT(utils::get_potential_file_path("ctest1.txt"), Eq("ctest1.txt"));
|
|
|
|
ASSERT_THAT(utils::get_potential_file_path("no_such_file.txt"), Eq(""));
|
2020-06-12 13:29:42 +08:00
|
|
|
|
|
|
|
const char *folder = getenv("LAMMPS_POTENTIALS");
|
|
|
|
if (folder != nullptr) {
|
2020-06-13 13:54:58 +08:00
|
|
|
std::string path = utils::path_join(folder, "Cu_u3.eam");
|
|
|
|
EXPECT_THAT(utils::get_potential_file_path("Cu_u3.eam"), Eq(path));
|
2020-06-16 04:14:13 +08:00
|
|
|
EXPECT_THAT(utils::get_potential_units(path, "EAM"), Eq("metal"));
|
2020-06-12 13:29:42 +08:00
|
|
|
}
|
|
|
|
|
2020-06-16 04:14:13 +08:00
|
|
|
ASSERT_THAT(utils::get_potential_date("ctest1.txt", "Test"), Eq("2020-02-20"));
|
|
|
|
ASSERT_THAT(utils::get_potential_units("ctest1.txt", "Test"), Eq("real"));
|
|
|
|
ASSERT_THAT(utils::get_potential_date("ctest2.txt", "Test"), Eq(""));
|
|
|
|
ASSERT_THAT(utils::get_potential_units("ctest2.txt", "Test"), Eq(""));
|
2020-06-12 13:29:42 +08:00
|
|
|
|
2020-06-14 18:22:47 +08:00
|
|
|
remove("ctest1.txt");
|
|
|
|
remove("ctest2.txt");
|
2020-06-12 13:29:42 +08:00
|
|
|
}
|