You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.6 KiB

package medianofarrays_test
import (
"math"
"testing"
"git.jxs.me/leetgo/medianofarrays"
"github.com/stretchr/testify/require"
)
func TestEmpty(t *testing.T) {
actual := medianofarrays.FindMedianSortedArrays([]int{}, []int{})
require.InDelta(t, math.NaN(), actual, 0)
}
func TestRightEmpty(t *testing.T) {
actual := medianofarrays.FindMedianSortedArrays([]int{1, 2, 3}, []int{})
require.Equal(t, 2.0, actual)
}
func TestLeftEmpty(t *testing.T) {
actual := medianofarrays.FindMedianSortedArrays([]int{4, 5, 6}, []int{})
require.Equal(t, 5.0, actual)
}
func TestEven(t *testing.T) {
actual := medianofarrays.FindMedianSortedArrays([]int{1, 3, 5}, []int{7, 11, 13})
require.Equal(t, 6.0, actual)
}
func TestOverlap(t *testing.T) {
actual := medianofarrays.FindMedianSortedArrays([]int{1, 7, 23}, []int{1, 7, 11, 17})
require.Equal(t, 7.0, actual)
}
func TestExample1(t *testing.T) {
actual := medianofarrays.FindMedianSortedArrays([]int{1, 2}, []int{3, 4})
require.Equal(t, 2.5, actual)
}
/*
goos: darwin
goarch: arm64
pkg: git.jxs.me/leetgo/medianofarrays
BenchmarkReal-8 95113045 63.64 ns/op
BenchmarkLazy-8 18085866 324.3 ns/op
*/
func BenchmarkReal(b *testing.B) {
left, right := []int{1, 7, 23, 29, 83, 107, 8000, 9000}, []int{2, 4, 6, 7, 9, 73, 84, 106, 108, 109, 8000, 8521, 9000}
for i := 0; i < b.N; i++ {
medianofarrays.FindMedianSortedArrays(left, right)
}
}
func BenchmarkLazy(b *testing.B) {
left, right := []int{1, 7, 23, 29, 83, 107, 8000, 9000}, []int{2, 4, 6, 7, 9, 73, 84, 106, 108, 109, 8000, 8521, 9000}
for i := 0; i < b.N; i++ {
medianofarrays.Lazy(left, right)
}
}