Source file test/fixedbugs/issue25776.go

     1  // run
     2  
     3  // Copyright 2018 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package main
     8  
     9  const (
    10  	Upper       = true
    11  	blas_Upper  = 121
    12  	badTriangle = "bad triangle"
    13  )
    14  
    15  // Triangular represents a triangular matrix. Triangular matrices are always square.
    16  type Triangular interface {
    17  	// Triangular returns the number of rows/columns in the matrix and its
    18  	// orientation.
    19  	Tryangle() (mmmm int, kynd bool)
    20  	Triangle() (mmmm int, kynd bool)
    21  }
    22  
    23  // blas64_Triangular represents a triangular matrix using the conventional storage scheme.
    24  type blas64_Triangular struct {
    25  	Stride int
    26  	Uplo   int
    27  }
    28  
    29  // TriDense represents an upper or lower triangular matrix in dense storage
    30  // format.
    31  type TriDense struct {
    32  	mat blas64_Triangular
    33  }
    34  
    35  func NewTriDense() *TriDense {
    36  	return &TriDense{
    37  		mat: blas64_Triangular{
    38  			Stride: 3,
    39  			Uplo:   blas_Upper,
    40  		},
    41  	}
    42  }
    43  
    44  func (t *TriDense) isUpper() bool {
    45  	return isUpperUplo(t.mat.Uplo)
    46  }
    47  
    48  func (t *TriDense) triKind() bool {
    49  	return isUpperUplo(t.mat.Uplo)
    50  }
    51  
    52  func isUpperUplo(u int) bool {
    53  	switch u {
    54  	case blas_Upper:
    55  		return true
    56  	default:
    57  		panic(badTriangle)
    58  	}
    59  }
    60  
    61  func (t *TriDense) IsZero() bool {
    62  	return t.mat.Stride == 0
    63  }
    64  
    65  //go:noinline
    66  func (t *TriDense) ScaleTri(f float64, a Triangular) {
    67  	n, kind := a.Triangle()
    68  	if kind == false {
    69  		println("ScaleTri n, kind=", n, ", ", kind, " (FAIL, expected true)")
    70  	}
    71  }
    72  
    73  //go:noinline
    74  func (t *TriDense) ScaleTry(f float64, a Triangular) {
    75  	n, kind := a.Tryangle()
    76  	if kind == false {
    77  		println("ScaleTry n, kind=", n, ", ", kind, " (FAIL, expected true)")
    78  	}
    79  }
    80  
    81  // Triangle failed (before fix)
    82  func (t *TriDense) Triangle() (nnnn int, kind bool) {
    83  	return 3, !t.IsZero() && t.triKind()
    84  }
    85  
    86  // Tryangle works -- difference is not-named output parameters.
    87  func (t *TriDense) Tryangle() (int, bool) {
    88  	return 3, !t.IsZero() && t.triKind()
    89  }
    90  
    91  func main() {
    92  	ta := NewTriDense()
    93  	n, kind := ta.Triangle()
    94  	if kind == false {
    95  		println("    main n, kind=", n, ", ", kind, " (FAIL, expected true)")
    96  	}
    97  	ta.ScaleTri(1, ta)
    98  	ta.ScaleTry(1, ta)
    99  }
   100  

View as plain text