LabelSplicer_test.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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/Constant.h"
  18. #include "util/Assert.h"
  19. #include <stdio.h>
  20. #include <inttypes.h>
  21. static void unsplice()
  22. {
  23. Assert_true(0x13 == LabelSplicer_unsplice(0x13, 1));
  24. }
  25. static void splice()
  26. {
  27. uint64_t goHere = Constant_base2(000000100);
  28. uint64_t viaHere = Constant_base2(000000100);
  29. uint64_t expected = Constant_base2(000010000);
  30. uint64_t out = LabelSplicer_splice(goHere, viaHere);
  31. printf("Splicing %" PRIu64 " with %" PRIu64 " yields %" PRIu64 ", expecting %" PRIu64 "\n",
  32. goHere, viaHere, out, expected);
  33. Assert_true(expected == out);
  34. }
  35. static uint64_t routeToInterface(uint32_t number)
  36. {
  37. uint32_t bits = NumberCompress_bitsUsedForNumber(number);
  38. return (1 << bits) | NumberCompress_getCompressed(number, bits);
  39. }
  40. static void isOneHop()
  41. {
  42. Assert_true(LabelSplicer_isOneHop(routeToInterface(0)));
  43. }
  44. static void routesThrough()
  45. {
  46. uint64_t dst = Constant_base2(0000000000000000100100000000101011101010100101011100101001010101);
  47. uint64_t mid = Constant_base2(0000000000000000000000010110010100100110001110011100011001010101);
  48. Assert_true(!LabelSplicer_routesThrough(dst, mid));
  49. Assert_true(LabelSplicer_routesThrough(dst, 1));
  50. }
  51. int main()
  52. {
  53. splice();
  54. isOneHop();
  55. routesThrough();
  56. unsplice();
  57. return 0;
  58. }