tst-makedev.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* Tests of functions to access `dev_t' values.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <sys/types.h>
  16. #include <sys/sysmacros.h>
  17. #include <stdio.h>
  18. #include <inttypes.h>
  19. /* Confirm that makedev (major (d), minor (d)) == d. */
  20. static int
  21. do_test_split_combine (dev_t d1)
  22. {
  23. unsigned int maj = major (d1);
  24. unsigned int min = minor (d1);
  25. dev_t d2 = makedev (maj, min);
  26. if (d1 != d2)
  27. {
  28. printf ("FAIL: %016" PRIx64 " != %016" PRIx64 " (maj %08x min %08x)\n",
  29. (uint64_t)d2, (uint64_t)d1, maj, min);
  30. return 1;
  31. }
  32. else
  33. return 0;
  34. }
  35. /* Confirm that major (makedev (maj, min)) == maj and
  36. minor (makedev (maj, min)) == min. */
  37. static int
  38. do_test_combine_split (unsigned int maj1, unsigned int min1)
  39. {
  40. dev_t d = makedev (maj1, min1);
  41. unsigned int maj2 = major (d);
  42. unsigned int min2 = minor (d);
  43. if (maj1 != maj2 && min1 != min2)
  44. {
  45. printf ("FAIL: %08x != %08x, %08x != %08x (dev %016" PRIx64 ")\n",
  46. maj2, maj1, min2, min1, (uint64_t)d);
  47. return 1;
  48. }
  49. else if (maj1 != maj2)
  50. {
  51. printf ("FAIL: %08x != %08x, %08x == %08x (dev %016" PRIx64 ")\n",
  52. maj2, maj1, min2, min1, (uint64_t)d);
  53. return 1;
  54. }
  55. else if (min1 != min2)
  56. {
  57. printf ("FAIL: %08x == %08x, %08x != %08x (dev %016" PRIx64 ")\n",
  58. maj2, maj1, min2, min1, (uint64_t)d);
  59. return 1;
  60. }
  61. else
  62. return 0;
  63. }
  64. static int
  65. do_test (void)
  66. {
  67. dev_t d;
  68. unsigned int maj, min;
  69. int status = 0;
  70. /* Test the traditional range (16-bit dev_t, 8-bit each maj/min)
  71. exhaustively. */
  72. for (d = 0; d <= 0xFFFF; d++)
  73. status |= do_test_split_combine (d);
  74. for (maj = 0; maj <= 0xFF; maj++)
  75. for (min = 0; min <= 0xFF; min++)
  76. status |= do_test_combine_split (maj, min);
  77. /* Test glibc's expanded range (64-bit dev_t, 32-bit each maj/min).
  78. Exhaustive testing would take much too long, instead we shift a
  79. pair of 1-bits over each range. */
  80. {
  81. unsigned int a, b;
  82. for (a = 0; a <= 63; a++)
  83. do_test_split_combine (((dev_t) 0x03) << a);
  84. for (a = 0; a < 31; a++)
  85. for (b = 0; b <= 31; b++)
  86. do_test_combine_split (0x03u << a, 0x03u << b);
  87. }
  88. return status;
  89. }
  90. #define TEST_FUNCTION do_test ()
  91. #include "../test-skeleton.c"