* Copyright (c) 2025-2026 IB-Robot Group & openEuler Embedded SIG & openharmony-robot sig_RoboFrame.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <set>
#include <string>
#include "c/driver_slot.h"
namespace ibmw {
namespace {
struct StubDdsDriver {
static constexpr const char* kStableDriverId = "ibmw.transport.dds";
};
struct StubIceoryxDriver {
static constexpr const char* kStableDriverId = "ibmw.transport.iceoryx";
};
struct StubRos2Driver {
static constexpr const char* kStableDriverId = "ibmw.transport.ros2";
};
struct StubMqttDriver {
static constexpr const char* kStableDriverId = "ibmw.transport.mqtt";
};
struct StubNetDriver {
static constexpr const char* kStableDriverId = "ibmw.transport.net";
};
struct StubZenohDriver {
static constexpr const char* kStableDriverId = "ibmw.transport.zenoh";
};
TEST(DriverSlotFromString, NonZeroForNonEmpty)
{
EXPECT_NE(DriverSlotFromString("ibmw.transport.dds"), IBMW_DRIVER_SLOT_INVALID);
EXPECT_NE(DriverSlotFromString("a"), IBMW_DRIVER_SLOT_INVALID);
}
TEST(DriverSlotFromString, EmptyStringYieldsOffsetBasis)
{
constexpr ibmw_driver_slot_t kFnvOffsetBasis = 0xcbf29ce484222325ULL;
EXPECT_EQ(DriverSlotFromString(""), kFnvOffsetBasis);
}
TEST(DriverSlotFromString, IsConstexpr)
{
constexpr ibmw_driver_slot_t kDds = DriverSlotFromString("ibmw.transport.dds");
static_assert(kDds != 0, "FNV-1a of non-empty should be non-zero");
EXPECT_EQ(kDds, DriverSlotFromString("ibmw.transport.dds"));
}
TEST(DriverSlotFromString, DistinctStringsDistinctSlots)
{
std::set<ibmw_driver_slot_t> slots;
const char* kIds[] = {
"ibmw.transport.dds",
"ibmw.transport.iceoryx",
"ibmw.transport.ros2",
"ibmw.transport.mqtt",
"ibmw.transport.net",
"ibmw.transport.zenoh",
"ibmw.transport.dds.",
"ibmw.transport.DDS",
"ibmw.transport.iceory",
"",
};
for (const char* s : kIds) {
slots.insert(DriverSlotFromString(s));
}
EXPECT_EQ(slots.size(), sizeof(kIds) / sizeof(kIds[0]));
}
TEST(DriverSlotFromString, SameInputSameOutput)
{
for (int i = 0; i < 100; ++i) {
EXPECT_EQ(DriverSlotFromString("ibmw.transport.dds"), DriverSlotFromString("ibmw.transport.dds"));
}
}
TEST(IbmwDriverIdMacro, ResolvesAtCompileTime)
{
constexpr ibmw_driver_slot_t kDds = IBMW_DRIVER_ID(StubDdsDriver);
constexpr ibmw_driver_slot_t kIox = IBMW_DRIVER_ID(StubIceoryxDriver);
static_assert(kDds != kIox, "stub driver ids must differ");
EXPECT_NE(kDds, kIox);
EXPECT_EQ(kDds, DriverSlotFromString("ibmw.transport.dds"));
EXPECT_EQ(kIox, DriverSlotFromString("ibmw.transport.iceoryx"));
}
TEST(IbmwDriverIdMacro, AllKnownStableIdsNoCollision)
{
std::set<ibmw_driver_slot_t> slots = {
IBMW_DRIVER_ID(StubDdsDriver), IBMW_DRIVER_ID(StubIceoryxDriver), IBMW_DRIVER_ID(StubRos2Driver),
IBMW_DRIVER_ID(StubMqttDriver), IBMW_DRIVER_ID(StubNetDriver), IBMW_DRIVER_ID(StubZenohDriver),
};
EXPECT_EQ(slots.size(), 6u) << "FNV-1a 64-bit collision among 6 known "
"transport driver stable ids";
EXPECT_EQ(slots.count(IBMW_DRIVER_SLOT_INVALID), 0u);
}
struct MultiDriverModuleStub {
StubDdsDriver* ddsDrv = nullptr;
StubIceoryxDriver* ioxDrv = nullptr;
void Dispatch(ibmw_driver_slot_t slotId, void* driverPtr)
{
bool results[] = {
([this, &slotId, &driverPtr]() -> bool {
if (slotId == IBMW_DRIVER_ID(StubDdsDriver)) {
ddsDrv = static_cast<StubDdsDriver*>(driverPtr);
return true;
}
return false;
}()),
([this, &slotId, &driverPtr]() -> bool {
if (slotId == IBMW_DRIVER_ID(StubIceoryxDriver)) {
ioxDrv = static_cast<StubIceoryxDriver*>(driverPtr);
return true;
}
return false;
}()),
};
(void)results;
}
};
TEST(MultiDriverDispatch, FirstSlotMatches)
{
MultiDriverModuleStub mod;
StubDdsDriver dds;
mod.Dispatch(IBMW_DRIVER_ID(StubDdsDriver), &dds);
EXPECT_EQ(mod.ddsDrv, &dds);
EXPECT_EQ(mod.ioxDrv, nullptr);
}
TEST(MultiDriverDispatch, SecondSlotMatches)
{
MultiDriverModuleStub mod;
StubIceoryxDriver iox;
mod.Dispatch(IBMW_DRIVER_ID(StubIceoryxDriver), &iox);
EXPECT_EQ(mod.ioxDrv, &iox);
EXPECT_EQ(mod.ddsDrv, nullptr);
}
TEST(MultiDriverDispatch, NoMatchLeavesPointersUnchanged)
{
MultiDriverModuleStub mod;
StubMqttDriver unused;
mod.Dispatch(IBMW_DRIVER_ID(StubMqttDriver), &unused);
EXPECT_EQ(mod.ddsDrv, nullptr);
EXPECT_EQ(mod.ioxDrv, nullptr);
}
TEST(MultiDriverDispatch, BothSlotsAcrossTwoCallsIndependent)
{
MultiDriverModuleStub mod;
StubDdsDriver dds;
StubIceoryxDriver iox;
mod.Dispatch(IBMW_DRIVER_ID(StubDdsDriver), &dds);
mod.Dispatch(IBMW_DRIVER_ID(StubIceoryxDriver), &iox);
EXPECT_EQ(mod.ddsDrv, &dds);
EXPECT_EQ(mod.ioxDrv, &iox);
}
}
}