diff --git a/cpp/Platform.Data.Doublets/Memory/Split/Generic/SplitMemoryLinksBase.h b/cpp/Platform.Data.Doublets/Memory/Split/Generic/SplitMemoryLinksBase.h index c180989c5..dda941aeb 100644 --- a/cpp/Platform.Data.Doublets/Memory/Split/Generic/SplitMemoryLinksBase.h +++ b/cpp/Platform.Data.Doublets/Memory/Split/Generic/SplitMemoryLinksBase.h @@ -315,18 +315,19 @@ namespace Platform::Data::Doublets::Memory::Split::Generic } return LinkAddressType{0}; } - auto value = LinkAddressType{}; if ((source == any)) { - value = target; + if ((storedLinkValue.Target == target)) + { + return LinkAddressType{1}; + } } if ((target == any)) { - value = source; - } - if ((storedLinkValue.Source == value) || (storedLinkValue.Target == value)) - { - return LinkAddressType{1}; + if ((storedLinkValue.Source == source)) + { + return LinkAddressType{1}; + } } return LinkAddressType{0}; } @@ -528,18 +529,19 @@ namespace Platform::Data::Doublets::Memory::Split::Generic } return LinkAddressType{0}; } - auto value = LinkAddressType{0}; if ((source == any)) { - value = target; + if ((storedLinkValue.Target == target)) + { + return LinkAddressType{1}; + } } if ((target == any)) { - value = source; - } - if ((storedLinkValue.Source == value) || (storedLinkValue.Target == value)) - { - return LinkAddressType{1}; + if ((storedLinkValue.Source == source)) + { + return LinkAddressType{1}; + } } return LinkAddressType{0}; } @@ -724,18 +726,19 @@ namespace Platform::Data::Doublets::Memory::Split::Generic } return $continue; } - auto value = LinkAddressType{0}; if (source == any) { - value = target; + if ((storedLinkValue.Target == target)) + { + return handler(GetLinkStruct(index)); + } } if (target == any) { - value = source; - } - if ((storedLinkValue.Source == value) || (storedLinkValue.Target == value)) - { - return handler(GetLinkStruct(index)); + if ((storedLinkValue.Source == source)) + { + return handler(GetLinkStruct(index)); + } } return $continue; } diff --git a/csharp/Platform.Data.Doublets/Memory/Split/Generic/SplitMemoryLinksBase.cs b/csharp/Platform.Data.Doublets/Memory/Split/Generic/SplitMemoryLinksBase.cs index 9ab5ade78..e5603c8ad 100644 --- a/csharp/Platform.Data.Doublets/Memory/Split/Generic/SplitMemoryLinksBase.cs +++ b/csharp/Platform.Data.Doublets/Memory/Split/Generic/SplitMemoryLinksBase.cs @@ -390,18 +390,19 @@ public virtual TLinkAddress Count(IList? restriction) } return GetZero(); } - var value = default(TLinkAddress); if ((source == any)) { - value = target; + if ((storedLinkValue.Target == target)) + { + return GetOne(); + } } if ((target == any)) { - value = source; - } - if ((storedLinkValue.Source == value) || (storedLinkValue.Target == value)) - { - return GetOne(); + if ((storedLinkValue.Source == source)) + { + return GetOne(); + } } return GetZero(); } diff --git a/examples/count_logic_test.cs b/examples/count_logic_test.cs new file mode 100644 index 000000000..dc0b6d0ff --- /dev/null +++ b/examples/count_logic_test.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; + +namespace Platform.Data.Doublets.Examples +{ + /// + /// Test case to verify the Count logic fix for issue #359 + /// This demonstrates the corrected behavior where source==any or target==any + /// should be handled separately rather than with variable overwriting + /// + public class CountLogicTest + { + public class MockStoredLinkValue + { + public ulong Source { get; set; } + public ulong Target { get; set; } + } + + private const ulong any = 0; // Assuming 0 represents "any" + + /// + /// Old (buggy) logic that had the issue + /// + public static bool OldBuggyLogic(MockStoredLinkValue storedLinkValue, ulong source, ulong target) + { + var value = default(ulong); + if (source == any) + { + value = target; + } + if (target == any) // This overwrites value even if source was any! + { + value = source; + } + return (storedLinkValue.Source == value) || (storedLinkValue.Target == value); + } + + /// + /// New (fixed) logic that handles each case separately + /// + public static bool NewFixedLogic(MockStoredLinkValue storedLinkValue, ulong source, ulong target) + { + if (source == any) + { + if (storedLinkValue.Target == target) + { + return true; + } + } + if (target == any) + { + if (storedLinkValue.Source == source) + { + return true; + } + } + return false; + } + + public static void RunTests() + { + var testLink = new MockStoredLinkValue { Source = 1, Target = 2 }; + + // Test case 1: source=any, target=2 (should match target) + Console.WriteLine("Test 1: source=any, target=2, stored=(1,2)"); + Console.WriteLine($"Old logic: {OldBuggyLogic(testLink, any, 2)}"); + Console.WriteLine($"New logic: {NewFixedLogic(testLink, any, 2)}"); + Console.WriteLine(); + + // Test case 2: source=1, target=any (should match source) + Console.WriteLine("Test 2: source=1, target=any, stored=(1,2)"); + Console.WriteLine($"Old logic: {OldBuggyLogic(testLink, 1, any)}"); + Console.WriteLine($"New logic: {NewFixedLogic(testLink, 1, any)}"); + Console.WriteLine(); + + // Test case 3: source=any, target=any (edge case, neither should match in this context) + Console.WriteLine("Test 3: source=any, target=any, stored=(1,2)"); + Console.WriteLine($"Old logic: {OldBuggyLogic(testLink, any, any)}"); + Console.WriteLine($"New logic: {NewFixedLogic(testLink, any, any)}"); + Console.WriteLine(); + + // Test case 4: source=1, target=3 (no match expected) + Console.WriteLine("Test 4: source=1, target=3, stored=(1,2)"); + Console.WriteLine($"Old logic: {OldBuggyLogic(testLink, 1, 3)}"); + Console.WriteLine($"New logic: {NewFixedLogic(testLink, 1, 3)}"); + } + } +} \ No newline at end of file