vfs_adjust_stat.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. lib/vfs - test vfs_adjust_stat() functionality
  3. Copyright (C) 2017-2025
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2017
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "/lib/vfs"
  20. #include "tests/mctest.h"
  21. #include <sys/stat.h>
  22. /* --------------------------------------------------------------------------------------------- */
  23. #if defined(HAVE_STRUCT_STAT_ST_BLKSIZE) && defined(HAVE_STRUCT_STAT_ST_BLOCKS)
  24. # define STRUCT_STAT(size, blksize, blocks) \
  25. { \
  26. .st_size = size, \
  27. .st_blksize = blksize, \
  28. .st_blocks = blocks, \
  29. }
  30. #elif defined(HAVE_STRUCT_STAT_ST_BLKSIZE)
  31. # define STRUCT_STAT(st_blksize, st_blocks) \
  32. { \
  33. .st_size = size, \
  34. .st_blksize = st_blksize, \
  35. }
  36. #elif defined(HAVE_STRUCT_STAT_ST_BLOCKS)
  37. # define STRUCT_STAT(st_blksize, st_blocks) \
  38. { \
  39. .st_size = size, \
  40. .st_blocks = st_blocks, \
  41. }
  42. #else
  43. # define STRUCT_STAT(st_blksize, st_blocks) \
  44. { \
  45. .st_size = size, \
  46. }
  47. #endif
  48. /* @DataSource("test_test_vfs_adjust_stat_ds") */
  49. static const struct test_vfs_adjust_stat_ds
  50. {
  51. struct stat etalon_stat;
  52. } test_vfs_adjust_stat_ds[] = {
  53. { .etalon_stat = STRUCT_STAT (0, 512, 0) }, // 0
  54. { .etalon_stat = STRUCT_STAT (4096, 512, 8) }, // 1
  55. { .etalon_stat = STRUCT_STAT (4096, 1024, 8) }, // 2
  56. { .etalon_stat = STRUCT_STAT (4096, 2048, 8) }, // 3
  57. { .etalon_stat = STRUCT_STAT (4096, 4096, 8) }, // 4
  58. { .etalon_stat = STRUCT_STAT (5000, 512, 10) }, // 5
  59. { .etalon_stat = STRUCT_STAT (5000, 1024, 10) }, // 6
  60. { .etalon_stat = STRUCT_STAT (5000, 2048, 12) }, // 7
  61. { .etalon_stat = STRUCT_STAT (5000, 4096, 16) }, // 8
  62. };
  63. #undef STRUCT_STAT
  64. /* --------------------------------------------------------------------------------------------- */
  65. /* @Test(dataSource = "test_vfs_adjust_stat_ds") */
  66. START_PARAMETRIZED_TEST (test_vfs_adjust_stat, test_vfs_adjust_stat_ds)
  67. {
  68. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  69. // given
  70. struct stat expected_stat;
  71. expected_stat.st_size = data->etalon_stat.st_size;
  72. # ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  73. expected_stat.st_blksize = data->etalon_stat.st_blksize;
  74. # endif
  75. // when
  76. vfs_adjust_stat (&expected_stat);
  77. // then
  78. ck_assert_int_eq (data->etalon_stat.st_blocks, expected_stat.st_blocks);
  79. #else
  80. ck_assert_int_eq (0, 0);
  81. #endif
  82. }
  83. END_PARAMETRIZED_TEST
  84. /* --------------------------------------------------------------------------------------------- */
  85. int
  86. main (void)
  87. {
  88. TCase *tc_core;
  89. tc_core = tcase_create ("Core");
  90. // Add new tests here: ***************
  91. mctest_add_parameterized_test (tc_core, test_vfs_adjust_stat, test_vfs_adjust_stat_ds);
  92. // ***********************************
  93. return mctest_run_all (tc_core);
  94. }
  95. /* --------------------------------------------------------------------------------------------- */