utility_tests.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. *
  5. * Tests for utility functions
  6. */
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "test_common.h"
  12. #include "utility.h"
  13. #include "vboot_common.h"
  14. /* Test utility.h and sysincludes.h macros */
  15. static void MacrosTest(void)
  16. {
  17. int64_t a = -10, b = -20;
  18. uint64_t u = (0xABCD00000000ULL);
  19. uint64_t v = (0xABCD000000ULL);
  20. TEST_EQ(CombineUint16Pair(1, 2), 0x00010002, "CombineUint16Pair");
  21. TEST_EQ(CombineUint16Pair(0xFFFE, 0xFFFF), 0xFFFEFFFF,
  22. "CombineUint16Pair 2");
  23. TEST_EQ(CombineUint16Pair(-4, -16), 0xFFFCFFF0,
  24. "CombineUint16Pair big negative");
  25. TEST_EQ(CombineUint16Pair(0x10003, 0x10004), 0x00030004,
  26. "CombineUint16Pair overflow");
  27. TEST_EQ(Min(1, 2), 1, "Min 1");
  28. TEST_EQ(Min(4, 3), 3, "Min 2");
  29. TEST_EQ(Min(5, 5), 5, "Min 5");
  30. TEST_EQ(Min(a, b), b, "Min uint64 1");
  31. TEST_EQ(Min(b, a), b, "Min uint64 2");
  32. TEST_EQ(Min(b, b), b, "Min uint64 same");
  33. TEST_EQ(u >> 8, v, "uint64_t >> 8");
  34. TEST_EQ(u >> 0, u, "uint64_t >> 0");
  35. TEST_EQ(u >> 36, (uint64_t)0xABC, "uint64_t >> 36");
  36. TEST_EQ(v * (uint32_t)0, 0, "uint64_t * uint32_t 0");
  37. TEST_EQ(v * (uint32_t)1, v, "uint64_t * uint32_t 1");
  38. TEST_EQ(v * (uint32_t)256, u, "uint64_t * uint32_t 256");
  39. }
  40. int main(int argc, char* argv[])
  41. {
  42. int error_code = 0;
  43. MacrosTest();
  44. if (!gTestSuccess)
  45. error_code = 255;
  46. return error_code;
  47. }