Validate International Securities Identification Number: Difference between revisions

updated Tcl to use new examples
(Added Kotlin)
(updated Tcl to use new examples)
Line 1,001:
run;</lang>
=={{header|Tcl}}==
 
{{update|Tcl|Use the new test-cases, and consider calling the existing Luhn algorithm implementation from the ''[[Luhn test of credit card numbers]]'' task instead of duplicating it.}}
 
<lang Tcl>package require Tcl 8.6 ;# mostly needed for [assert]. Substitute a simpler one or a NOP if required.</lang>
Line 1,034 ⟶ 1,032:
}
 
# copied from "Luhn test of credit card numbers"
proc cksum {isin} {
# included here for ease of testing, and because it is short
set isin [normalize $isin]
proc cksumluhn {isin}digitString {
assert {[string is digit -strict $isin]}
setif digits{[regexp {[split^0-9]} $isindigitString]} {error "not a number"]}
ifset {[llengthsum $digits] % 2} {0
set digits [list 0flip {*}$digits]1
foreach ch [lreverse [split $digitString {}]] {
incr sum [exprlindex {$o + ($e * 2) % 9}]
{0 1 2 3 4 5 6 7 8 9}
{0 2 4 6 8 1 3 5 7 9}
} [expr {$sum[incr eqflip] [cksum& 1}] $bodych]}
}
foreachreturn [expr {o($sum e}% $digits10) {== 0}]
incr sum [expr {$o + ($e * 2) % 9}]
}
expr {(10 - ($sum % 10)) % 10}
}
 
proc validate {isin} {
setif isin{![regexp {^[normalizeA-Z]{2}[A-Z0-9]{9}[0-9]$} $isin]} {return false}
regexpluhn {^(.*)(.)$}[normalize $isin -> body sum]
expr {$sum eq [cksum $body]}
}
 
}</lang>
 
To run the test suite, we use the tcltest framework included with Tcl:
Finally, some tcltests pinched from other examples in this page:
 
<lang Tcl>package require tcltest
 
tcltest::test isin-1 "Test isin validation" -body {
foreach {strisin sumok} {
US037833100US0378331005 5 yes
US037383100US0373831005 9 no
SU037833100U50378331005 5 no
AU0000XVGZAUS03378331005 3 no
AU0000VXGZAAU0000XVGZA3 3 yes
GB000263494AU0000VXGZA3 6 yes
FR0000988040 yes
} {
assertif {[isin::cksum $str]ok} eq $sum}{
assert {![isin::validate $str$sumisin]}
} else {
 
assert {![isin::validate $str$sumisin]}
set err [expr {1+int(rand()*8)}] ;# create a random checksum error
set sum [expr {$sum + $err % 10}]
assert {![isin::validate $str$sum]}
}
return ok
} -result ok</lang>
</lang>
 
=={{header|Visual Basic}}==
Anonymous user