AES-XTS VFS (#171)

Co-authored-by: Ben Krieger <ben.krieger@intel.com>
This commit is contained in:
Nuno Cruces
2024-10-17 23:53:39 +01:00
committed by GitHub
parent 714ea0e779
commit d6aebe67cc
15 changed files with 660 additions and 25 deletions

22
internal/util/math.go Normal file
View File

@@ -0,0 +1,22 @@
package util
func abs(n int) int {
if n < 0 {
return -n
}
return n
}
func GCD(m, n int) int {
for n != 0 {
m, n = n, m%n
}
return abs(m)
}
func LCM(m, n int) int {
if n == 0 {
return 0
}
return abs(n) * (abs(m) / GCD(m, n))
}

View File

@@ -0,0 +1,82 @@
package util
import (
"math"
"testing"
)
func Test_abs(t *testing.T) {
tests := []struct {
arg int
want int
}{
{0, 0},
{1, 1},
{-1, 1},
{math.MaxInt, math.MaxInt},
{math.MinInt, math.MinInt},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
if got := abs(tt.arg); got != tt.want {
t.Errorf("abs(%d) = %d, want %d", tt.arg, got, tt.want)
}
})
}
}
func Test_GCD(t *testing.T) {
tests := []struct {
arg1 int
arg2 int
want int
}{
{0, 0, 0},
{0, 1, 1},
{1, 0, 1},
{1, 1, 1},
{2, 3, 1},
{42, 56, 14},
{48, -18, 6},
{1e9, 1e9, 1e9},
{1e9, -1e9, 1e9},
{-1e9, -1e9, 1e9},
{math.MaxInt, math.MaxInt, math.MaxInt},
{math.MinInt, math.MinInt, math.MinInt},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
if got := GCD(tt.arg1, tt.arg2); got != tt.want {
t.Errorf("gcd(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want)
}
})
}
}
func Test_LCM(t *testing.T) {
tests := []struct {
arg1 int
arg2 int
want int
}{
{0, 0, 0},
{0, 1, 0},
{1, 0, 0},
{1, 1, 1},
{2, 3, 6},
{42, 56, 168},
{48, -18, 144},
{1e9, 1e9, 1e9},
{1e9, -1e9, 1e9},
{-1e9, -1e9, 1e9},
{math.MaxInt, math.MaxInt, math.MaxInt},
{math.MinInt, math.MinInt, math.MinInt},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
if got := LCM(tt.arg1, tt.arg2); got != tt.want {
t.Errorf("lcm(%d, %d) = %d, want %d", tt.arg1, tt.arg2, got, tt.want)
}
})
}
}