CREATE OR REPLACE FUNCTION create_check_digit( p_card_number IN NUMBER ) RETURN NUMBER IS TYPE t_digits IS TABLE OF INTEGER; l_table T_DIGITS := T_DIGITS(); l_count INTEGER := 0; l_num INTEGER; l_digit INTEGER; l_odd INTEGER := 0; l_even INTEGER := 0; l_sum INTEGER := 0; l_check_digit INTEGER; BEGIN IF p_card_number IS NULL THEN raise_application_error( -20001, ‘you must provide a card number‘ ); END IF; FOR i IN REVERSE 1..LENGTH( p_card_number ) LOOP l_count := l_count + 1; l_table.EXTEND(1); l_table( l_count ) := SUBSTR( p_card_number, i, 1 ); END LOOP; FOR i IN 1..l_table.COUNT LOOP l_digit := l_table(i); IF MOD( i, 2 ) > 0 THEN l_num := l_digit * 2; IF l_num > 9 THEN FOR i IN 1..LENGTH( l_num ) LOOP l_odd := l_odd + SUBSTR( l_num, i, 1 ); END LOOP; ELSE l_odd := l_num; END IF; p( ‘odd: ‘ || l_odd ); ELSE l_even := l_digit; END IF; l_sum := l_sum + l_odd + l_even; p( ‘l_sum: ‘ || l_sum ); l_odd := 0; l_even := 0; END LOOP; l_check_digit := ABS( ( CEIL( MOD( l_sum / 10, 10 ) ) * 10 ) - l_sum ); p( ‘check digit: ‘ || l_check_digit ); p( ‘l_sum: ‘ || l_sum ); p( p_card_number || l_check_digit ); RETURN l_check_digit; END create_check_digit;(译注:一大堆的 Oracle 存储过程函数)
[sqlunit] *** Running SQLUnit file: p_cc.xml [sqlunit] Getting connection(DEFAULT) [sqlunit] Setting up test... [sqlunit] Running test[1]: PASSING NULL (125ms) [sqlunit] Running test[2]: VALID CARD NUMBER (4992739871) (15ms) [sqlunit] Running test[3]: VALID CARD NUMBER (4012888888881881) (16ms) [sqlunit] Running test[4]: VALID CARD NUMBER (4111111111111111) (0ms) [sqlunit] Running test[5]: VALID CARD NUMBER (4222222222222) (15ms) [sqlunit] Running test[6]: RANDOM (1) NUMBER (5) (0ms) [sqlunit] Running test[7]: RANDOM (2) NUMBER (55) (0ms) [sqlunit] Running test[8]: RANDOM (3) NUMBER (557) (16ms) [sqlunit] Running test[9]: RANDOM (4) NUMBER (5579) (0ms) [sqlunit] Running test[10]: RANDOM (5) NUMBER (65579) (0ms) [sqlunit] Running test[11]: RANDOM (14) NUMBER (12345678965579) (16ms) [sqlunit] Running test[12]: RANDOM NUMBER (5498975) (0ms) [sqlunit] Tearing down test...
[sqlunit] *** Running SQLUnit file: p_cc.xml [sqlunit] Getting connection(DEFAULT) [sqlunit] Setting up test... [sqlunit] Running test[1]: PASSING NULL (109ms) [sqlunit] Running test[2]: VALID CARD NUMBER (4992739871) (109ms) [sqlunit] Assertion "outparams-equal" failed (6(NUMERIC) != 1(NUMERIC) at outparams[0]) [sqlunit] *** expected: [sqlunit] [sqlunit] 6 [sqlunit] [sqlunit] *** but got: [sqlunit] [sqlunit] 1 [sqlunit] [sqlunit] [sqlunit] Running test[3]: VALID CARD NUMBER (4012888888881881) (0ms) [sqlunit] Running test[4]: VALID CARD NUMBER (4111111111111111) (0ms) [sqlunit] Running test[5]: VALID CARD NUMBER (4222222222222) (0ms) [sqlunit] Assertion "outparams-equal" failed (2(NUMERIC) != 0(NUMERIC) at outparams[0]) [sqlunit] *** expected: [sqlunit] [sqlunit] 2 [sqlunit] [sqlunit] *** but got: [sqlunit] [sqlunit] 0 [sqlunit] [sqlunit] [sqlunit] Running test[6]: RANDOM NUMBER (5498975) (0ms) [sqlunit] Tearing down test... [sqlunit] sqlunit-ant: SQLUnit Tests Failed: In file: p_cc.xml, tests: 6, failures: 2, errors = 0 [sqlunit] SQLUnit Tests Failed: In file: p_cc.xml, tests: 6, failures: 2, errors = 0
数据库单元测试工具-SQLUnit,布布扣,bubuko.com
原文:http://blog.csdn.net/lvye1221/article/details/22063445