<?php
function valueByClassification ( $valor , $classificacao ) {
if ( $classificacao === 'C' ) {
$valor_final = $valor ;
} else {
$valor_final = abs ( $valor ) ;
if ( $classificacao === 'B' ) {
$valor_final *= - 1 ;
}
}
return $valor_final ;
}
$tests = [
// {valor} {classificacao} {esperado}
[ 5 , 'A' , 5 ] ,
[ - 3 , 'A' , 3 ] ,
[ 0 , 'A' , 0 ] ,
[ 5 , 'B' , - 5 ] ,
[ - 3 , 'B' , - 3 ] ,
[ 0 , 'B' , 0 ] ,
[ 5 , 'C' , 5 ] ,
[ - 3 , 'C' , - 3 ] ,
[ 0 , 'C' , 0 ] ,
] ;
foreach ( $tests as $i => $test ) {
list ( $valor , $classificacao , $esperado ) = $test ;
$obtido = valueByClassification( $valor , $classificacao ) ;
if ( $obtido === $esperado ) {
echo "Teste $i foi aprovado." , PHP_EOL;
} else {
echo "Teste $i foi reprovado. Era esperado {$esperado} , mas foi obtido {$obtido} ." , PHP_EOL;
}
}
PD9waHAKCmZ1bmN0aW9uIHZhbHVlQnlDbGFzc2lmaWNhdGlvbiAoJHZhbG9yLCAkY2xhc3NpZmljYWNhbykgewogICAgCiAgICBpZiAoJGNsYXNzaWZpY2FjYW8gPT09ICdDJykgewogICAgICAgIAogICAgICAgICR2YWxvcl9maW5hbCA9ICR2YWxvcjsKICAgICAgICAKICAgIH0gZWxzZSB7CiAgICAgICAgCiAgICAgICAgJHZhbG9yX2ZpbmFsID0gYWJzKCR2YWxvcik7CgogICAgICAgIGlmICgkY2xhc3NpZmljYWNhbyA9PT0gJ0InKSB7CiAgICAgICAgICAgICR2YWxvcl9maW5hbCAqPSAtMTsKICAgICAgICB9CiAgICAgICAgCiAgICB9CiAgICAKICAgIHJldHVybiAkdmFsb3JfZmluYWw7Cn0KCiR0ZXN0cyA9IFsKICAgIC8vIHt2YWxvcn0gICAgICAgIHtjbGFzc2lmaWNhY2FvfSAgICAgICAge2VzcGVyYWRvfQogICAgWyAgICA1LCAgICAgICAgICAgICAgICAgICdBJywgICAgICAgICAgICAgICAgNSAgICAgXSwgICAgCiAgICBbICAgLTMsICAgICAgICAgICAgICAgICAgJ0EnLCAgICAgICAgICAgICAgICAzICAgICBdLAogICAgWyAgICAwLCAgICAgICAgICAgICAgICAgICdBJywgICAgICAgICAgICAgICAgMCAgICAgXSwgICAgCiAgICAKICAgIFsgICAgNSwgICAgICAgICAgICAgICAgICAnQicsICAgICAgICAgICAgICAgLTUgICAgIF0sICAgIAogICAgWyAgIC0zLCAgICAgICAgICAgICAgICAgICdCJywgICAgICAgICAgICAgICAtMyAgICAgXSwgICAgCiAgICBbICAgIDAsICAgICAgICAgICAgICAgICAgJ0InLCAgICAgICAgICAgICAgICAwICAgICBdLCAgICAKICAgICAgICAKICAgIFsgICAgNSwgICAgICAgICAgICAgICAgICAnQycsICAgICAgICAgICAgICAgIDUgICAgIF0sICAgIAogICAgWyAgIC0zLCAgICAgICAgICAgICAgICAgICdDJywgICAgICAgICAgICAgICAtMyAgICAgXSwgICAgCiAgICBbICAgIDAsICAgICAgICAgICAgICAgICAgJ0MnLCAgICAgICAgICAgICAgICAwICAgICBdLCAgICAKXTsKCmZvcmVhY2goJHRlc3RzIGFzICRpID0+ICR0ZXN0KSB7CiAgICBsaXN0KCR2YWxvciwgJGNsYXNzaWZpY2FjYW8sICRlc3BlcmFkbykgPSAkdGVzdDsKICAgIAogICAgJG9idGlkbyA9IHZhbHVlQnlDbGFzc2lmaWNhdGlvbigkdmFsb3IsICRjbGFzc2lmaWNhY2FvKTsKICAgIAogICAgaWYgKCRvYnRpZG8gPT09ICRlc3BlcmFkbykgewogICAgICAgIGVjaG8gIlRlc3RlICRpIGZvaSBhcHJvdmFkby4iLCBQSFBfRU9MOwogICAgfSBlbHNlIHsKICAgICAgICBlY2hvICJUZXN0ZSAkaSBmb2kgcmVwcm92YWRvLiBFcmEgZXNwZXJhZG8geyRlc3BlcmFkb30sIG1hcyBmb2kgb2J0aWRvIHskb2J0aWRvfS4iLCBQSFBfRU9MOwogICAgfQp9