Built with blockbuilder.org
Last active
March 14, 2017 20:47
-
-
Save ocarneiro/4d12ed28ccbed6cd8d281b9c74e94dbf to your computer and use it in GitHub Desktop.
tdd - valida DV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
license: mit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<head> | |
<meta charset="utf-8"> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
<style> | |
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; } | |
</style> | |
</head> | |
<body> | |
<svg width=300 height=300><circle cx=120 cy=120 r=100 style="fill:red" /></svg> | |
<script> | |
var circle = d3.select("circle") | |
var fail = false; | |
function separaDigitos(patrimonio) { | |
var retorno = []; | |
for (l in patrimonio) { | |
retorno.push(parseInt(patrimonio[l])); | |
} | |
return retorno; | |
} | |
function calculaDV(parcial) { | |
var digitos = separaDigitos(parcial); | |
console.log(digitos); | |
var soma = digitos[0] * 7 | |
+ digitos[1] * 6 | |
+ digitos[2] * 5 | |
+ digitos[3] * 4 | |
+ digitos[4] * 3 | |
+ digitos[5] * 2; | |
soma = soma * 10; | |
console.log(soma); | |
var quociente = Math.floor(soma / 11); | |
var subtracao = soma - quociente * 11; | |
var dv = subtracao % 10; | |
return dv; | |
} | |
function validaDV() { | |
} | |
test_valida_dv_zero(); | |
test_calcula_dv_298197(); | |
test_calcula_dv_208242(); | |
//test_valida_dv_um(); | |
test_separa_111(); | |
test_separa_222(); | |
function test_calcula_dv_208242(){ | |
var parcial = "208242"; | |
var dv = calculaDV(parcial); | |
if (! (dv == 0)) { | |
console.log("Falhou: test_calcula_dv_208242"); | |
console.log("DV = " + dv); | |
fail = true; | |
} | |
} | |
function test_calcula_dv_298197(){ | |
var parcial = "298197"; | |
var dv = calculaDV(parcial); | |
if (! (dv == 1)) { | |
console.log("Falhou: test_calcula_dv_298197"); | |
console.log("DV = " + dv); | |
fail = true; | |
} | |
} | |
function test_separa_111() { | |
var patrimonio = "111"; | |
var separado = "[1,1,1]"; | |
var resultado = JSON.stringify(separaDigitos(patrimonio)); | |
if (! (resultado == separado)) { | |
console.log(resultado); | |
console.log("Falhou: test_separa_111"); | |
fail = true; | |
}; | |
} | |
function test_separa_222() { | |
var patrimonio = "222"; | |
var separado = "[2,2,2]"; | |
var resultado = JSON.stringify(separaDigitos(patrimonio)); | |
if (! (resultado == separado)) { | |
console.log(resultado); | |
console.log("Falhou: test_separa_222"); | |
fail = true; | |
}; | |
} | |
function test_valida_dv_um() { | |
if (! validaDV("2981971")==1) { | |
console.log("Falhou: test_valida_dv_um"); | |
fail = true; | |
}; | |
} | |
function test_valida_dv_zero() { | |
if (! validaDV("2082420")==0) { | |
fail = true; | |
}; | |
} | |
if (! fail) { | |
success(); | |
} | |
function success() { | |
circle.attr("style", "fill:green"); | |
} | |
</script> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment