github.com/lheiskan/zebrapack@v4.1.1-0.20181107023619-e955d028f9bf+incompatible/cmd/addzid/old_bambam_tests/doubleslice_test.go (about)

     1  package main
     2  
     3  import (
     4  	"testing"
     5  
     6  	cv "github.com/glycerine/goconvey/convey"
     7  )
     8  
     9  func Test002SlistSliceIntToListListInt64(t *testing.T) {
    10  
    11  	cv.Convey("Given a parsable golang source file with type Matrix struct { M [][]int }", t, func() {
    12  		cv.Convey("then the slice should be converted to a List(List(Int64)) in the capnp output", func() {
    13  
    14  			ex0 := `
    15  type Matrix struct {
    16    M [][]int
    17  }`
    18  			cv.So(ExtractString2String(ex0), ShouldStartWithModuloWhiteSpace, `
    19  struct MatrixCapn { 
    20    m  @0:   List(List(Int64)); 
    21  } 
    22  
    23    func (s *Matrix) Save(w io.Writer) error {
    24      	seg := capn.NewBuffer(nil)
    25      	MatrixGoToCapn(seg, s)
    26      	_, err := seg.WriteTo(w)
    27          return err
    28    }
    29     
    30    func (s *Matrix) Load(r io.Reader) error {
    31      	capMsg, err := capn.ReadFromStream(r, nil)
    32      	if err != nil {
    33      		//panic(fmt.Errorf("capn.ReadFromStream error: %s", err))
    34              return err
    35      	}
    36      	z := ReadRootMatrixCapn(capMsg)
    37          MatrixCapnToGo(z, s)
    38          return nil
    39    }
    40    
    41    func MatrixCapnToGo(src MatrixCapn, dest *Matrix) *Matrix { 
    42      if dest == nil { 
    43        dest = &Matrix{} 
    44      }
    45    
    46      var n int
    47    
    48        // M
    49    	n = src.M().Len()
    50    	dest.M = make([][]int, n)
    51    	for i := 0; i < n; i++ {
    52  		dest.M[i] = Int64ListToSliceInt(capn.Int64List(src.M().At(i)))
    53      }
    54      
    55      return dest
    56    } 
    57  
    58    func MatrixGoToCapn(seg *capn.Segment, src *Matrix) MatrixCapn { 
    59      dest := AutoNewMatrixCapn(seg)
    60    
    61  	mylist1 := seg.NewPointerList(len(src.M))
    62  	for i := range src.M {
    63  		mylist1.Set(i, capn.Object(SliceIntToInt64List(seg, src.M[i])))
    64  	}
    65  	dest.SetM(mylist1)
    66  
    67      return dest
    68    } 
    69  
    70  func SliceIntToInt64List(seg *capn.Segment, m []int) capn.Int64List {
    71  	lst := seg.NewInt64List(len(m))
    72  	for i := range m {
    73  		lst.Set(i, int64(m[i]))
    74  	}
    75  	return lst
    76  }
    77  
    78  func Int64ListToSliceInt(p capn.Int64List) []int {
    79  	v := make([]int, p.Len())
    80  	for i := range v {
    81  		v[i] = int(p.At(i))
    82  	}
    83  	return v
    84  }
    85  `)
    86  		})
    87  	})
    88  }