mirror of
https://github.com/eclipse-paho/paho.mqtt.cpp.git
synced 2025-09-15 12:58:39 +08:00
Started a name/value collection. Moved string collection unit test from CppUnit to Catch2
This commit is contained in:
parent
f8c1270ad1
commit
e337b3e173
@ -33,6 +33,7 @@
|
||||
#include "mqtt/will_options.h"
|
||||
#include "mqtt/ssl_options.h"
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <chrono>
|
||||
|
||||
namespace mqtt {
|
||||
@ -46,7 +47,7 @@ namespace mqtt {
|
||||
class connect_options
|
||||
{
|
||||
/** The default C struct */
|
||||
static const MQTTAsync_connectOptions DFLT_C_STRUCT ;
|
||||
static const MQTTAsync_connectOptions DFLT_C_STRUCT;
|
||||
|
||||
/** The underlying C connection options */
|
||||
MQTTAsync_connectOptions opts_;
|
||||
|
||||
@ -24,8 +24,10 @@
|
||||
#ifndef __mqtt_string_collection_h
|
||||
#define __mqtt_string_collection_h
|
||||
|
||||
#include "MQTTAsync.h"
|
||||
#include "mqtt/types.h"
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
namespace mqtt {
|
||||
@ -209,7 +211,6 @@ public:
|
||||
* @return A const reference to the string.
|
||||
*/
|
||||
const string& operator[](size_t i) const { return coll_[i]; }
|
||||
|
||||
/**
|
||||
* Gets a pointer to an array of NUL-terminated C string pointers.
|
||||
* This is the collection type supported by the underlying Paho C
|
||||
@ -222,7 +223,6 @@ public:
|
||||
*
|
||||
*/
|
||||
char* const* c_arr() const { return (char* const *) cArr_.data(); }
|
||||
//const char* const* c_arr() const { return cArr_.data(); }
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
@ -233,6 +233,85 @@ using string_collection_ptr = string_collection::ptr_t;
|
||||
/** Smart/shared pointer to a const string_collection */
|
||||
using const_string_collection_ptr = string_collection::const_ptr_t;
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* A colleciton of name/value string pairs.
|
||||
*/
|
||||
class name_value_collection
|
||||
{
|
||||
/** The type for the collection of name/value pairs */
|
||||
using collection_type = std::map<string, string>;
|
||||
/** The type for the C pointers to pass to Paho C */
|
||||
using c_arr_type = std::vector<MQTTAsync_nameValue>;
|
||||
|
||||
/**
|
||||
* The name/value pairs.
|
||||
*/
|
||||
collection_type map_;
|
||||
/**
|
||||
* A collection of pairs of NUL-terminated C strings.
|
||||
*/
|
||||
c_arr_type cArr_;
|
||||
/**
|
||||
* Updated the cArr_ object to agree with the values in coll_
|
||||
* This should be called any time the coll_ variable is modified
|
||||
* <i>in any way</i>.
|
||||
*/
|
||||
void update_c_arr();
|
||||
|
||||
public:
|
||||
/** Smart/shared pointer to an object of this type */
|
||||
using ptr_t = std::shared_ptr<name_value_collection>;
|
||||
/** Smart/shared pointer to a const object of this type */
|
||||
using const_ptr_t = std::shared_ptr<const name_value_collection>;
|
||||
|
||||
/**
|
||||
* Default construtor for an empty collection.
|
||||
*/
|
||||
name_value_collection() =default;
|
||||
/**
|
||||
* Creates a name/value collection from an underlying STL collection.
|
||||
* @param map The collection of name/value pairs.
|
||||
*/
|
||||
name_value_collection(const collection_type& map) : map_(map) {
|
||||
update_c_arr();
|
||||
}
|
||||
/**
|
||||
* Creates a name/value collection from an underlying STL collection.
|
||||
* @param map The collection of name/value pairs.
|
||||
*/
|
||||
name_value_collection(collection_type&& map) : map_(std::move(map)) {
|
||||
update_c_arr();
|
||||
}
|
||||
/**
|
||||
* Copy constructor.
|
||||
* @param other Another collection of name/value pairs.
|
||||
*/
|
||||
name_value_collection(const name_value_collection& other)
|
||||
: map_(other.map_) {
|
||||
update_c_arr();
|
||||
}
|
||||
/**
|
||||
* Move constructor.
|
||||
* @param other Another collection of name/value pairs
|
||||
*/
|
||||
name_value_collection(name_value_collection&& other) = default;
|
||||
/**
|
||||
* Gets a pointer to an array of NUL-terminated C string pointer pairs.
|
||||
* This is a collection type supported by the underlying Paho C
|
||||
* library. The returned pointer is guaranteed valid so long as the
|
||||
* object is not updated. The return value may change if the object is
|
||||
* modified, so the application should not cache the return value, but
|
||||
* rather request the value when needed.
|
||||
* @return pointer to an array of NUL-terminated C string pointer pairs
|
||||
* for name/values. The array is terminated by a NULL/NULL pair.
|
||||
*
|
||||
*/
|
||||
char* const* c_arr() const { return (char* const *) cArr_.data(); }
|
||||
};
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// end namespace mqtt
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
// string_collection.cpp
|
||||
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017 Frank Pagliughi <fpagliughi@mindspring.com>
|
||||
* Copyright (c) 2017-2020 Frank Pagliughi <fpagliughi@mindspring.com>
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
@ -16,7 +16,6 @@
|
||||
* Frank Pagliughi - initial implementation and documentation
|
||||
*******************************************************************************/
|
||||
|
||||
|
||||
#include "mqtt/string_collection.h"
|
||||
|
||||
namespace mqtt {
|
||||
@ -95,6 +94,19 @@ void string_collection::clear()
|
||||
cArr_.clear();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void name_value_collection::update_c_arr()
|
||||
{
|
||||
cArr_.clear();
|
||||
cArr_.reserve(map_.size());
|
||||
for (const auto& m : map_) {
|
||||
cArr_.push_back(
|
||||
MQTTAsync_nameValue{ m.first.c_str(), m.second.c_str() }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// end namespace mqtt
|
||||
}
|
||||
|
||||
@ -1,347 +0,0 @@
|
||||
// string_collection_test.h
|
||||
// Unit tests for the string_collection class in the Paho MQTT C++ library.
|
||||
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017 Frank Pagliughi <fpagliughi@mindspring.com>
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Frank Pagliughi - initial implementation and documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __mqtt_string_collection_test_h
|
||||
#define __mqtt_string_collection_test_h
|
||||
|
||||
#include "mqtt/string_collection.h"
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
|
||||
#include <cppunit/ui/text/TestRunner.h>
|
||||
#include <cppunit/extensions/HelperMacros.h>
|
||||
|
||||
|
||||
namespace mqtt {
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
class string_collection_test : public CppUnit::TestFixture
|
||||
{
|
||||
CPPUNIT_TEST_SUITE( string_collection_test );
|
||||
|
||||
CPPUNIT_TEST( test_dflt_ctor );
|
||||
CPPUNIT_TEST( test_str_copy_ctor );
|
||||
CPPUNIT_TEST( test_str_move_ctor );
|
||||
CPPUNIT_TEST( test_vec_copy_ctor );
|
||||
CPPUNIT_TEST( test_vec_move_ctor );
|
||||
CPPUNIT_TEST( test_ini_str_ctor );
|
||||
CPPUNIT_TEST( test_ini_cstr_ctor );
|
||||
CPPUNIT_TEST( test_copy_ctor );
|
||||
CPPUNIT_TEST( test_move_ctor );
|
||||
CPPUNIT_TEST( test_copy_assignment );
|
||||
CPPUNIT_TEST( test_move_assignment );
|
||||
CPPUNIT_TEST( test_push_str );
|
||||
CPPUNIT_TEST( test_push_cstr );
|
||||
CPPUNIT_TEST( test_clear );
|
||||
|
||||
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
const string EMPTY_STR;
|
||||
|
||||
const string STR { "Some random string" };
|
||||
const std::vector<string> VEC { "test0", "test1", "test2" };
|
||||
|
||||
public:
|
||||
void setUp() {}
|
||||
void tearDown() {}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the default constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_dflt_ctor() {
|
||||
string_collection sc;
|
||||
|
||||
CPPUNIT_ASSERT(sc.empty());
|
||||
CPPUNIT_ASSERT_EQUAL(size_t(0), sc.size());
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the string copy constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_str_copy_ctor() {
|
||||
string_collection sc(STR);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(size_t(1), sc.size());
|
||||
CPPUNIT_ASSERT_EQUAL(STR, sc[0]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
CPPUNIT_ASSERT(!strcmp(STR.c_str(), c_arr[0]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the string move constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_str_move_ctor() {
|
||||
string str(STR);
|
||||
string_collection sc(std::move(str));
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(size_t(1), sc.size());
|
||||
CPPUNIT_ASSERT_EQUAL(STR, sc[0]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
CPPUNIT_ASSERT(!strcmp(STR.c_str(), c_arr[0]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the vector copy constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_vec_copy_ctor() {
|
||||
string_collection sc(VEC);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC.size(), sc.size());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[0], sc[0]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[1], sc[1]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[2], sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the vector move constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_vec_move_ctor() {
|
||||
std::vector<string> vec{ VEC };
|
||||
|
||||
string_collection sc(std::move(vec));
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC.size(), sc.size());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[0], sc[0]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[1], sc[1]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[2], sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the ini vector constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_ini_str_ctor() {
|
||||
string_collection sc( { VEC[0], VEC[1], VEC[2] } );
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(size_t(3), sc.size());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[0], sc[0]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[1], sc[1]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[2], sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the ini vector constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_ini_cstr_ctor() {
|
||||
string_collection sc( { "test0", "test1", "test2" } );
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(size_t(3), sc.size());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(string("test0"), sc[0]);
|
||||
CPPUNIT_ASSERT_EQUAL(string("test1"), sc[1]);
|
||||
CPPUNIT_ASSERT_EQUAL(string("test2"), sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
CPPUNIT_ASSERT(!strcmp(sc[0].c_str(), c_arr[0]));
|
||||
CPPUNIT_ASSERT(!strcmp(sc[1].c_str(), c_arr[1]));
|
||||
CPPUNIT_ASSERT(!strcmp(sc[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the copy constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_copy_ctor() {
|
||||
string_collection orgSC(VEC);
|
||||
string_collection sc(orgSC);;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC.size(), sc.size());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[0], sc[0]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[1], sc[1]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[2], sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the move constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_move_ctor() {
|
||||
string_collection orgSC(VEC);
|
||||
string_collection sc(std::move(orgSC));
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC.size(), sc.size());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[0], sc[0]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[1], sc[1]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[2], sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the copy assignment
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_copy_assignment() {
|
||||
string_collection orgSC(VEC);
|
||||
string_collection sc;
|
||||
|
||||
sc = orgSC;
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC.size(), sc.size());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[0], sc[0]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[1], sc[1]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[2], sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the move assignment
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_move_assignment() {
|
||||
string_collection orgSC(VEC);
|
||||
string_collection sc;
|
||||
|
||||
sc = std::move(orgSC);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC.size(), sc.size());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[0], sc[0]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[1], sc[1]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[2], sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the push back of strings
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_push_str() {
|
||||
string_collection sc;
|
||||
|
||||
for (const auto& s : VEC)
|
||||
sc.push_back(s);
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC.size(), sc.size());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[0], sc[0]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[1], sc[1]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[2], sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the push back of C strings
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_push_cstr() {
|
||||
string_collection sc;
|
||||
|
||||
for (const auto& s : VEC)
|
||||
sc.push_back(s.c_str());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC.size(), sc.size());
|
||||
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[0], sc[0]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[1], sc[1]);
|
||||
CPPUNIT_ASSERT_EQUAL(VEC[2], sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
CPPUNIT_ASSERT(!strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the clear method
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
void test_clear() {
|
||||
string_collection sc(VEC);
|
||||
|
||||
CPPUNIT_ASSERT(!sc.empty());
|
||||
|
||||
sc.clear();
|
||||
|
||||
CPPUNIT_ASSERT(sc.empty());
|
||||
CPPUNIT_ASSERT_EQUAL(size_t(0), sc.size());
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// end namespace mqtt
|
||||
}
|
||||
|
||||
#endif // __mqtt_string_collection_test_h
|
||||
|
||||
@ -18,7 +18,6 @@
|
||||
*******************************************************************************/
|
||||
|
||||
#include "buffer_ref_test.h"
|
||||
#include "string_collection_test.h"
|
||||
#include "async_client_test.h"
|
||||
#include "async_client_v3_test.h"
|
||||
#include "client_test.h"
|
||||
@ -41,7 +40,6 @@ using namespace CppUnit;
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::buffer_ref_test );
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::string_collection_test );
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( mqtt::will_options_test );
|
||||
|
||||
#if defined(OPENSSL)
|
||||
|
||||
@ -30,6 +30,7 @@ find_package(Catch2 REQUIRED)
|
||||
|
||||
add_executable(unit_tests unit_tests.cpp
|
||||
test_properties.cpp
|
||||
test_string_collection.cpp
|
||||
)
|
||||
|
||||
# --- Link for executables ---
|
||||
|
||||
319
test/unit/test_string_collection.cpp
Normal file
319
test/unit/test_string_collection.cpp
Normal file
@ -0,0 +1,319 @@
|
||||
// test_string_collection.cpp
|
||||
//
|
||||
// Unit tests for the string_collection and name_value_collection classes in
|
||||
// the Paho MQTT C++ library.
|
||||
//
|
||||
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2017-2020 Frank Pagliughi <fpagliughi@mindspring.com>
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Frank Pagliughi - initial implementation and documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include <vector>
|
||||
#include <cstring>
|
||||
#include "catch2/catch.hpp"
|
||||
#include "mqtt/string_collection.h"
|
||||
|
||||
using namespace mqtt;
|
||||
|
||||
const string STR { "Some random string" };
|
||||
const std::vector<string> VEC { "test0", "test1", "test2" };
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the default constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_dflt_ctor", "[collections]")
|
||||
{
|
||||
string_collection sc;
|
||||
|
||||
REQUIRE(sc.empty());
|
||||
REQUIRE(sc.size() == size_t(0));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the string copy constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_str_copy_ctor", "[collections]")
|
||||
{
|
||||
string_collection sc(STR);
|
||||
|
||||
REQUIRE(size_t(1) == sc.size());
|
||||
REQUIRE(STR == sc[0]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
REQUIRE(0 == strcmp(STR.c_str(), c_arr[0]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the string move constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_str_move_ctor", "[collections]")
|
||||
{
|
||||
string str(STR);
|
||||
string_collection sc(std::move(str));
|
||||
|
||||
REQUIRE(size_t(1) == sc.size());
|
||||
REQUIRE(STR == sc[0]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
REQUIRE(0 == strcmp(STR.c_str(), c_arr[0]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the vector copy constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_vec_copy_ctor", "[collections]")
|
||||
{
|
||||
string_collection sc(VEC);
|
||||
|
||||
REQUIRE(VEC.size() == sc.size());
|
||||
|
||||
REQUIRE(VEC[0] == sc[0]);
|
||||
REQUIRE(VEC[1] == sc[1]);
|
||||
REQUIRE(VEC[2] == sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
REQUIRE(0 == strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
REQUIRE(0 == strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
REQUIRE(0 == strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the vector move constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_vec_move_ctor", "[collections]")
|
||||
{
|
||||
std::vector<string> vec{ VEC };
|
||||
|
||||
string_collection sc(std::move(vec));
|
||||
|
||||
REQUIRE(VEC.size() == sc.size());
|
||||
|
||||
REQUIRE(VEC[0] == sc[0]);
|
||||
REQUIRE(VEC[1] == sc[1]);
|
||||
REQUIRE(VEC[2] == sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
REQUIRE(0 == strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
REQUIRE(0 == strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
REQUIRE(0 == strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the ini vector constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_ini_str_ctor", "[collections]")
|
||||
{
|
||||
string_collection sc( { VEC[0], VEC[1], VEC[2] } );
|
||||
|
||||
REQUIRE(size_t(3) == sc.size());
|
||||
|
||||
REQUIRE(VEC[0] == sc[0]);
|
||||
REQUIRE(VEC[1] == sc[1]);
|
||||
REQUIRE(VEC[2] == sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
REQUIRE(0 == strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
REQUIRE(0 == strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
REQUIRE(0 == strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the ini vector constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_ini_cstr_ctor", "[collections]")
|
||||
{
|
||||
string_collection sc( { "test0", "test1", "test2" } );
|
||||
|
||||
REQUIRE(size_t(3) == sc.size());
|
||||
|
||||
REQUIRE(string("test0") == sc[0]);
|
||||
REQUIRE(string("test1") == sc[1]);
|
||||
REQUIRE(string("test2") == sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
REQUIRE(0 == strcmp(sc[0].c_str(), c_arr[0]));
|
||||
REQUIRE(0 == strcmp(sc[1].c_str(), c_arr[1]));
|
||||
REQUIRE(0 == strcmp(sc[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the copy constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_copy_ctor", "[collections]")
|
||||
{
|
||||
string_collection orgSC(VEC);
|
||||
string_collection sc(orgSC);;
|
||||
|
||||
REQUIRE(VEC.size() == sc.size());
|
||||
|
||||
REQUIRE(VEC[0] == sc[0]);
|
||||
REQUIRE(VEC[1] == sc[1]);
|
||||
REQUIRE(VEC[2] == sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
REQUIRE(0 == strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
REQUIRE(0 == strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
REQUIRE(0 == strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the move constructor
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_move_ctor", "[collections]")
|
||||
{
|
||||
string_collection orgSC(VEC);
|
||||
string_collection sc(std::move(orgSC));
|
||||
|
||||
REQUIRE(VEC.size() == sc.size());
|
||||
|
||||
REQUIRE(VEC[0] == sc[0]);
|
||||
REQUIRE(VEC[1] == sc[1]);
|
||||
REQUIRE(VEC[2] == sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
REQUIRE(0 == strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
REQUIRE(0 == strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
REQUIRE(0 == strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the copy assignment
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_copy_assignment", "[collections]")
|
||||
{
|
||||
string_collection orgSC(VEC);
|
||||
string_collection sc;
|
||||
|
||||
sc = orgSC;
|
||||
|
||||
REQUIRE(VEC.size() == sc.size());
|
||||
|
||||
REQUIRE(VEC[0] == sc[0]);
|
||||
REQUIRE(VEC[1] == sc[1]);
|
||||
REQUIRE(VEC[2] == sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
REQUIRE(0 == strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
REQUIRE(0 == strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
REQUIRE(0 == strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the move assignment
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_move_assignment", "[collections]")
|
||||
{
|
||||
string_collection orgSC(VEC);
|
||||
string_collection sc;
|
||||
|
||||
sc = std::move(orgSC);
|
||||
|
||||
REQUIRE(VEC.size() == sc.size());
|
||||
|
||||
REQUIRE(VEC[0] == sc[0]);
|
||||
REQUIRE(VEC[1] == sc[1]);
|
||||
REQUIRE(VEC[2] == sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
REQUIRE(0 == strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
REQUIRE(0 == strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
REQUIRE(0 == strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the push back of strings
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_push_str", "[collections]")
|
||||
{
|
||||
string_collection sc;
|
||||
|
||||
for (const auto& s : VEC)
|
||||
sc.push_back(s);
|
||||
|
||||
REQUIRE(VEC.size() == sc.size());
|
||||
|
||||
REQUIRE(VEC[0] == sc[0]);
|
||||
REQUIRE(VEC[1] == sc[1]);
|
||||
REQUIRE(VEC[2] == sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
REQUIRE(0 == strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
REQUIRE(0 == strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
REQUIRE(0 == strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the push back of C strings
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_push_cstr", "[collections]")
|
||||
{
|
||||
string_collection sc;
|
||||
|
||||
for (const auto& s : VEC)
|
||||
sc.push_back(s.c_str());
|
||||
|
||||
REQUIRE(VEC.size() == sc.size());
|
||||
|
||||
REQUIRE(VEC[0] == sc[0]);
|
||||
REQUIRE(VEC[1] == sc[1]);
|
||||
REQUIRE(VEC[2] == sc[2]);
|
||||
|
||||
auto c_arr = sc.c_arr();
|
||||
|
||||
REQUIRE(0 == strcmp(VEC[0].c_str(), c_arr[0]));
|
||||
REQUIRE(0 == strcmp(VEC[1].c_str(), c_arr[1]));
|
||||
REQUIRE(0 == strcmp(VEC[2].c_str(), c_arr[2]));
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// Test the clear method
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
TEST_CASE("test_clear", "[collections]")
|
||||
{
|
||||
string_collection sc(VEC);
|
||||
|
||||
REQUIRE_FALSE(sc.empty());
|
||||
|
||||
sc.clear();
|
||||
|
||||
REQUIRE(sc.empty());
|
||||
REQUIRE(size_t(0) == sc.size());
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user