add multitype data type and unittest (including tests for ubuf)

This commit is contained in:
Axel Kohlmeyer
2023-06-06 20:38:15 -04:00
parent 9cc6839a8b
commit 14acb3e0ca
3 changed files with 168 additions and 2 deletions

View File

@ -232,6 +232,84 @@ union ubuf {
ubuf(const int64_t &arg) : i(arg) {}
ubuf(const int &arg) : i(arg) {}
};
/** Data structure for dynamic typing of int, bigint, and double
*
* Using this union allows to store any of the supported data types
* in the same container and allows to "see" its current type.
\verbatim embed:rst
**Usage:**
.. code-block:: c++
:caption: To store data in multitype array:
multitype m[5];
int foo = 1;
double bar = 2.5;
bigint baz = 1<<40 - 1;
m[0] = foo;
m[1] = bar;
m[2] = -1;
m[3] = 2.0;
m[4] = baz;
.. code-block:: c++
:caption: To format data from multitype array into a space separated string:
std::string str;
for (int i = 0; i < 5; ++i) {
switch (m[i].type) {
case multitype::DOUBLE:
str += std::to_string(m[i].data.d) + ' ';
break;
case multitype::INT:
str += std::to_string(m[i].data.i) + ' ';
break;
case multitype::BIGINT:
str += std::to_string(m[i].data.b) + ' ';
break;
default:
break;
}
}
\endverbatim
*/
struct multitype {
enum { NONE, DOUBLE, INT, BIGINT };
int type;
union {
double d;
int i;
int64_t b;
} data;
multitype() : type(NONE) { data.d = 0.0; }
multitype(const multitype &) = default;
multitype(multitype &&) = default;
~multitype() = default;
multitype &operator=(const double &_d)
{
type = DOUBLE;
data.d = _d;
return *this;
}
multitype &operator=(const int &_i)
{
type = INT;
data.i = _i;
return *this;
}
multitype &operator=(const int64_t &_b)
{
type = BIGINT;
data.b = _b;
return *this;
}
};
} // namespace LAMMPS_NS
// preprocessor macros for compiler specific settings

View File

@ -7,6 +7,10 @@ add_executable(test_mempool test_mempool.cpp)
target_link_libraries(test_mempool PRIVATE lammps GTest::GMockMain)
add_test(NAME MemPool COMMAND test_mempool)
add_executable(test_lmptype test_lmptype.cpp)
target_link_libraries(test_lmptype PRIVATE lammps GTest::GMockMain)
add_test(NAME LmpType COMMAND test_lmptype)
add_executable(test_argutils test_argutils.cpp)
target_link_libraries(test_argutils PRIVATE lammps GTest::GMockMain)
add_test(NAME ArgUtils COMMAND test_argutils)

View File

@ -0,0 +1,84 @@
/* ----------------------------------------------------------------------
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 "lmptype.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <string>
using namespace LAMMPS_NS;
TEST(Types, ubuf)
{
double buf[3];
double d1 = 0.1;
int i1 = -10;
#if defined(LAMMPS_SMALLSMALL)
bigint b1 = 2048;
#else
bigint b1 = (1L << 58) + (1L << 50);
#endif
buf[0] = d1;
buf[1] = ubuf(i1).d;
buf[2] = ubuf(b1).d;
EXPECT_EQ(d1, buf[0]);
EXPECT_EQ(i1, (int)ubuf(buf[1]).i);
EXPECT_EQ(b1, (bigint)ubuf(buf[2]).i);
}
TEST(Types, multitype)
{
multitype m[6];
int64_t b1 = (3L << 48) - 1;
int i1 = 20;
double d1 = 0.1;
m[0] = b1;
m[1] = i1;
m[2] = d1;
#if !defined(LAMMPS_SMALLSMALL)
m[3] = -((1L << 40) + (1L << 50));
#endif
m[4] = -1023;
m[5] = -2.225;
#if defined(LAMMPS_SMALLSMALL)
EXPECT_EQ(m[0].type, multitype::INT);
#else
EXPECT_EQ(m[0].type, multitype::BIGINT);
#endif
EXPECT_EQ(m[1].type, multitype::INT);
EXPECT_EQ(m[2].type, multitype::DOUBLE);
#if defined(LAMMPS_SMALLSMALL)
EXPECT_EQ(m[3].type, multitype::NONE);
#else
EXPECT_EQ(m[3].type, multitype::BIGINT);
#endif
EXPECT_EQ(m[4].type, multitype::INT);
EXPECT_EQ(m[5].type, multitype::DOUBLE);
EXPECT_EQ(m[0].data.b, b1);
EXPECT_EQ(m[1].data.i, i1);
EXPECT_EQ(m[2].data.d, d1);
#if !defined(LAMMPS_SMALLSMALL)
EXPECT_EQ(m[3].data.b, -((1L << 40) + (1L << 50)));
#endif
EXPECT_EQ(m[4].data.i, -1023);
EXPECT_EQ(m[5].data.d, -2.225);
}