From a0a19bcfd9ecfa1a441be87648ba5acd569f142b Mon Sep 17 00:00:00 2001 From: Dave Hickin Date: Fri, 4 Sep 2015 09:39:27 +0100 Subject: [PATCH] Fix bug in NTUtils::is_a Previous implementation matches :1x.0 and :1.0, depending on the order of the arguments, e.g. NTUtils::is_a("epics:nt/NTTable:11.0", "epics:nt/NTTable:1.0") returned true. Fix this and add unit tests to cover this. --- src/nt/ntutils.cpp | 13 ++++++++----- test/nt/ntutilsTest.cpp | 4 +++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/nt/ntutils.cpp b/src/nt/ntutils.cpp index 5e244af..858163a 100644 --- a/src/nt/ntutils.cpp +++ b/src/nt/ntutils.cpp @@ -14,12 +14,15 @@ namespace epics { namespace nt { bool NTUtils::is_a(const std::string &u1, const std::string &u2) { - // remove minor for the u2 - size_t pos = u2.find_last_of('.'); - std::string su2 = (pos == string::npos) ? u2 : u2.substr(0, pos); + // remove minor for the u1 + size_t pos1 = u1.find_last_of('.'); + std::string su1 = (pos1 == string::npos) ? u1 : u1.substr(0, pos1); - // "starts with comparison" - return su2.size() <= u1.size() && u1.compare(0, su2.size(), su2) == 0; + // remove minor for the u2 + size_t pos2 = u2.find_last_of('.'); + std::string su2 = (pos2 == string::npos) ? u2 : u2.substr(0, pos2); + + return su2 == su1; } }} diff --git a/test/nt/ntutilsTest.cpp b/test/nt/ntutilsTest.cpp index cc57ddc..30ef656 100644 --- a/test/nt/ntutilsTest.cpp +++ b/test/nt/ntutilsTest.cpp @@ -24,11 +24,13 @@ void test_is_a() testOk1(!NTUtils::is_a("epics:nt/NTTable:1.0", "epics:nt/NTTable:2.0")); testOk1(!NTUtils::is_a("epics:nt/NTTable:2.0", "epics:nt/NTTable:1.0")); testOk1(!NTUtils::is_a("epics:nt/NTTable:1.3", "epics:nt/NTTable:2.3")); + testOk1(!NTUtils::is_a("epics:nt/NTTable:1.0", "epics:nt/NTTable:11.0")); + testOk1(!NTUtils::is_a("epics:nt/NTTable:11.0", "epics:nt/NTTable:1.0")); testOk1(!NTUtils::is_a("epics:nt/NTTable:1.0", "epics:nt/NTMatrix:1.0")); } MAIN(testNTUtils) { - testPlan(8); + testPlan(10); test_is_a(); return testDone(); }