github.com/Seikaijyu/gio@v0.0.1/internal/stroke/stroke_test.go (about)

     1  // SPDX-License-Identifier: Unlicense OR MIT
     2  
     3  package stroke
     4  
     5  import (
     6  	"strconv"
     7  	"testing"
     8  
     9  	"github.com/Seikaijyu/gio/internal/f32"
    10  )
    11  
    12  func BenchmarkSplitCubic(b *testing.B) {
    13  	type scenario struct {
    14  		segments               int
    15  		from, ctrl0, ctrl1, to f32.Point
    16  	}
    17  
    18  	scenarios := []scenario{
    19  		{
    20  			segments: 4,
    21  			from:     f32.Pt(0, 0),
    22  			ctrl0:    f32.Pt(10, 10),
    23  			ctrl1:    f32.Pt(10, 10),
    24  			to:       f32.Pt(20, 0),
    25  		},
    26  		{
    27  			segments: 8,
    28  			from:     f32.Pt(-145.90305, 703.21277),
    29  			ctrl0:    f32.Pt(-940.20215, 606.05994),
    30  			ctrl1:    f32.Pt(74.58341, 405.815),
    31  			to:       f32.Pt(104.35474, -241.543),
    32  		},
    33  		{
    34  			segments: 16,
    35  			from:     f32.Pt(770.35626, 639.77765),
    36  			ctrl0:    f32.Pt(735.57135, 545.07837),
    37  			ctrl1:    f32.Pt(286.7138, 853.7052),
    38  			to:       f32.Pt(286.7138, 890.5413),
    39  		},
    40  		{
    41  			segments: 33,
    42  			from:     f32.Pt(0, 0),
    43  			ctrl0:    f32.Pt(0, 0),
    44  			ctrl1:    f32.Pt(100, 100),
    45  			to:       f32.Pt(100, 100),
    46  		},
    47  	}
    48  
    49  	for _, s := range scenarios {
    50  		s := s
    51  		b.Run(strconv.Itoa(s.segments), func(b *testing.B) {
    52  			from, ctrl0, ctrl1, to := s.from, s.ctrl0, s.ctrl1, s.to
    53  			quads := make([]QuadSegment, s.segments)
    54  			b.ResetTimer()
    55  			for i := 0; i < b.N; i++ {
    56  				quads = SplitCubic(from, ctrl0, ctrl1, to, quads[:0])
    57  			}
    58  			if len(quads) != s.segments {
    59  				// this is just for checking that we are benchmarking similar splits
    60  				// when splitting algorithm splits differently, then it's fine to adjust the
    61  				// parameters to give appropriate number of segments.
    62  				b.Fatalf("expected %d but got %d", s.segments, len(quads))
    63  			}
    64  		})
    65  	}
    66  }