Skip to content

Commit

Permalink
Added tests for the new string types.
Browse files Browse the repository at this point in the history
Update #414
  • Loading branch information
eugenwintersberger committed Sep 2, 2019
1 parent 13dccb8 commit 791eec1
Show file tree
Hide file tree
Showing 5 changed files with 255 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/h5cpp/stl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
#include <h5cpp/stl/vector_memory_adapter.hpp>
#include <h5cpp/stl/complex_memory_adapter.hpp>
#include <h5cpp/stl/complex_datatype_trait.hpp>
#include <h5cpp/stl/string_types.hpp>
2 changes: 2 additions & 0 deletions src/h5cpp/stl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ set(SOURCES )
set(HEADERS
${dir}/vector_memory_adapter.hpp
${dir}/complex_memory_adapter.hpp
${dir}/string_types.hpp
${dir}/complex_datatype_trait.hpp
)

install(FILES ${HEADERS}
Expand Down
158 changes: 158 additions & 0 deletions src/h5cpp/stl/string_types.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//
// (c) Copyright 2019 Eugen Wintersberger <[email protected]>
//
// This file is part of h5cpp.
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the
// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
// Boston, MA 02110-1301 USA
// ===========================================================================
//
// Author: Eugen Wintersberger <[email protected]>
// Created on: Sep 1, 2019
//
#pragma once

#include <string>


namespace hdf5 {
namespace type {

/**
* @brief fixed UTF8 string type
*/
class FixedUTF8String : public std::string
{
public:
using std::string::string;
FixedUTF8String(const std::string &string) : std::string(string) {}
FixedUTF8String(std::string &&string) : std::string(std::move(string)) {}
};

/**
* @brief fixed ASCII string type
*/
class FixedASCIIString : public std::string
{
public:
using std::string::string;
FixedASCIIString(const std::string &string) : std::string(string) {}
FixedASCIIString(std::string &&string) : std::string(std::move(string)) {}
};

/**
* @brief variable UTF8 string type
*/
class VariableUTF8String : public std::string
{
public:
using std::string::string;
VariableUTF8String(const std::string &string) : std::string(string) {}
VariableUTF8String(std::string &&string) : std::string(std::move(string)) {}
};


/**
* @brief variable ASCII string type
*/
class VariableASCIIString : public std::string
{
public:
using std::string::string;
VariableASCIIString(const std::string &string) : std::string(string) {}
VariableASCIIString(std::string &&string) : std::string(std::move(string)) {}
};


} // end of namespace type


namespace datatype {

/**
* @brief type trait for a fixed size UTF8 string
*/
template<>
class Trait<type::FixedUTF8String>
{
public:
using Type = type::FixedUTF8String;
using TypeClass = datatype::String;

static datatype::String create(const Type &value)
{
datatype::String type = String::fixed(value.size());
type.encoding(datatype::CharacterEncoding::UTF8);
return type;
}
};

/**
* @brief type trait for a fixed size ASCII string
*/
template<>
class Trait<type::FixedASCIIString>
{
public:
using Type = type::FixedASCIIString;
using TypeClass = datatype::String;

static datatype::String create(const Type &value)
{
auto type = String::fixed(value.size());
type.encoding(datatype::CharacterEncoding::ASCII);
return type;
}
};

/**
* @brief type trait for a variable size UTF8 string
*/
template<>
class Trait<type::VariableUTF8String>
{
public:
using Type = type::VariableUTF8String;
using TypeClass = datatype::String;

static datatype::String create(const Type & = Type())
{
auto type = String::variable();
type.encoding(datatype::CharacterEncoding::UTF8);
return type;
}
};

/**
* @brief type trait for a variable ASCII string
*/
template<>
class Trait<type::VariableASCIIString>
{
public:
using Type = type::VariableASCIIString;
using TypeClass = datatype::String;

static datatype::String create(const Type & = Type())
{
auto type = String::variable();
type.encoding(datatype::CharacterEncoding::ASCII);
return type;
}
};


} // end of namespace datatype
} // end of namespace hdf5
1 change: 1 addition & 0 deletions test/stl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ set(test_sources
${test_sources}
${dir}/vector_memory_adapter_test.cpp
${dir}/complex_datatype_trait_test.cpp
${dir}/fixed_utf8_string_test.cpp
PARENT_SCOPE)
93 changes: 93 additions & 0 deletions test/stl/fixed_utf8_string_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// (c) Copyright 2019 Eugen Wintersberger <[email protected]>
//
// This file is part of h5pp.
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
// License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this library; if not, write to the
// Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
// Boston, MA 02110-1301 USA
// ===========================================================================
//
// Author: Eugen Wintersberger <[email protected]>
// Created on: Sep 2, 2019
//

#include <h5cpp/hdf5.hpp>
#include <h5cpp/stl.hpp>
#include <gtest/gtest.h>

using namespace hdf5;


template<typename T>
class BaseStringTest : public ::testing::Test
{
protected:
std::string original_string;
std::string temp_string;

BaseStringTest():
original_string("hello world"),
temp_string("hello world")
{}
};


using test_types = ::testing::Types<type::FixedUTF8String,
type::FixedASCIIString,
type::VariableASCIIString,
type::VariableUTF8String>;

TYPED_TEST_CASE(BaseStringTest, test_types);


TYPED_TEST(BaseStringTest, test_literal_construction)
{
TypeParam new_string = "hello world";
EXPECT_EQ(this->original_string, new_string);
}


TYPED_TEST(BaseStringTest, test_copy_construction)
{
TypeParam new_string = this->original_string;
EXPECT_EQ(this->original_string, new_string);
}

TYPED_TEST(BaseStringTest, test_move_construction)
{
TypeParam new_string = std::move(this->temp_string);
EXPECT_EQ(this->original_string, new_string);

}

TYPED_TEST(BaseStringTest, test_literal_assignment)
{
TypeParam new_string;
new_string = "hello world";
EXPECT_EQ(this->original_string, new_string);
}

TYPED_TEST(BaseStringTest, test_copy_assignment)
{
TypeParam new_string;
new_string = this->original_string;
}

TYPED_TEST(BaseStringTest, test_move_assignment)
{
TypeParam new_string;
new_string = std::move(this->original_string);
}

0 comments on commit 791eec1

Please sign in to comment.