refactor: miniooni should be outside of the engine (#206)
* refactor: miniooni should be outside of the engine This is part of https://github.com/ooni/probe/issues/1335. We also need to think whether we wanna keep libminiooni and miniooni separated. The previous use case for having a top-level libminiooni was that of enabling others to integrate miniooni into other binaries. This was usegul when studying internet censorship in Spain in May 2020. I am wondering whether we should be keeping this complexity. I am not sure about this and probably we should be killing it. (In any case, reducing complexity is not the objective of this diff, since I would like instead to move things around with minimal changes and make sure we have a ~good repository organization here.) * fix: import in libminiooni
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
// Package humanizex is like dustin/go-humanize
|
||||
package humanizex
|
||||
|
||||
import "fmt"
|
||||
|
||||
// SI is like dustin/go-humanize.SI
|
||||
func SI(value float64, unit string) string {
|
||||
value, prefix := reduce(value)
|
||||
return fmt.Sprintf("%3.0f %s%s", value, prefix, unit)
|
||||
}
|
||||
|
||||
func reduce(value float64) (float64, string) {
|
||||
if value < 1e03 {
|
||||
return value, " "
|
||||
}
|
||||
value /= 1e03
|
||||
if value < 1e03 {
|
||||
return value, "k"
|
||||
}
|
||||
value /= 1e03
|
||||
if value < 1e03 {
|
||||
return value, "M"
|
||||
}
|
||||
value /= 1e03
|
||||
return value, "G"
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package humanizex_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/engine/humanizex"
|
||||
)
|
||||
|
||||
func TestGood(t *testing.T) {
|
||||
if humanizex.SI(128, "bit/s") != "128 bit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(1280, "bit/s") != " 1 kbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(12800, "bit/s") != " 13 kbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(128000, "bit/s") != "128 kbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(1280000, "bit/s") != " 1 Mbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(12800000, "bit/s") != " 13 Mbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(128000000, "bit/s") != "128 Mbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
if humanizex.SI(1280000000, "bit/s") != " 1 Gbit/s" {
|
||||
t.Fatal("unexpected result")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user