github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/utils/test/test_test.go (about) 1 // Copyright 2019 Dolthub, Inc. 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 15 package test 16 17 import ( 18 "io" 19 "io/ioutil" 20 "os" 21 "path/filepath" 22 "testing" 23 ) 24 25 // test your tests so you can test while you test 26 27 func TestLDTestUtils(t *testing.T) { 28 dir, err := ChangeToTestDir("TestLDTestUtils") 29 30 if err != nil { 31 t.Fatal("Couldn't change to test dir") 32 } 33 34 dataSize := 32 35 data := RandomData(dataSize) 36 37 if len(data) != dataSize { 38 t.Error("Wrong amount of data") 39 } 40 41 fName := "test.data" 42 err = ioutil.WriteFile(fName, data, os.ModePerm) 43 44 if err != nil { 45 t.Fatal("Couldn't write to current directory") 46 } 47 48 absPath := filepath.Join(dir, fName) 49 50 fInfo, err := os.Stat(absPath) 51 52 if err != nil { 53 t.Error("File not where expected") 54 } else if fInfo.Size() != int64(dataSize) { 55 t.Error("File not of expected size") 56 } 57 } 58 59 func TestTestReader(t *testing.T) { 60 { 61 dest := make([]byte, 32) 62 tr := NewTestReader(32, 16) 63 readTest(t, dest[:8], tr, 0, 8, false) 64 readTest(t, dest[8:16], tr, 8, 8, false) 65 readTest(t, dest[16:24], tr, 16, 0, true) 66 } 67 68 { 69 dest := make([]byte, 32) 70 tr := NewTestReader(32, 16) 71 readTest(t, dest[:12], tr, 0, 12, false) 72 readTest(t, dest[12:24], tr, 12, 4, true) 73 } 74 75 { 76 dest := make([]byte, 32) 77 tr := NewTestReader(32, -1) 78 readTest(t, dest[:12], tr, 0, 12, false) 79 readTest(t, dest[12:24], tr, 12, 12, false) 80 readTest(t, dest[24:32], tr, 24, 8, false) 81 82 n, err := tr.Read(dest) 83 84 if n != 0 || err != io.EOF { 85 t.Error("Should have hit EOF") 86 } 87 } 88 } 89 90 func readTest(t *testing.T, dest []byte, tr *TestReader, min byte, expectedRead int, expectErr bool) { 91 n, err := tr.Read(dest) 92 93 if n != expectedRead { 94 t.Error("Didn't read expected number of bytes") 95 } 96 97 if expectErr && err == nil { 98 t.Error("Expected error that didn't happen.") 99 } else if !expectErr && err != nil { 100 t.Error("Unexpected error.", err) 101 } 102 103 for i := 0; i < n; i++ { 104 if dest[i] != min+byte(i) { 105 t.Error("Unexpected value found at index", i) 106 } 107 } 108 }