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