1
0

LabelSplicer_test.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "switch/LabelSplicer.h"
  16. #include "util/Endian.h"
  17. #include "util/Assert.h"
  18. #include <stdio.h>
  19. #include <inttypes.h>
  20. static void unsplice()
  21. {
  22. Assert_always(0x13 == LabelSplicer_unsplice(0x13, 1));
  23. }
  24. static void splice()
  25. {
  26. // 000000100
  27. uint64_t goHere = 1<<2;
  28. // 000000100
  29. uint64_t viaHere = 1<<2;
  30. // 000010000
  31. uint64_t expected = 1<<4;
  32. uint64_t out = LabelSplicer_splice(goHere, viaHere);
  33. printf("Splicing %" PRIu64 " with %" PRIu64 " yields %" PRIu64 ", expecting %" PRIu64 "\n",
  34. goHere, viaHere, out, expected);
  35. Assert_always(expected == out);
  36. }
  37. static uint64_t routeToInterface(uint32_t number)
  38. {
  39. uint32_t bits = NumberCompress_bitsUsedForNumber(number);
  40. return (1 << bits) | NumberCompress_getCompressed(number, bits);
  41. }
  42. static void isOneHop()
  43. {
  44. Assert_always(LabelSplicer_isOneHop(routeToInterface(0)));
  45. }
  46. static void routesThrough()
  47. {
  48. // 0000000000000000100100000000101011101010100101011100101001010101
  49. uint64_t dest = 0x0000900aea95ca55llu;
  50. // 0000000000000000000000010110010100100110001110011100011001010101
  51. uint64_t mid = 0x000001652639c655llu;
  52. Assert_always(!LabelSplicer_routesThrough(dest, mid));
  53. Assert_always(LabelSplicer_routesThrough(dest, 1));
  54. }
  55. int main()
  56. {
  57. splice();
  58. isOneHop();
  59. routesThrough();
  60. unsplice();
  61. return 0;
  62. }