forked from google/periph
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
host: convert multiple benchmark frequency calculation to use physic (g…
…oogle#389) This changes the calculation to be purely integer based, with clear rounding rules. Include a unit test to confirm the actual range.
- Loading branch information
Showing
6 changed files
with
396 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
host/allwinner/allwinnersmoketest/benchmark_gpio_support_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2019 The Periph Authors. All rights reserved. | ||
// Use of this source code is governed under the Apache License, Version 2.0 | ||
// that can be found in the LICENSE file. | ||
|
||
package allwinnersmoketest | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"periph.io/x/periph/conn/physic" | ||
) | ||
|
||
func TestToHz(t *testing.T) { | ||
data := []struct { | ||
N int | ||
T time.Duration | ||
expected physic.Frequency | ||
}{ | ||
{ | ||
0, | ||
time.Second, | ||
0, | ||
}, | ||
{ | ||
1, | ||
0, | ||
0, | ||
}, | ||
{ | ||
1, | ||
time.Millisecond, | ||
physic.KiloHertz, | ||
}, | ||
{ | ||
1, | ||
time.Second, | ||
physic.Hertz, | ||
}, | ||
{ | ||
3, | ||
7 * time.Millisecond, | ||
// 3/7 with perfect rounding. | ||
428571429 * physic.MicroHertz, | ||
}, | ||
{ | ||
3000, | ||
7 * time.Microsecond, | ||
// 3/7 with perfect rounding. | ||
428571428571429 * physic.MicroHertz, | ||
}, | ||
{ | ||
1000000, | ||
1000 * time.Second, | ||
physic.KiloHertz, | ||
}, | ||
{ | ||
1000000, | ||
time.Second, | ||
physic.MegaHertz, | ||
}, | ||
{ | ||
1000000, | ||
time.Millisecond, | ||
physic.GigaHertz, | ||
}, | ||
{ | ||
1000000000, | ||
1000 * time.Second, | ||
physic.MegaHertz, | ||
}, | ||
{ | ||
1000000000, | ||
time.Second, | ||
physic.GigaHertz, | ||
}, | ||
{ | ||
1234556000, | ||
// 2.3s. | ||
2345567891 * time.Nanosecond, | ||
// 10 digits of resolution for 526.336MHz. | ||
526335849711 * physic.MilliHertz, | ||
}, | ||
{ | ||
1000000000, | ||
time.Millisecond, | ||
physic.TeraHertz, | ||
}, | ||
{ | ||
300000000, | ||
7 * time.Millisecond, | ||
// 3/7 with pretty good rounding, keeping in mind that's 42.857GHz. | ||
42857142857143 * physic.MilliHertz, | ||
}, | ||
} | ||
for i, line := range data { | ||
r := testing.BenchmarkResult{N: line.N, T: line.T} | ||
if actual := toHz(&r); actual != line.expected { | ||
t.Fatalf("#%d: toHz(%d, %s) = %s(%d); expected %s(%d)", i, line.N, line.T, actual, actual, line.expected, line.expected) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
host/bcm283x/bcm283xsmoketest/benchmark_gpio_support_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2019 The Periph Authors. All rights reserved. | ||
// Use of this source code is governed under the Apache License, Version 2.0 | ||
// that can be found in the LICENSE file. | ||
|
||
package bcm283xsmoketest | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"periph.io/x/periph/conn/physic" | ||
) | ||
|
||
func TestToHz(t *testing.T) { | ||
data := []struct { | ||
N int | ||
T time.Duration | ||
expected physic.Frequency | ||
}{ | ||
{ | ||
0, | ||
time.Second, | ||
0, | ||
}, | ||
{ | ||
1, | ||
0, | ||
0, | ||
}, | ||
{ | ||
1, | ||
time.Millisecond, | ||
physic.KiloHertz, | ||
}, | ||
{ | ||
1, | ||
time.Second, | ||
physic.Hertz, | ||
}, | ||
{ | ||
3, | ||
7 * time.Millisecond, | ||
// 3/7 with perfect rounding. | ||
428571429 * physic.MicroHertz, | ||
}, | ||
{ | ||
3000, | ||
7 * time.Microsecond, | ||
// 3/7 with perfect rounding. | ||
428571428571429 * physic.MicroHertz, | ||
}, | ||
{ | ||
1000000, | ||
1000 * time.Second, | ||
physic.KiloHertz, | ||
}, | ||
{ | ||
1000000, | ||
time.Second, | ||
physic.MegaHertz, | ||
}, | ||
{ | ||
1000000, | ||
time.Millisecond, | ||
physic.GigaHertz, | ||
}, | ||
{ | ||
1000000000, | ||
1000 * time.Second, | ||
physic.MegaHertz, | ||
}, | ||
{ | ||
1000000000, | ||
time.Second, | ||
physic.GigaHertz, | ||
}, | ||
{ | ||
1234556000, | ||
// 2.3s. | ||
2345567891 * time.Nanosecond, | ||
// 10 digits of resolution for 526.336MHz. | ||
526335849711 * physic.MilliHertz, | ||
}, | ||
{ | ||
1000000000, | ||
time.Millisecond, | ||
physic.TeraHertz, | ||
}, | ||
{ | ||
300000000, | ||
7 * time.Millisecond, | ||
// 3/7 with pretty good rounding, keeping in mind that's 42.857GHz. | ||
42857142857143 * physic.MilliHertz, | ||
}, | ||
} | ||
for i, line := range data { | ||
r := testing.BenchmarkResult{N: line.N, T: line.T} | ||
if actual := toHz(&r); actual != line.expected { | ||
t.Fatalf("#%d: toHz(%d, %s) = %s(%d); expected %s(%d)", i, line.N, line.T, actual, actual, line.expected, line.expected) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.