-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up Unix time and frequency conversions
- Loading branch information
Showing
6 changed files
with
85 additions
and
25 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
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,59 @@ | ||
package unixutil_test | ||
|
||
// Based on an Claude AI interaction | ||
|
||
import ( | ||
"testing" | ||
|
||
"example.com/scion-time/base/unixutil" | ||
) | ||
|
||
func TestScaledPPMFromFreq(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
freq float64 | ||
expected int64 | ||
}{ | ||
{"Zero frequency", 0, 0}, | ||
{"Positive frequency", 1, 65536000000}, | ||
{"Negative frequency", -1, -65536000000}, | ||
{"Small positive frequency", 0.000001, 65536}, | ||
{"Small negative frequency", -0.000001, -65536}, | ||
{"Large positive frequency", 1000, 65536000000000}, | ||
{"Large negative frequency", -1000, -65536000000000}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := unixutil.ScaledPPMFromFreq(tt.freq) | ||
if result != tt.expected { | ||
t.Errorf("ScaledPPMFromFreq(%f) = %d; want %d", tt.freq, result, tt.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestFreqFromScaledPPM(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
scaledPPM int64 | ||
expected float64 | ||
}{ | ||
{"Zero scaled PPM", 0, 0}, | ||
{"Positive scaled PPM", 65536000000, 1}, | ||
{"Negative scaled PPM", -65536000000, -1}, | ||
{"Small positive scaled PPM", 65536, 0.000001}, | ||
{"Small negative scaled PPM", -65536, -0.000001}, | ||
{"Large positive scaled PPM", 65536000000000, 1000}, | ||
{"Large negative scaled PPM", -65536000000000, -1000}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := unixutil.FreqFromScaledPPM(tt.scaledPPM) | ||
if result != tt.expected { | ||
t.Errorf("FreqFromScaledPPM(%d) = %f; want %f", tt.scaledPPM, result, tt.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package unixutil | ||
|
||
import ( | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func TimevalFromNsec(nsec int64) unix.Timeval { | ||
sec := nsec / 1e9 | ||
nsec = nsec % 1e9 | ||
// The field unix.Timeval.Usec must always be non-negative. | ||
if nsec < 0 { | ||
sec -= 1 | ||
nsec += 1e9 | ||
} | ||
return unix.Timeval{ | ||
Sec: sec, | ||
Usec: nsec, | ||
} | ||
} |
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
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
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