Skip to content

Commit 0520ded

Browse files
committed
Add tests and docs for divisors
1 parent 45f42e3 commit 0520ded

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

docs/src/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ Primes.primesmask
3434
```@docs
3535
Primes.radical
3636
Primes.totient
37+
Primes.divisors
3738
```

test/runtests.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,29 @@ end
313313
end
314314
end
315315

316+
# brute-force way to get divisors. Same elements as divisors(n), but order may differ.
317+
divisors_brute_force(n) = [d for d in one(n):n if iszero(n % d)]
318+
319+
@testset "divisors(::$T)" for T in [Int16, Int32, Int64, BigInt]
320+
# 1 and 0 are handled specially
321+
@test divisors(one(T)) == divisors(-one(T)) == T[one(T)]
322+
@test_throws ArgumentError @inferred(divisors(T(0)))
323+
324+
for n in 2:1000
325+
ds = divisors(T(n))
326+
@test ds == divisors(-T(n))
327+
@test sort!(ds) == divisors_brute_force(T(n))
328+
end
329+
end
330+
331+
@testset "divisors(::Factorization)" begin
332+
# divisors(n) calls divisors(factor(abs(n))), so the previous testset covers most cases.
333+
# We just need to verify that the following cases are also handled correctly:
334+
@test divisors(factor(1)) == divisors(factor(-1)) == [1] # factorizations of 1 and -1
335+
@test divisors(factor(-56)) == divisors(factor(56)) == [1, 2, 4, 8, 7, 14, 28, 56] # factorizations of negative numbers
336+
@test_throws ArgumentError @inferred(divisors(factor(0))) # factorizations of 0
337+
end
338+
316339
# check copy property for big primes relied upon in nextprime/prevprime
317340
for n = rand(big(-10):big(10), 10)
318341
@test n+0 !== n

0 commit comments

Comments
 (0)