<?php
declare(strict_types=1);
require '/workspace/zhct/zhctproject/store/thinkphp/library/think/db/Expression.php';
require __DIR__.'/candidate/NutritionPlainFloatWeight.php';
use app\common\service\NutritionPlainFloatWeight as Plain;
$mode=$argv[1]??'original';if(!in_array($mode,['original','candidate'],true))exit(78);$failed=[];$checks=0;$test=static function($label,$condition)use(&$failed,&$checks){$checks++;if(!$condition)$failed[]=$label;};
foreach(['12345','1234567']as $case){$b=json_decode(file_get_contents(__DIR__.'/private/B/wire-plain-weight-'.$case.'-info.json'),true);$c=json_decode(file_get_contents(__DIR__.'/private/C/wire-plain-weight-'.$case.'-info.json'),true);$request=$c['before']['nutrition'];$input=$case==='12345'?'12.345':'123.4567';$row=['use_weight'=>$input,'power'=>'12.345','other'=>'untouched'];$mapped=$mode==='candidate'?Plain::mapFields($row,true):$row;$weight=$mapped['use_weight'];if($weight instanceof \think\db\Expression){$sql=$weight->getValue();$test($case.' SQL generated finite',preg_match('/^CAST\(([-+0-9.eE]+) AS REAL\)$/D',$sql,$m)===1);$value=(float)($m[1]??'NAN');}else{$value=(float)$weight;}
 $test($case.' actual B promoted32 write oracle',bin2hex(pack('d',$value))===$b['physical']['fields']['computed_weight']['bits64']);
 $physical=['use_weight'=>$c['physical']['fields']['use_weight']['value'],'power'=>12.345];$read=$mode==='candidate'?Plain::physicalRow($physical,true):$physical;$test($case.' actual B getter oracle',bin2hex(pack('d',$read['use_weight']))===$b['physical']['fields']['use_weight']['bits64']);$test($case.' five M,D and other write unchanged',$mapped['power']==='12.345'&&$mapped['other']==='untouched');$test($case.' five M,D read unchanged',$read['power']===12.345);
}
if($mode==='candidate'){
 foreach([[],['power'=>'12.345'],['use_weight'=>null],['use_weight'=>[]],['use_weight'=>INF],['use_weight'=>NAN],['use_weight'=>'notnumeric']]as $row){$out=Plain::mapFields($row,true);$test('unsupported/no field untouched',serialize($out)===serialize($row));}
 $row=['use_weight'=>'123.4567','power'=>'12.345'];$test('B write byte and type unchanged',Plain::mapFields($row,false)===$row);$r=['use_weight'=>123.4567,'power'=>12.345];$test('B physical unchanged',Plain::physicalRow($r,false)===$r);
 foreach([['1e39','CAST(3.4028234663852886e+38 AS REAL)'],['-1e39','CAST(-3.4028234663852886e+38 AS REAL)']]as [$x,$sql])$test('plain finite capacity pure only',(string)Plain::mapFields(['use_weight'=>$x],true)['use_weight']===$sql);
 $computed=['computed_weight'=>123.45670318603516,'sum_weight'=>123.45670318603516];$test('computed aliases untouched',Plain::physicalRow($computed,true)===$computed);
}
$result=['mode'=>$mode,'assertions'=>$checks,'failures'=>$failed,'expected_red'=>$mode==='original','HTTP'=>0,'PDO'=>0,'DML'=>0,'DDL'=>0,'oracle'=>'independent captured formal B/C physical and getter values; finite capacity cases are pure source-model only'];$file=__DIR__.'/private/tdd-'.$mode.'.json';file_put_contents($file,json_encode($result,JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));chmod($file,0600);echo json_encode($result),PHP_EOL;exit($failed?1:0);

