Source file src/simd/midway_variadic_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 simd_test
     8  
     9  import (
    10  	"simd"
    11  	"testing"
    12  )
    13  
    14  func variadicAny(xs ...any) int {
    15  	_ = simd.BroadcastUint8s(0)
    16  	return len(xs)
    17  }
    18  
    19  func variadicInt(xs ...int) int {
    20  	_ = simd.BroadcastUint8s(0)
    21  	sum := 0
    22  	for _, x := range xs {
    23  		sum += x
    24  	}
    25  	return sum
    26  }
    27  
    28  func variadicAfterFixed(base int, xs ...int) int {
    29  	_ = simd.BroadcastUint8s(0)
    30  	return base + len(xs)
    31  }
    32  
    33  func TestMidwayVariadicDispatch(t *testing.T) {
    34  	// An "any" element type is the interesting case: a []any slice is
    35  	// assignable to an any parameter, so dropping the "..." is not a type
    36  	// error and the wrong count is returned instead.
    37  	if got := variadicAny(1, 2, 3); got != 3 {
    38  		t.Errorf("variadicAny(1, 2, 3) = %d, want 3", got)
    39  	}
    40  	if got := variadicAny(); got != 0 {
    41  		t.Errorf("variadicAny() = %d, want 0", got)
    42  	}
    43  	if got := variadicInt(1, 2, 3); got != 6 {
    44  		t.Errorf("variadicInt(1, 2, 3) = %d, want 6", got)
    45  	}
    46  	if got := variadicInt([]int{4, 5}...); got != 9 {
    47  		t.Errorf("variadicInt([]int{4, 5}...) = %d, want 9", got)
    48  	}
    49  	if got := variadicAfterFixed(10, 1, 2); got != 12 {
    50  		t.Errorf("variadicAfterFixed(10, 1, 2) = %d, want 12", got)
    51  	}
    52  }
    53  

View as plain text