Source file src/simd/testdata/iface_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build goexperiment.simd
     6  
     7  package testdata_test
     8  
     9  import (
    10  	"reflect"
    11  	"simd"
    12  	"simd/testdata/iface"
    13  	"testing"
    14  )
    15  
    16  func TestIFaceFoo(t *testing.T) {
    17  	u := simd.BroadcastFloat32s(4)
    18  	v := simd.BroadcastFloat32s(1)
    19  	vc := iface.VC(v)
    20  	uc := iface.VC(u)
    21  
    22  	hv := iface.MakeHasFoo(vc) // generic interface w/ Foo method
    23  
    24  	sum := hv.Foo(uc)
    25  
    26  	s := make([]float32, u.Len())
    27  
    28  	sum.Data().Store(s) // The method of a dependent type works.
    29  
    30  	if s[0] != 5 {
    31  		t.Errorf("(from Data()) expected 5, got %f", s[0])
    32  	}
    33  
    34  	sum.Field.Store(s)
    35  
    36  	if s[0] != 5 {
    37  		t.Errorf("(from Field) expected 5, got %f", s[0])
    38  	}
    39  }
    40  
    41  func TestIFaceBar(t *testing.T) {
    42  	u := simd.BroadcastFloat32s(4)
    43  	v := simd.BroadcastFloat32s(1)
    44  	vc := iface.VC(v)
    45  	uc := iface.VC(u)
    46  
    47  	hv := iface.MakeHasBar(vc) // non-generic interface w/ Foo method
    48  
    49  	sum := hv.Bar(uc)
    50  
    51  	s := make([]float32, u.Len())
    52  
    53  	sum.Data().Store(s) // The method of a dependent type works.
    54  
    55  	if s[0] != 5 {
    56  		t.Errorf("(from Data()) expected 5, got %f", s[0])
    57  	}
    58  
    59  	sum.Field.Store(s)
    60  
    61  	if s[0] != 5 {
    62  		t.Errorf("(from Field) expected 5, got %f", s[0])
    63  	}
    64  }
    65  
    66  func TestIFaceEmbedFoo(t *testing.T) {
    67  	u := simd.BroadcastFloat32s(4)
    68  	v := simd.BroadcastFloat32s(1)
    69  	vc := iface.VC(v)
    70  	uc := iface.VC(u)
    71  
    72  	hv := iface.MakeHasEmbedFoo(vc) // generic interface w/ Foo method
    73  
    74  	sum := hv.Foo(uc)
    75  
    76  	s := make([]float32, u.Len())
    77  
    78  	sum.Data().Store(s) // The method of a dependent type works.
    79  
    80  	if s[0] != 5 {
    81  		t.Errorf("(from Data()) expected 5, got %f", s[0])
    82  	}
    83  
    84  	sum.Field.Store(s)
    85  
    86  	if s[0] != 5 {
    87  		t.Errorf("(from Field) expected 5, got %f", s[0])
    88  	}
    89  
    90  	rv := reflect.ValueOf(hv)
    91  	rt := rv.Type()
    92  
    93  	t.Logf("reflect.value is %v", rv)
    94  	t.Logf("reflect.type is %v", rt)
    95  }
    96  
    97  func TestIFaceEmbedBar(t *testing.T) {
    98  	u := simd.BroadcastFloat32s(4)
    99  	v := simd.BroadcastFloat32s(1)
   100  	vc := iface.VC(v)
   101  	uc := iface.VC(u)
   102  
   103  	hv := iface.MakeHasEmbedBar(vc) // generic interface w/ Foo method
   104  
   105  	sum := hv.Bar(uc)
   106  
   107  	s := make([]float32, u.Len())
   108  
   109  	sum.Data().Store(s) // The method of a dependent type works.
   110  
   111  	if s[0] != 5 {
   112  		t.Errorf("(from Data()) expected 5, got %f", s[0])
   113  	}
   114  
   115  	sum.Field.Store(s)
   116  
   117  	if s[0] != 5 {
   118  		t.Errorf("(from Field) expected 5, got %f", s[0])
   119  	}
   120  
   121  	rv := reflect.ValueOf(hv)
   122  	rt := rv.Type()
   123  
   124  	t.Logf("reflect.value is %v", rv)
   125  	t.Logf("reflect.type is %v", rt)
   126  }
   127  
   128  func TestIFaceVL(t *testing.T) {
   129  	var v simd.Int8s
   130  	if a, b := iface.VL, iface.Generic[simd.Int8s](1); a != b {
   131  		t.Errorf("expected iface.VL [%d] == iface.Generic[simd.Int8s](1) [%d], but not true", a, b)
   132  	}
   133  	if a, b := iface.VL, v.Len()+1; a != b {
   134  		t.Errorf("expected iface.VL [%d] == v.Len()+1 [%d], but not true", a, b)
   135  	}
   136  }
   137  

View as plain text