From 5819c1cdbd1c0eb2f44fd3128e291f0f4897faf6 Mon Sep 17 00:00:00 2001 From: Caleb James DeLisle Date: Sat, 11 Apr 2015 16:18:28 +0200 Subject: [PATCH] Add a new function EncodingScheme_isOneHop() because NumberCompress_isOneHop() only works for *our* scheme, not for anyone elses --- switch/EncodingScheme.c | 10 ++++++++++ switch/EncodingScheme.h | 5 +++++ switch/test/EncodingScheme_test.c | 25 +++++++++++++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/switch/EncodingScheme.c b/switch/EncodingScheme.c index 48892dbee..45d4d483a 100644 --- a/switch/EncodingScheme.c +++ b/switch/EncodingScheme.c @@ -408,3 +408,13 @@ int EncodingScheme_isSelfRoute(struct EncodingScheme* scheme, uint64_t routeLabe return (routeLabel & Bits_maxBits64(currentForm->prefixLen + currentForm->bitCount)) == 1; } + +int EncodingScheme_isOneHop(struct EncodingScheme* scheme, uint64_t routeLabel) +{ + int fn = EncodingScheme_getFormNum(scheme, routeLabel); + if (fn == EncodingScheme_getFormNum_INVALID) { return 0; } + struct EncodingScheme_Form* form = &scheme->forms[fn]; + if (Bits_log2x64(routeLabel) == form->prefixLen + form->bitCount) { return true; } + if ((routeLabel & Bits_maxBits64(form->prefixLen + form->bitCount)) == 1) { return true; } + return false; +} diff --git a/switch/EncodingScheme.h b/switch/EncodingScheme.h index 5fb2db8da..285e914e9 100644 --- a/switch/EncodingScheme.h +++ b/switch/EncodingScheme.h @@ -104,4 +104,9 @@ List* EncodingScheme_asList(struct EncodingScheme* list, struct Allocator* alloc */ int EncodingScheme_isSelfRoute(struct EncodingScheme* scheme, uint64_t routeLabel); +/** + * @return non-zero if the route label is one hop. + */ +int EncodingScheme_isOneHop(struct EncodingScheme* scheme, uint64_t routeLabel); + #endif diff --git a/switch/test/EncodingScheme_test.c b/switch/test/EncodingScheme_test.c index 602351fc3..4071197db 100644 --- a/switch/test/EncodingScheme_test.c +++ b/switch/test/EncodingScheme_test.c @@ -269,12 +269,37 @@ static void convertLabelRand(struct Random* rand, struct EncodingScheme* scheme) } } +static void isOneHopScheme(struct Allocator* allocator) +{ + struct Allocator* alloc = Allocator_child(allocator); + struct EncodingScheme* s4x8 = NumberCompress_v4x8_defineScheme(alloc); + Assert_true(EncodingScheme_isOneHop(s4x8, 1)); + Assert_true(EncodingScheme_isOneHop(s4x8, 0x21)); + Assert_true(EncodingScheme_isOneHop(s4x8, 0x23)); + Assert_true(!EncodingScheme_isOneHop(s4x8, 0x12)); + Assert_true(EncodingScheme_isOneHop(s4x8, 0x220)); + Assert_true(EncodingScheme_isOneHop(s4x8, 0x210)); + Assert_true(!EncodingScheme_isOneHop(s4x8, 0x110)); + + struct EncodingScheme* s3x5x8 = NumberCompress_v3x5x8_defineScheme(alloc); + Assert_true(EncodingScheme_isOneHop(s3x5x8, 1)); + Assert_true(EncodingScheme_isOneHop(s3x5x8, 0x13)); + Assert_true(EncodingScheme_isOneHop(s3x5x8, 0x15)); + Assert_true(EncodingScheme_isOneHop(s3x5x8, 0x96)); + Assert_true(EncodingScheme_isOneHop(s3x5x8, 0x400)); + Assert_true(!EncodingScheme_isOneHop(s3x5x8, 0x115)); + Assert_true(!EncodingScheme_isOneHop(s3x5x8, 0x166)); + Assert_true(!EncodingScheme_isOneHop(s3x5x8, 0x1400)); + Allocator_free(alloc); +} + int main() { struct Allocator* alloc = MallocAllocator_new(20000000); struct Random* rand = Random_new(alloc, NULL, NULL); encoding(alloc); + isOneHopScheme(alloc); for (int i = 0; i < 1000; i++) { randomTest(alloc, rand);