github.com/pbberlin/tools@v0.0.0-20160910141205-7aa5421c2169/os/fsi/tests/suite1_test.go (about)

     1  // +build suite1
     2  // go test -tags=suite1
     3  
     4  package tests
     5  
     6  // Copyright © 2014 Steve Francia <spf@spf13.com>.
     7  // Copyright 2009 The Go Authors. All rights reserved.
     8  //
     9  // Licensed under the Apache License, Version 2.0 (the "License");
    10  // you may not use this file except in compliance with the License.
    11  // You may obtain a copy of the License at
    12  // http://www.apache.org/licenses/LICENSE-2.0
    13  //
    14  // Unless required by applicable law or agreed to in writing, software
    15  // distributed under the License is distributed on an "AS IS" BASIS,
    16  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    17  // See the License for the specific language governing permissions and
    18  // limitations under the License.
    19  
    20  import (
    21  	"io"
    22  	"testing"
    23  
    24  	"github.com/pbberlin/tools/os/fsi/memfs"
    25  )
    26  
    27  //Read with length 0 should not return EOF.
    28  func TestRead0(t *testing.T) {
    29  
    30  	Fss, c := initFileSystems()
    31  	defer c.Close()
    32  	for _, fs := range Fss {
    33  		path := testDir + "/" + testName
    34  		if err := fs.MkdirAll(testDir, 0777); err != nil {
    35  			t.Fatal(fs.Name(), "unable to create dir", err)
    36  		}
    37  
    38  		f, err := fs.Create(path)
    39  		if err != nil {
    40  			t.Fatal(fs.Name(), "create failed:", err)
    41  		}
    42  		defer f.Close()
    43  		f.WriteString("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
    44  
    45  		b := make([]byte, 0)
    46  		n, err := f.Read(b)
    47  		if n != 0 || err != nil {
    48  			t.Errorf("%v: Read(0) = %d, %v, want 0, nil", fs.Name(), n, err)
    49  		}
    50  		f.Seek(0, 0)
    51  		b = make([]byte, 100)
    52  		n, err = f.Read(b)
    53  		if n <= 0 || err != nil {
    54  			t.Errorf("%v: Read(100) = %d, %v, want >0, nil", fs.Name(), n, err)
    55  		}
    56  	}
    57  }
    58  
    59  func TestMemFileRead(t *testing.T) {
    60  
    61  	Fss, c := initFileSystems()
    62  	defer c.Close()
    63  	for _, fs := range Fss {
    64  
    65  		fsc, ok := memfs.Unwrap(fs)
    66  		if !ok {
    67  			return
    68  		}
    69  
    70  		f, err := fsc.Create("testfile")
    71  		if err != nil {
    72  			t.Errorf("MemFileRead - create failed %v -  %v", err, f)
    73  		}
    74  
    75  		f.WriteString("abcd")
    76  		f.Seek(0, 0)
    77  		b := make([]byte, 8)
    78  		n, err := f.Read(b)
    79  		if n != 4 {
    80  			t.Errorf("didn't read all bytes: %v %v %v", n, err, b)
    81  		}
    82  		if err != nil {
    83  			t.Errorf("err is not nil: %v %v %v", n, err, b)
    84  		}
    85  		n, err = f.Read(b)
    86  		if n != 0 {
    87  			t.Errorf("read more bytes: %v %v %v", n, err, b)
    88  		}
    89  		if err != io.EOF {
    90  			t.Errorf("error is not EOF: %v %v %v", n, err, b)
    91  		}
    92  	}
    93  
    94  }