Skip to content

Commit

Permalink
Add a new function EncodingScheme_isOneHop() because NumberCompress_i…
Browse files Browse the repository at this point in the history
…sOneHop() only works for *our* scheme, not for anyone elses
  • Loading branch information
cjdelisle committed Apr 11, 2015
1 parent b99be9a commit 5819c1c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions switch/EncodingScheme.c
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions switch/EncodingScheme.h
Expand Up @@ -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
25 changes: 25 additions & 0 deletions switch/test/EncodingScheme_test.c
Expand Up @@ -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);
Expand Down

0 comments on commit 5819c1c

Please sign in to comment.