Skip to content

Commit

Permalink
Reuse math.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Oct 17, 2024
1 parent 47afd3c commit 4360f55
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 113 deletions.
8 changes: 4 additions & 4 deletions vfs/xts/math.go → internal/util/math.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xts
package util

func abs(n int) int {
if n < 0 {
Expand All @@ -7,16 +7,16 @@ func abs(n int) int {
return n
}

func gcd(m, n int) int {
func GCD(m, n int) int {
for n != 0 {
m, n = n, m%n
}
return abs(m)
}

func lcm(m, n int) int {
func LCM(m, n int) int {
if n == 0 {
return 0
}
return abs(n) * (abs(m) / gcd(m, n))
return abs(n) * (abs(m) / GCD(m, n))
}
6 changes: 3 additions & 3 deletions vfs/xts/math_test.go → internal/util/math_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xts
package util

import (
"math"
Expand Down Expand Up @@ -46,7 +46,7 @@ func Test_gcd(t *testing.T) {
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
if got := gcd(tt.arg1, tt.arg2); got != tt.want {
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)
}
})
Expand Down Expand Up @@ -74,7 +74,7 @@ func Test_lcm(t *testing.T) {
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
if got := lcm(tt.arg1, tt.arg2); got != tt.want {
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)
}
})
Expand Down
2 changes: 1 addition & 1 deletion vfs/adiantum/hbsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (h *hbshFile) Truncate(size int64) error {
}

func (h *hbshFile) SectorSize() int {
return lcm(h.File.SectorSize(), blockSize)
return util.LCM(h.File.SectorSize(), blockSize)
}

func (h *hbshFile) DeviceCharacteristics() vfs.DeviceCharacteristic {
Expand Down
22 changes: 0 additions & 22 deletions vfs/adiantum/math.go

This file was deleted.

82 changes: 0 additions & 82 deletions vfs/adiantum/math_test.go

This file was deleted.

2 changes: 1 addition & 1 deletion vfs/xts/xts.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (x *xtsFile) Truncate(size int64) error {
}

func (x *xtsFile) SectorSize() int {
return lcm(x.File.SectorSize(), sectorSize)
return util.LCM(x.File.SectorSize(), sectorSize)
}

func (x *xtsFile) DeviceCharacteristics() vfs.DeviceCharacteristic {
Expand Down

0 comments on commit 4360f55

Please sign in to comment.