1
0

format_mutation_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package common
  2. import "testing"
  3. // TestFormatTraffic_UnitBoundaries pins the exact switch point in the loop
  4. // condition `size >= 1024`: a unit must roll over at exactly 1024 (not 1023,
  5. // not 1025), and a value one byte short must stay in the lower unit. This kills
  6. // CONDITIONALS_BOUNDARY (>= -> >) and ARITHMETIC_BASE on the 1024 comparison.
  7. func TestFormatTraffic_UnitBoundaries(t *testing.T) {
  8. cases := []struct {
  9. name string
  10. bytes int64
  11. want string
  12. }{
  13. // Just below the first boundary: must NOT roll over to KB.
  14. {"one_below_kb", 1023, "1023.00B"},
  15. // Exactly at the boundary: must roll over to KB.
  16. {"exactly_kb", 1024, "1.00KB"},
  17. // Just above: stays in KB.
  18. {"one_above_kb", 1025, "1.00KB"},
  19. // Just below the MB boundary: stays in KB (proves division divisor 1024).
  20. {"one_below_mb", 1024*1024 - 1, "1024.00KB"},
  21. // Exactly at the MB boundary: rolls over to MB.
  22. {"exactly_mb", 1024 * 1024, "1.00MB"},
  23. }
  24. for _, c := range cases {
  25. t.Run(c.name, func(t *testing.T) {
  26. if got := FormatTraffic(c.bytes); got != c.want {
  27. t.Fatalf("FormatTraffic(%d) = %q, want %q", c.bytes, got, c.want)
  28. }
  29. })
  30. }
  31. }
  32. // TestFormatTraffic_ClampsAtPB pins the upper bound guard
  33. // `unitIndex < len(units)-1`: huge values must clamp at PB instead of indexing
  34. // past the units slice. A mutated bound (< -> <= via CONDITIONALS_BOUNDARY, or
  35. // len(units)-1 -> len(units)+1 via INVERT_NEGATIVES/ARITHMETIC_BASE) would run
  36. // one extra iteration and panic with index-out-of-range, so the assertion that
  37. // these return a normal "PB" string kills those mutants.
  38. func TestFormatTraffic_ClampsAtPB(t *testing.T) {
  39. const pb = int64(1024 * 1024 * 1024 * 1024 * 1024)
  40. cases := []struct {
  41. name string
  42. bytes int64
  43. want string
  44. }{
  45. // Stays at PB even though size is still >= 1024 at the PB level.
  46. {"1024_pb", 1024 * pb, "1024.00PB"},
  47. // Max int64 must not overflow the units slice.
  48. {"max_int64", 9223372036854775807, "8192.00PB"},
  49. }
  50. for _, c := range cases {
  51. t.Run(c.name, func(t *testing.T) {
  52. if got := FormatTraffic(c.bytes); got != c.want {
  53. t.Fatalf("FormatTraffic(%d) = %q, want %q", c.bytes, got, c.want)
  54. }
  55. })
  56. }
  57. }