Browse Source

Add a new function EncodingScheme_isOneHop() because NumberCompress_isOneHop() only works for *our* scheme, not for anyone elses

Caleb James DeLisle 9 years ago
parent
commit
5819c1cdbd
3 changed files with 40 additions and 0 deletions
  1. 10 0
      switch/EncodingScheme.c
  2. 5 0
      switch/EncodingScheme.h
  3. 25 0
      switch/test/EncodingScheme_test.c

+ 10 - 0
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;
+}

+ 5 - 0
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

+ 25 - 0
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);