# big mp div test
# test (2**p + j) ** n / (2**p + j) ** k
# and  (2**p - j) ** n / (2**p - j) ** k
# and the same with remainders 1 and divisor-1

package require tcltest

proc testbzdiv {} {
    for {set p 28} {$p <= 256*28} {incr p $p} {
	puts [list p $p]
	flush stdout
	set two_to_p [expr {1<<$p}] 
	for {set j 1} {$j < $two_to_p && $j < (1<<28)} {incr j $j} {
	    testbzdiv2 $p $two_to_p $j
	    if {$j < $two_to_p} {
		testbzdiv2 $p $two_to_p [expr {-$j}]
	    }
	}
    }
}

proc testbzdiv2 {p two_to_p j} {
    set base [expr {$two_to_p + $j}]
    set powers [list 1 $base]	
    for {set n 2} {$n < 16 && $n*$p <= 800*28} {incr n} {
	lappend powers [expr {[lindex $powers end] * $base}]
    }
    set n 1
    foreach base_to_n [lrange $powers 1 end] {
	set k 0
	foreach base_to_k $powers {
	    if {$k > $n} break
	    set quotient [lindex $powers [expr {$n - $k}]]
	    tcltest::test bzdiv-1.$p.$j.$n.$k {division} {
		list \
		    [expr {($base_to_n - 1) / $base_to_k == ($quotient - 1)}] \
		    [expr {($base_to_n - 1) % $base_to_k == ($base_to_k - 1)}] \
		    [expr {$base_to_n / $base_to_k == $quotient}]
	    } {1 1 1}
	    incr k
	}
	incr n
    }
}

testbzdiv
tcltest::cleanupTests	