lab.nexedi.com/kirr/go123@v0.0.0-20240207185015-8299741fa871/xio/xio_test.go (about)

     1  // Copyright (C) 2019  Nexedi SA and Contributors.
     2  //                     Kirill Smelkov <kirr@nexedi.com>
     3  //
     4  // This program is free software: you can Use, Study, Modify and Redistribute
     5  // it under the terms of the GNU General Public License version 3, or (at your
     6  // option) any later version, as published by the Free Software Foundation.
     7  //
     8  // You can also Link and Combine this program with other software covered by
     9  // the terms of any of the Free Software licenses or any of the Open Source
    10  // Initiative approved licenses and Convey the resulting work. Corresponding
    11  // source of such a combination shall include the source code for all other
    12  // software used.
    13  //
    14  // This program is distributed WITHOUT ANY WARRANTY; without even the implied
    15  // warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    16  //
    17  // See COPYING file for full licensing terms.
    18  // See https://www.nexedi.com/licensing for rationale and options.
    19  
    20  package xio
    21  
    22  import (
    23  	"context"
    24  	"testing"
    25  )
    26  
    27  // xIO is test Reader/Writer/Closer/...
    28  type xIO struct{}
    29  
    30  func (_ *xIO) Read(ctx context.Context, dst []byte) (int, error) {
    31  	for i := range dst {
    32  		dst[i] = 0
    33  	}
    34  	return len(dst), nil
    35  }
    36  
    37  func (_ *xIO) Write(ctx context.Context, src []byte) (int, error) {
    38  	return len(src), nil
    39  }
    40  
    41  func (_ *xIO) Close() error {
    42  	return nil
    43  }
    44  
    45  // tIO is test io.Reader/io.Writer/...
    46  type tIO struct{}
    47  
    48  func (_ *tIO) Read(dst []byte) (int, error) {
    49  	for i := range dst {
    50  		dst[i] = 0
    51  	}
    52  	return len(dst), nil
    53  }
    54  
    55  func (_ *tIO) Write(src []byte) (int, error) {
    56  	return len(src), nil
    57  }
    58  
    59  func (_ *tIO) Close() error {
    60  	return nil
    61  }
    62  
    63  
    64  // ok1 asserts that v is true.
    65  func ok1(v bool) {
    66  	if !v {
    67  		panic("not ok")
    68  	}
    69  }
    70  
    71  // Verify xio.X <-> io.X conversion
    72  func TestConvert(t *testing.T) {
    73  	x := new(xIO)
    74  	i := new(tIO)
    75  	bg := context.Background()
    76  
    77  	// WithCtx(BindCtx(X)) = X
    78  	ok1( WithCtxR(BindCtxR(x, bg)) == x )
    79  
    80  	ok1( WithCtxW(BindCtxW(x, bg)) == x )
    81  
    82  	ok1( WithCtxR (BindCtxRW(x, bg)) == x )
    83  	ok1( WithCtxW (BindCtxRW(x, bg)) == x )
    84  	ok1( WithCtxRW(BindCtxRW(x, bg)) == x )
    85  
    86  	ok1( WithCtxR (BindCtxRC(x, bg)) == x )
    87  	ok1( WithCtxRC(BindCtxRC(x, bg)) == x )
    88  
    89  	ok1( WithCtxW (BindCtxWC(x, bg)) == x )
    90  	ok1( WithCtxWC(BindCtxWC(x, bg)) == x )
    91  
    92  	ok1( WithCtxR  (BindCtxRWC(x, bg)) == x )
    93  	ok1( WithCtxW  (BindCtxRWC(x, bg)) == x )
    94  	ok1( WithCtxRW (BindCtxRWC(x, bg)) == x )
    95  	ok1( WithCtxRC (BindCtxRWC(x, bg)) == x )
    96  	ok1( WithCtxWC (BindCtxRWC(x, bg)) == x )
    97  	ok1( WithCtxRWC(BindCtxRWC(x, bg)) == x )
    98  
    99  
   100  	// BindCtx(WithCtx(X), bg) = X
   101  	ok1( BindCtxR(WithCtxR(i), bg) == i )
   102  
   103  	ok1( BindCtxW(WithCtxW(i), bg) == i )
   104  
   105  	ok1( BindCtxR (WithCtxRW(i), bg) == i )
   106  	ok1( BindCtxW (WithCtxRW(i), bg) == i )
   107  	ok1( BindCtxRW(WithCtxRW(i), bg) == i )
   108  
   109  	ok1( BindCtxR (WithCtxRC(i), bg) == i )
   110  	ok1( BindCtxRC(WithCtxRC(i), bg) == i )
   111  
   112  	ok1( BindCtxW (WithCtxWC(i), bg) == i )
   113  	ok1( BindCtxWC(WithCtxWC(i), bg) == i )
   114  
   115  	ok1( BindCtxR  (WithCtxRWC(i), bg) == i )
   116  	ok1( BindCtxW  (WithCtxRWC(i), bg) == i )
   117  	ok1( BindCtxRW (WithCtxRWC(i), bg) == i )
   118  	ok1( BindCtxRC (WithCtxRWC(i), bg) == i )
   119  	ok1( BindCtxWC (WithCtxRWC(i), bg) == i )
   120  	ok1( BindCtxRWC(WithCtxRWC(i), bg) == i )
   121  }