-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
764 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
# LIBRARIES | ||
################################################## | ||
common_libolacommon_la_SOURCES += common/dmx/RunLengthEncoder.cpp | ||
common_libolacommon_la_SOURCES += \ | ||
common/dmx/RunLengthEncoder.cpp \ | ||
common/dmx/UniverseChannelAddress.cpp | ||
|
||
|
||
# TESTS | ||
################################################## | ||
test_programs += common/dmx/RunLengthEncoderTester | ||
test_programs += \ | ||
common/dmx/RunLengthEncoderTester \ | ||
common/dmx/UniverseChannelAddressTester | ||
|
||
common_dmx_RunLengthEncoderTester_SOURCES = common/dmx/RunLengthEncoderTest.cpp | ||
common_dmx_RunLengthEncoderTester_CXXFLAGS = $(COMMON_TESTING_FLAGS) | ||
common_dmx_RunLengthEncoderTester_LDADD = $(COMMON_TESTING_LIBS) | ||
|
||
common_dmx_UniverseChannelAddressTester_SOURCES = common/dmx/UniverseChannelAddressTest.cpp | ||
common_dmx_UniverseChannelAddressTester_CXXFLAGS = $(COMMON_TESTING_FLAGS) | ||
common_dmx_UniverseChannelAddressTester_LDADD = $(COMMON_TESTING_LIBS) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* 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 of | ||
* MERCHANTABILITY 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 | ||
* | ||
* UniverseChannelAddress.cpp | ||
* Represents a universe-channel address pair. | ||
* Copyright (C) 2023 Peter Newman | ||
*/ | ||
|
||
#include <assert.h> | ||
#include <ola/Logging.h> | ||
#include <ola/StringUtils.h> | ||
#include <ola/dmx/UniverseChannelAddress.h> | ||
#include <string.h> | ||
#include <string> | ||
|
||
namespace ola { | ||
namespace dmx { | ||
|
||
using std::string; | ||
|
||
|
||
string UniverseChannelAddress::ToString() const { | ||
std::ostringstream str; | ||
str << Universe() << ":" << Channel(); | ||
return str.str(); | ||
} | ||
|
||
|
||
/** | ||
* Extract a UniverseChannelAddress from a string. | ||
*/ | ||
bool UniverseChannelAddress::FromString( | ||
const string &input, | ||
UniverseChannelAddress *universe_channel_address) { | ||
size_t pos = input.find_first_of(":"); | ||
if (pos == string::npos) { | ||
return false; | ||
} | ||
|
||
unsigned int universe; | ||
if (!StringToInt(input.substr(0, pos), &universe)) { | ||
return false; | ||
} | ||
uint16_t channel; | ||
if (!StringToInt(input.substr(pos + 1), &channel)) { | ||
return false; | ||
} | ||
*universe_channel_address = UniverseChannelAddress(universe, channel); | ||
return true; | ||
} | ||
|
||
|
||
UniverseChannelAddress UniverseChannelAddress::FromStringOrDie( | ||
const string &address) { | ||
UniverseChannelAddress universe_channel_address; | ||
assert(FromString(address, &universe_channel_address)); | ||
return universe_channel_address; | ||
} | ||
|
||
|
||
string UniverseChannelAddressOneBased::ToStringZeroBased() const { | ||
std::ostringstream str; | ||
str << Universe() << ":" << ChannelZeroBased(); | ||
return str.str(); | ||
} | ||
|
||
|
||
/** | ||
* Extract a UniverseChannelAddressOneBased from a string. | ||
*/ | ||
bool UniverseChannelAddressOneBased::FromString( | ||
const string &input, | ||
UniverseChannelAddressOneBased *universe_channel_address_one_based) { | ||
size_t pos = input.find_first_of(":"); | ||
if (pos == string::npos) { | ||
return false; | ||
} | ||
|
||
unsigned int universe; | ||
if (!StringToInt(input.substr(0, pos), &universe)) { | ||
return false; | ||
} | ||
uint16_t channel; | ||
if (!StringToInt(input.substr(pos + 1), &channel)) { | ||
return false; | ||
} | ||
*universe_channel_address_one_based = UniverseChannelAddressOneBased( | ||
universe, | ||
channel); | ||
return true; | ||
} | ||
} // namespace dmx | ||
} // namespace ola |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* 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 of | ||
* MERCHANTABILITY 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 | ||
* | ||
* UniverseChannelAddressTest.cpp | ||
* Test fixture for the UniverseChannelAddress class | ||
* Copyright (C) 2023 Peter Newman | ||
*/ | ||
|
||
#include <cppunit/extensions/HelperMacros.h> | ||
#include <string> | ||
|
||
#include "ola/dmx/UniverseChannelAddress.h" | ||
#include "ola/testing/TestUtils.h" | ||
|
||
using ola::dmx::UniverseChannelAddress; | ||
using ola::dmx::UniverseChannelAddressOneBased; | ||
using std::ostringstream; | ||
using std::string; | ||
|
||
class UniverseChannelAddressTest: public CppUnit::TestFixture { | ||
CPPUNIT_TEST_SUITE(UniverseChannelAddressTest); | ||
CPPUNIT_TEST(testUniverseChannelAddress); | ||
CPPUNIT_TEST(testUniverseChannelAddressFromString); | ||
CPPUNIT_TEST_SUITE_END(); | ||
|
||
public: | ||
void testUniverseChannelAddress(); | ||
void testUniverseChannelAddressToString(); | ||
void testUniverseChannelAddressFromString(); | ||
}; | ||
|
||
CPPUNIT_TEST_SUITE_REGISTRATION(UniverseChannelAddressTest); | ||
|
||
|
||
/* | ||
* Test the UniverseChannelAddress class works | ||
*/ | ||
void UniverseChannelAddressTest::testUniverseChannelAddress() { | ||
UniverseChannelAddress universe_channel_address(10, 500); | ||
OLA_ASSERT_EQ(static_cast<unsigned int>(10), | ||
universe_channel_address.Universe()); | ||
OLA_ASSERT_EQ(static_cast<uint16_t>(500), | ||
universe_channel_address.Channel()); | ||
|
||
UniverseChannelAddressOneBased universe_channel_address_one_based(10, 501); | ||
OLA_ASSERT_EQ(static_cast<unsigned int>(10), | ||
universe_channel_address_one_based.Universe()); | ||
OLA_ASSERT_EQ(static_cast<uint16_t>(501), | ||
universe_channel_address_one_based.Channel()); | ||
OLA_ASSERT_EQ(static_cast<uint16_t>(500), | ||
universe_channel_address_one_based.ChannelZeroBased()); | ||
|
||
// TODO(Peter): Test out of range channel values | ||
|
||
// test comparison operators | ||
UniverseChannelAddress universe_channel_address2(10, 499); | ||
UniverseChannelAddress universe_channel_address3(10, 501); | ||
UniverseChannelAddress universe_channel_address4(9, 500); | ||
UniverseChannelAddress universe_channel_address5(11, 500); | ||
|
||
OLA_ASSERT_EQ(universe_channel_address, universe_channel_address); | ||
OLA_ASSERT_NE(universe_channel_address, universe_channel_address2); | ||
OLA_ASSERT_NE(universe_channel_address, universe_channel_address3); | ||
OLA_ASSERT_NE(universe_channel_address, universe_channel_address4); | ||
OLA_ASSERT_NE(universe_channel_address, universe_channel_address5); | ||
|
||
OLA_ASSERT_LT(universe_channel_address2, universe_channel_address); | ||
OLA_ASSERT_LT(universe_channel_address, universe_channel_address3); | ||
OLA_ASSERT_LT(universe_channel_address4, universe_channel_address); | ||
OLA_ASSERT_LT(universe_channel_address4, universe_channel_address3); | ||
|
||
OLA_ASSERT_GT(universe_channel_address, universe_channel_address2); | ||
OLA_ASSERT_GT(universe_channel_address3, universe_channel_address); | ||
OLA_ASSERT_GT(universe_channel_address, universe_channel_address4); | ||
OLA_ASSERT_GT(universe_channel_address3, universe_channel_address4); | ||
|
||
OLA_ASSERT_EQ(universe_channel_address, | ||
static_cast<UniverseChannelAddress>( | ||
universe_channel_address_one_based)); | ||
|
||
// test assignment & copy constructor | ||
UniverseChannelAddress copy_address(universe_channel_address); | ||
universe_channel_address4 = universe_channel_address; | ||
OLA_ASSERT_EQ(universe_channel_address, copy_address); | ||
OLA_ASSERT_EQ(universe_channel_address, universe_channel_address4); | ||
} | ||
|
||
/** | ||
* Test that ToString() works | ||
*/ | ||
void UniverseChannelAddressTest::testUniverseChannelAddressToString() { | ||
UniverseChannelAddress universe_channel_address(10, 500); | ||
|
||
OLA_ASSERT_EQ(string("10:500"), universe_channel_address.ToString()); | ||
|
||
universe_channel_address.Universe(100); | ||
universe_channel_address.Channel(50); | ||
OLA_ASSERT_EQ(string("100:50"), universe_channel_address.ToString()); | ||
|
||
ostringstream str; | ||
str << universe_channel_address; | ||
OLA_ASSERT_EQ(string("100:50"), str.str()); | ||
} | ||
|
||
/** | ||
* Test that FromString() works | ||
*/ | ||
void UniverseChannelAddressTest::testUniverseChannelAddressFromString() { | ||
UniverseChannelAddress universe_channel_address; | ||
OLA_ASSERT_TRUE( | ||
UniverseChannelAddress::FromString("127:80", &universe_channel_address)); | ||
OLA_ASSERT_EQ(static_cast<unsigned int>(127), | ||
universe_channel_address.Universe()); | ||
OLA_ASSERT_EQ(static_cast<uint16_t>(80), universe_channel_address.Channel()); | ||
|
||
UniverseChannelAddressOneBased universe_channel_address_one_based; | ||
OLA_ASSERT_TRUE(UniverseChannelAddressOneBased::FromString( | ||
"127:81", &universe_channel_address_one_based)); | ||
OLA_ASSERT_EQ(static_cast<unsigned int>(127), | ||
universe_channel_address_one_based.Universe()); | ||
OLA_ASSERT_EQ(static_cast<uint16_t>(81), | ||
universe_channel_address_one_based.Channel()); | ||
OLA_ASSERT_EQ(static_cast<uint16_t>(80), | ||
universe_channel_address_one_based.ChannelZeroBased()); | ||
|
||
OLA_ASSERT_FALSE( | ||
UniverseChannelAddress::FromString("127", &universe_channel_address)); | ||
OLA_ASSERT_FALSE( | ||
UniverseChannelAddress::FromString("foo", &universe_channel_address)); | ||
OLA_ASSERT_FALSE( | ||
UniverseChannelAddress::FromString("127:", &universe_channel_address)); | ||
OLA_ASSERT_FALSE( | ||
UniverseChannelAddress::FromString("foo:", &universe_channel_address)); | ||
OLA_ASSERT_FALSE( | ||
UniverseChannelAddress::FromString(":80", &universe_channel_address)); | ||
|
||
OLA_ASSERT_FALSE(UniverseChannelAddressOneBased::FromString( | ||
"127", &universe_channel_address_one_based)); | ||
OLA_ASSERT_FALSE(UniverseChannelAddressOneBased::FromString( | ||
"foo", &universe_channel_address_one_based)); | ||
OLA_ASSERT_FALSE(UniverseChannelAddressOneBased::FromString( | ||
"127:", &universe_channel_address_one_based)); | ||
OLA_ASSERT_FALSE(UniverseChannelAddressOneBased::FromString( | ||
"foo:", &universe_channel_address_one_based)); | ||
OLA_ASSERT_FALSE(UniverseChannelAddressOneBased::FromString( | ||
":80", &universe_channel_address_one_based)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.