test_llong.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* This file is part of asmc, a bootstrapping OS with minimal seed
  2. Copyright (C) 2018-2019 Giovanni Mascellani <gio@debian.org>
  3. https://gitlab.com/giomasce/asmc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. long long square(long long x) {
  15. return x*x;
  16. }
  17. int test_llong() {
  18. long long x = 10;
  19. int y = x;
  20. return y == 10;
  21. }
  22. int test_llong_sum() {
  23. long long x = 10;
  24. long long y = 20;
  25. long long z = x + y;
  26. return z == 30;
  27. }
  28. int test_llong_ops() {
  29. unsigned long long x = 4000000000;
  30. unsigned long long y = 4000000000;
  31. unsigned long long z = x + y;
  32. unsigned long long w = 2 * x;
  33. if (z != w) return 0;
  34. long long sq = square(2);
  35. if (sq != 4) return 0;
  36. sq = square(2000000000);
  37. if (sq != 4000000000000000000ULL) return 0;
  38. if ((unsigned long long) sq == 2643460096) return 0;
  39. if ((unsigned) sq != 2643460096) return 0;
  40. return 1;
  41. }
  42. int test_one_udiv(unsigned long long x, unsigned long long y) {
  43. unsigned long long d = x / y;
  44. unsigned long long m = x % y;
  45. if (x != y * d + m) return 0;
  46. if (m >= y) return 0;
  47. return 1;
  48. }
  49. int test_one_sdiv(long long x, long long y) {
  50. long long d = x / y;
  51. long long m = x % y;
  52. long long y_abs = y >= 0 ? y : -y;
  53. if (x != y * d + m) return 0;
  54. if (m >= y_abs) return 0;
  55. if (m <= -y_abs) return 0;
  56. return 1;
  57. }
  58. int test_llong_mul_div() {
  59. if (!test_one_udiv(23472873523784, 23946287)) return 0;
  60. if (!test_one_udiv(23472873523784, 239)) return 0;
  61. if (!test_one_udiv(23472873523784, 23927428734572354)) return 0;
  62. if (!test_one_sdiv(23472873523784, 23946287)) return 0;
  63. if (!test_one_sdiv(23472873523784, 239)) return 0;
  64. if (!test_one_sdiv(23472873523784, 23927428734572354)) return 0;
  65. if (!test_one_sdiv(-23472873523784, 23946287)) return 0;
  66. if (!test_one_sdiv(-23472873523784, 239)) return 0;
  67. if (!test_one_sdiv(-23472873523784, 23927428734572354)) return 0;
  68. if (!test_one_sdiv(23472873523784, -23946287)) return 0;
  69. if (!test_one_sdiv(23472873523784, -239)) return 0;
  70. if (!test_one_sdiv(23472873523784, -23927428734572354)) return 0;
  71. if (!test_one_sdiv(-23472873523784, -23946287)) return 0;
  72. if (!test_one_sdiv(-23472873523784, -239)) return 0;
  73. if (!test_one_sdiv(-23472873523784, -23927428734572354)) return 0;
  74. return 1;
  75. }