Go testing Package

Description

Go testing Package

package math //from   w  w  w .  j av a  2 s  .  co  m

import "testing" 

func TestMath(t *testing.T) { 
 cases := []struct { 
     xs    []float64 
     max, min float64 
 }{ 
   { 
      xs: []float64{3, 5, 2, 1, 7, 9}, 
      max: 9, 
      min: 1, 
   }, 
   { 
      xs: []float64{}, 
      max: 0, 
      min: 0, 
   }, 
 } 

 for _, c := range cases { 
     max := Max(c.xs) 
     if max != c.max { 
      t.Errorf("expected %f got %f", c.max, max) 
     } 
     min := Min(c.xs) 
     if min != c.min { 
      t.Errorf("expected %f got %f", c.min, min) 
     } 
  } 
} 
     
func Max(xs []float64) float64 { 
 var max float64 
 for i, x := range xs { 
     if i == 0 || x > max { 
     max = x 
     } 
 } 
 return max 
} 

func Min(xs []float64) float64 { 
 var min float64 
 for i, x := range xs { 
     if i == 0 || x < min { 
           min = x 
     } 
 } 
 return min 
}    



PreviousNext

Related