mathutil_test.go 866 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package mathutil
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestClosestDivision(t *testing.T) {
  7. tests := []struct {
  8. current int
  9. into int
  10. expected int
  11. }{
  12. {0, 60, 0},
  13. {1, 60, 1},
  14. {2, 60, 2},
  15. {8, 60, 10},
  16. {7, 60, 6},
  17. {11, 60, 12},
  18. {41, 60, 30},
  19. {42, 60, 30},
  20. {43, 60, 30},
  21. {44, 60, 30},
  22. {45, 60, 60},
  23. {46, 60, 60},
  24. {47, 60, 60},
  25. {48, 60, 60},
  26. {49, 60, 60},
  27. {50, 60, 60},
  28. }
  29. for _, test := range tests {
  30. t.Run(fmt.Sprintf("TestClosestDivision[%d_%d]", test.current, test.into), func(t *testing.T) {
  31. result := FindClosestDivisor(test.current, test.into)
  32. if result != test.expected {
  33. t.Errorf("Expected %d, got %d", test.expected, result)
  34. }
  35. })
  36. }
  37. }
  38. func BenchmarkClosestDivision(b *testing.B) {
  39. for i := 0; i < b.N; i++ {
  40. for current := 0; current <= 60; current++ {
  41. FindClosestDivisor(current, 60)
  42. }
  43. }
  44. }