test_truncate.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/bin/bash
  2. set -eu
  3. SMALLSIZE=32
  4. MEDIUMSIZE=2048
  5. LARGESIZE=8192
  6. echo "=== Truncate tests ==="
  7. rm -rf blocks
  8. tests/test.py << TEST
  9. lfs_format(&lfs, &cfg) => 0;
  10. TEST
  11. truncate_test() {
  12. STARTSIZES="$1"
  13. HOTSIZES="$2"
  14. COLDSIZES="$3"
  15. tests/test.py << TEST
  16. static const lfs_off_t startsizes[] = {$STARTSIZES};
  17. static const lfs_off_t hotsizes[] = {$HOTSIZES};
  18. lfs_mount(&lfs, &cfg) => 0;
  19. for (int i = 0; i < sizeof(startsizes)/sizeof(startsizes[0]); i++) {
  20. sprintf((char*)buffer, "hairyhead%d", i);
  21. lfs_file_open(&lfs, &file[0], (const char*)buffer,
  22. LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
  23. strcpy((char*)buffer, "hair");
  24. size = strlen((char*)buffer);
  25. for (int j = 0; j < startsizes[i]; j += size) {
  26. lfs_file_write(&lfs, &file[0], buffer, size) => size;
  27. }
  28. lfs_file_size(&lfs, &file[0]) => startsizes[i];
  29. lfs_file_truncate(&lfs, &file[0], hotsizes[i]) => 0;
  30. lfs_file_size(&lfs, &file[0]) => hotsizes[i];
  31. lfs_file_close(&lfs, &file[0]) => 0;
  32. }
  33. lfs_unmount(&lfs) => 0;
  34. TEST
  35. tests/test.py << TEST
  36. static const lfs_off_t startsizes[] = {$STARTSIZES};
  37. static const lfs_off_t hotsizes[] = {$HOTSIZES};
  38. static const lfs_off_t coldsizes[] = {$COLDSIZES};
  39. lfs_mount(&lfs, &cfg) => 0;
  40. for (int i = 0; i < sizeof(startsizes)/sizeof(startsizes[0]); i++) {
  41. sprintf((char*)buffer, "hairyhead%d", i);
  42. lfs_file_open(&lfs, &file[0], (const char*)buffer, LFS_O_RDWR) => 0;
  43. lfs_file_size(&lfs, &file[0]) => hotsizes[i];
  44. size = strlen("hair");
  45. int j = 0;
  46. for (; j < startsizes[i] && j < hotsizes[i]; j += size) {
  47. lfs_file_read(&lfs, &file[0], buffer, size) => size;
  48. memcmp(buffer, "hair", size) => 0;
  49. }
  50. for (; j < hotsizes[i]; j += size) {
  51. lfs_file_read(&lfs, &file[0], buffer, size) => size;
  52. memcmp(buffer, "\0\0\0\0", size) => 0;
  53. }
  54. lfs_file_truncate(&lfs, &file[0], coldsizes[i]) => 0;
  55. lfs_file_size(&lfs, &file[0]) => coldsizes[i];
  56. lfs_file_close(&lfs, &file[0]) => 0;
  57. }
  58. lfs_unmount(&lfs) => 0;
  59. TEST
  60. tests/test.py << TEST
  61. static const lfs_off_t startsizes[] = {$STARTSIZES};
  62. static const lfs_off_t hotsizes[] = {$HOTSIZES};
  63. static const lfs_off_t coldsizes[] = {$COLDSIZES};
  64. lfs_mount(&lfs, &cfg) => 0;
  65. for (int i = 0; i < sizeof(startsizes)/sizeof(startsizes[0]); i++) {
  66. sprintf((char*)buffer, "hairyhead%d", i);
  67. lfs_file_open(&lfs, &file[0], (const char*)buffer, LFS_O_RDONLY) => 0;
  68. lfs_file_size(&lfs, &file[0]) => coldsizes[i];
  69. size = strlen("hair");
  70. int j = 0;
  71. for (; j < startsizes[i] && j < hotsizes[i] && j < coldsizes[i];
  72. j += size) {
  73. lfs_file_read(&lfs, &file[0], buffer, size) => size;
  74. memcmp(buffer, "hair", size) => 0;
  75. }
  76. for (; j < coldsizes[i]; j += size) {
  77. lfs_file_read(&lfs, &file[0], buffer, size) => size;
  78. memcmp(buffer, "\0\0\0\0", size) => 0;
  79. }
  80. lfs_file_close(&lfs, &file[0]) => 0;
  81. }
  82. lfs_unmount(&lfs) => 0;
  83. TEST
  84. }
  85. echo "--- Cold shrinking truncate ---"
  86. truncate_test \
  87. "2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE" \
  88. "2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE" \
  89. " 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE"
  90. echo "--- Cold expanding truncate ---"
  91. truncate_test \
  92. " 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE" \
  93. " 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE" \
  94. "2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE"
  95. echo "--- Warm shrinking truncate ---"
  96. truncate_test \
  97. "2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE" \
  98. " 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE" \
  99. " 0, 0, 0, 0, 0"
  100. echo "--- Warm expanding truncate ---"
  101. truncate_test \
  102. " 0, $SMALLSIZE, $MEDIUMSIZE, $LARGESIZE, 2*$LARGESIZE" \
  103. "2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE" \
  104. "2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE, 2*$LARGESIZE"
  105. echo "--- Results ---"
  106. tests/stats.py