preallocate_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fileutil
  15. import (
  16. "io/ioutil"
  17. "os"
  18. "testing"
  19. )
  20. func TestPreallocateExtend(t *testing.T) { runPreallocTest(t, testPreallocateExtend) }
  21. func testPreallocateExtend(t *testing.T, f *os.File) {
  22. size := int64(64 * 1000)
  23. if err := Preallocate(f, size, true); err != nil {
  24. t.Fatal(err)
  25. }
  26. stat, err := f.Stat()
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. if stat.Size() != size {
  31. t.Errorf("size = %d, want %d", stat.Size(), size)
  32. }
  33. }
  34. func TestPreallocateFixed(t *testing.T) { runPreallocTest(t, testPreallocateFixed) }
  35. func testPreallocateFixed(t *testing.T, f *os.File) {
  36. size := int64(64 * 1000)
  37. if err := Preallocate(f, size, false); err != nil {
  38. t.Fatal(err)
  39. }
  40. stat, err := f.Stat()
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if stat.Size() != 0 {
  45. t.Errorf("size = %d, want %d", stat.Size(), 0)
  46. }
  47. }
  48. func runPreallocTest(t *testing.T, test func(*testing.T, *os.File)) {
  49. p, err := ioutil.TempDir(os.TempDir(), "preallocateTest")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. defer os.RemoveAll(p)
  54. f, err := ioutil.TempFile(p, "")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. test(t, f)
  59. }