File: test_subscribe_options.cpp

package info (click to toggle)
paho.mqtt.cpp 1.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,672 kB
  • sloc: cpp: 13,068; ansic: 113; sh: 55; makefile: 22
file content (129 lines) | stat: -rw-r--r-- 4,215 bytes parent folder | download | duplicates (2)
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
// test_subscribe_options.cpp
//
// Unit tests for the subscribe_options class in the Paho MQTT C++ library.
//

/*******************************************************************************
 * Copyright (c) 2025 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 v2.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *    http://www.eclipse.org/legal/epl-v20.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
 *******************************************************************************/

#define UNIT_TESTS

#include <cstring>

#include "catch2_version.h"
#include "mqtt/subscribe_options.h"

using namespace mqtt;

// The struct_id for the Paho C MQTTSubscribe_options struct.
static constexpr const char* STRUCT_ID = "MQSO";

/////////////////////////////////////////////////////////////////////////////

// ----------------------------------------------------------------------
// Test default constructor
// ----------------------------------------------------------------------

TEST_CASE("subscribe_options dflt ctor", "[options]")
{
    subscribe_options opts;
    const auto& copts = opts.c_struct();

    REQUIRE(0 == memcmp(copts.struct_id, STRUCT_ID, 4));
    REQUIRE(0 == copts.noLocal);
    REQUIRE(0 == copts.retainAsPublished);
    REQUIRE(0 == copts.retainHandling);

    REQUIRE(!opts.get_no_local());
    REQUIRE(!opts.get_retain_as_published());
    REQUIRE(0 == opts.get_retain_handling());
}

// ----------------------------------------------------------------------
// Test constructor
// ----------------------------------------------------------------------

TEST_CASE("subscribe_options ctor", "[options]")
{
    subscribe_options opts{
        subscribe_options::NO_LOCAL,
        subscribe_options::RETAIN_AS_PUBLISHED,
        subscribe_options::DONT_SEND_RETAINED,
    };
    const auto& copts = opts.c_struct();

    REQUIRE(0 == memcmp(copts.struct_id, STRUCT_ID, 4));
    REQUIRE(0 != copts.noLocal);
    REQUIRE(0 != copts.retainAsPublished);
    REQUIRE(2 == copts.retainHandling);

    REQUIRE(opts.get_no_local());
    REQUIRE(opts.get_retain_as_published());
    REQUIRE(subscribe_options::DONT_SEND_RETAINED == opts.get_retain_handling());
}

// ----------------------------------------------------------------------
// Test copy constructor
// ----------------------------------------------------------------------

TEST_CASE("subscribe_options copy ctor", "[options]")
{
    subscribe_options org_opts{
        subscribe_options::NO_LOCAL,
        subscribe_options::RETAIN_AS_PUBLISHED,
        subscribe_options::DONT_SEND_RETAINED,
    };

    subscribe_options opts{org_opts};
    const auto& copts = opts.c_struct();

    REQUIRE(0 == memcmp(copts.struct_id, STRUCT_ID, 4));
    REQUIRE(0 != copts.noLocal);
    REQUIRE(0 != copts.retainAsPublished);
    REQUIRE(2 == copts.retainHandling);

    REQUIRE(opts.get_no_local());
    REQUIRE(opts.get_retain_as_published());
    REQUIRE(subscribe_options::DONT_SEND_RETAINED == opts.get_retain_handling());
}

// ----------------------------------------------------------------------
// Test copy assignment
// ----------------------------------------------------------------------

TEST_CASE("subscribe_options copy assignment", "[options]")
{
    subscribe_options org_opts{
        subscribe_options::NO_LOCAL,
        subscribe_options::RETAIN_AS_PUBLISHED,
        subscribe_options::DONT_SEND_RETAINED,
    };

    subscribe_options opts;

    opts = org_opts;
    const auto& copts = opts.c_struct();

    REQUIRE(0 == memcmp(copts.struct_id, STRUCT_ID, 4));
    REQUIRE(0 != copts.noLocal);
    REQUIRE(0 != copts.retainAsPublished);
    REQUIRE(2 == copts.retainHandling);

    REQUIRE(opts.get_no_local());
    REQUIRE(opts.get_retain_as_published());
    REQUIRE(subscribe_options::DONT_SEND_RETAINED == opts.get_retain_handling());
}