github.com/gopherd/gonum@v0.0.4/mat/shadow_test.go (about)

     1  // Copyright ©2015 The Gonum 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  package mat
     6  
     7  import (
     8  	"testing"
     9  
    10  	"math/rand"
    11  )
    12  
    13  func TestDenseOverlaps(t *testing.T) {
    14  	t.Parallel()
    15  	type view struct {
    16  		i, j, r, c int
    17  		*Dense
    18  	}
    19  
    20  	rnd := rand.New(rand.NewSource(1))
    21  
    22  	for r := 1; r < 20; r++ {
    23  		for c := 1; c < 20; c++ {
    24  			m := NewDense(r, c, nil)
    25  			panicked, message := panics(func() { m.checkOverlap(m.RawMatrix()) })
    26  			if !panicked {
    27  				t.Error("expected matrix overlap with self")
    28  			}
    29  			if message != regionIdentity {
    30  				t.Errorf("unexpected panic message for self overlap: got: %q want: %q", message, regionIdentity)
    31  			}
    32  
    33  			for i := 0; i < 1000; i++ {
    34  				var views [2]view
    35  				for k := range views {
    36  					if r > 1 {
    37  						views[k].i = rnd.Intn(r - 1)
    38  						views[k].r = rnd.Intn(r-views[k].i-1) + 1
    39  					} else {
    40  						views[k].r = 1
    41  					}
    42  					if c > 1 {
    43  						views[k].j = rnd.Intn(c - 1)
    44  						views[k].c = rnd.Intn(c-views[k].j-1) + 1
    45  					} else {
    46  						views[k].c = 1
    47  					}
    48  					views[k].Dense = m.Slice(views[k].i, views[k].i+views[k].r, views[k].j, views[k].j+views[k].c).(*Dense)
    49  
    50  					panicked, _ = panics(func() { m.checkOverlap(views[k].RawMatrix()) })
    51  					if !panicked {
    52  						t.Errorf("expected matrix (%d×%d) overlap with view {rows=%d:%d, cols=%d:%d}",
    53  							r, c, views[k].i, views[k].i+views[k].r, views[k].j, views[k].j+views[k].c)
    54  					}
    55  					panicked, _ = panics(func() { views[k].checkOverlap(m.RawMatrix()) })
    56  					if !panicked {
    57  						t.Errorf("expected view {rows=%d:%d, cols=%d:%d} overlap with parent (%d×%d)",
    58  							views[k].i, views[k].i+views[k].r, views[k].j, views[k].j+views[k].c, r, c)
    59  					}
    60  				}
    61  
    62  				overlapRows := intervalsOverlap(
    63  					interval{views[0].i, views[0].i + views[0].r},
    64  					interval{views[1].i, views[1].i + views[1].r},
    65  				)
    66  				overlapCols := intervalsOverlap(
    67  					interval{views[0].j, views[0].j + views[0].c},
    68  					interval{views[1].j, views[1].j + views[1].c},
    69  				)
    70  				want := overlapRows && overlapCols
    71  
    72  				for k, v := range views {
    73  					w := views[1-k]
    74  					got, _ := panics(func() { v.checkOverlap(w.RawMatrix()) })
    75  					if got != want {
    76  						t.Errorf("unexpected result for overlap test for {rows=%d:%d, cols=%d:%d} with {rows=%d:%d, cols=%d:%d}: got: %t want: %t",
    77  							v.i, v.i+v.r, v.j, v.j+v.c,
    78  							w.i, w.i+w.r, w.j, w.j+w.c,
    79  							got, want)
    80  					}
    81  				}
    82  			}
    83  		}
    84  	}
    85  }
    86  
    87  type interval struct{ from, to int }
    88  
    89  func intervalsOverlap(a, b interval) bool {
    90  	return a.to > b.from && b.to > a.from
    91  }
    92  
    93  // See https://github.com/gonum/matrix/issues/359 for details.
    94  func TestIssue359(t *testing.T) {
    95  	t.Parallel()
    96  	for xi := 0; xi < 2; xi++ {
    97  		for xj := 0; xj < 2; xj++ {
    98  			for yi := 0; yi < 2; yi++ {
    99  				for yj := 0; yj < 2; yj++ {
   100  					a := NewDense(3, 3, []float64{
   101  						1, 2, 3,
   102  						4, 5, 6,
   103  						7, 8, 9,
   104  					})
   105  					x := a.Slice(xi, xi+2, xj, xj+2).(*Dense)
   106  					y := a.Slice(yi, yi+2, yj, yj+2).(*Dense)
   107  
   108  					panicked, _ := panics(func() { x.checkOverlap(y.mat) })
   109  					if !panicked {
   110  						t.Errorf("expected panic for aliased with offsets x(%d,%d) y(%d,%d):\nx:\n%v\ny:\n%v",
   111  							xi, xj, yi, yj, Formatted(x), Formatted(y),
   112  						)
   113  					}
   114  				}
   115  			}
   116  		}
   117  	}
   118  }