Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1 @@
|
|||
package require Tcl 8.6 ;# mostly needed for [assert]. Substitute a simpler one or a NOP if required.
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
proc assert {expr} { ;# for "static" assertions that throw nice errors
|
||||
if {![uplevel 1 [list expr $expr]]} {
|
||||
set msg "{$expr}"
|
||||
catch {append msg " {[uplevel 1 [list subst -noc $expr]]}"}
|
||||
tailcall throw {ASSERT ERROR} $msg
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
namespace eval isin {
|
||||
proc _init {} { ;# sets up the map used on every call
|
||||
variable map
|
||||
set alphabet abcdefghijklmnopqrstuvwxyz
|
||||
set n 9
|
||||
lmap c [split $alphabet ""] {
|
||||
lappend map $c [incr n]
|
||||
}
|
||||
}
|
||||
_init
|
||||
|
||||
proc normalize {isin} {
|
||||
variable map
|
||||
string map $map [string tolower [string trim $isin]]
|
||||
}
|
||||
|
||||
# copied from "Luhn test of credit card numbers"
|
||||
# included here for ease of testing, and because it is short
|
||||
proc luhn digitString {
|
||||
if {[regexp {[^0-9]} $digitString]} {error "not a number"}
|
||||
set sum 0
|
||||
set flip 1
|
||||
foreach ch [lreverse [split $digitString {}]] {
|
||||
incr sum [lindex {
|
||||
{0 1 2 3 4 5 6 7 8 9}
|
||||
{0 2 4 6 8 1 3 5 7 9}
|
||||
} [expr {[incr flip] & 1}] $ch]
|
||||
}
|
||||
return [expr {($sum % 10) == 0}]
|
||||
}
|
||||
|
||||
proc validate {isin} {
|
||||
if {![regexp {^[A-Z]{2}[A-Z0-9]{9}[0-9]$} $isin]} {return false}
|
||||
luhn [normalize $isin]
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package require tcltest
|
||||
|
||||
tcltest::test isin-1 "Test isin validation" -body {
|
||||
foreach {isin ok} {
|
||||
US0378331005 yes
|
||||
US0373831005 no
|
||||
U50378331005 no
|
||||
US03378331005 no
|
||||
AU0000XVGZA3 yes
|
||||
AU0000VXGZA3 yes
|
||||
FR0000988040 yes
|
||||
} {
|
||||
if {$ok} {
|
||||
assert {[isin::validate $isin]}
|
||||
} else {
|
||||
assert {![isin::validate $isin]}
|
||||
}
|
||||
}
|
||||
return ok
|
||||
} -result ok
|
||||
Loading…
Add table
Add a link
Reference in a new issue