コンテンツにスキップ

英文维基 | 中文维基 | 日文维基 | 草榴社区

モジュール:Check ncid/testcases

local p = require('Module:UnitTests')

function p:test_live()
    self:preprocess_equals_many('{{#invoke:Check ncid|main|', '}}', {
    	{'', 'error'},
    	{' ', 'error'},
    	{'BA79996686', ''},
    	{'  BA79996686  ', ''},
    	{'BC14112496', ''},
    	{'BN0397311X', ''},
    	{'AN0024521X', ''},
    	{'AN00245210', 'error'},
    	{'AN00245211', 'error'},
    	{'AN00245212', 'error'},
    	{'AN00245213', 'error'},
    	{'AN00245214', 'error'},
    	{'AN00245215', 'error'},
    	{'AN00245216', 'error'},
    	{'AN00245217', 'error'},
    	{'AN00245218', 'error'},
    	{'AN00245219', 'error'},
    	{'BA79996680', 'error'},
    	{'BA79996681', 'error'},
    	{'BA79996682', 'error'},
    	{'BA79996683', 'error'},
    	{'BA79996684', 'error'},
    	{'BA79996685', 'error'},
    	{'BA79996687', 'error'},
    	{'BA79996688', 'error'},
    	{'BA79996689', 'error'},
    	{'BA7999668X', 'error'},
    	{'1234567890', 'error'},
    	{'BA7999668', 'error'},
    	{'BA799966860', 'error'},
    	{'AC12345677', 'error'},
    	{'DA01926703', 'error'}
    }, {nowiki=1})
end

-- 編集画面のデバッグコンソールで使用するための関数
function p.calc_checkdigit(id)
	if not id:match('^%s*([AB][A-DN]%d%d%d%d%d%d%d[%dX]?)%s*$') then
		error('異常なフォーマット')
	end
	
	local c1, c2 = id:byte(1, 2)
	local work = (c1 - 64) % 10 * 9 + (c2 - 64) % 10 * 8
	for i= 3, 9 do
		work = work + tonumber(id:sub(i, i)) * (10 - i)
	end
	work = work % 11
	
	return work == 10 and 'X' or work
end

return p