ThinkPower / test_scoreboard.py
minjiechen's picture
Publish ThinkPower topology analysis website
17f4b23 verified
Raw History Blame Contribute Delete
2.38 kB
"""Verify mechanism sums, nonoverlapping charging and relative score semantics."""
import json,subprocess,unittest
from pathlib import Path
ROOT=Path(__file__).resolve().parent
def build(params=None,one=False):
script="const fs=require('fs'),p=JSON.parse(fs.readFileSync(0,'utf8')),R=require('./static/recommend.js'),E=require('./static/engine.js'),T=require('./static/technology.js'),S=require('./static/scoreboard.js'),d=require('./static/models-data.json'),r=R.recommend(p.params,d,E,T);if(p.one)r.eligible=r.eligible.slice(0,1);console.log(JSON.stringify({report:r,score:S.build(r)}))"
return json.loads(subprocess.check_output(['node','-e',script],cwd=ROOT,input=json.dumps(dict(params=params or {},one=one)).encode()))
class ScoreboardTest(unittest.TestCase):
def test_mechanisms_sum_and_charge_not_double_counted(self):
d=build();r=d['report'];by={x['id']:x for x in r['eligible']}
self.assertEqual(len(d['score']['mechanisms']),5)
for s in d['score']['rows']:
row=by[s['id']];self.assertAlmostEqual(sum(s['powers'].values()),row['loss'])
self.assertAlmostEqual(s['powers']['fet'],r['output_current']**2*row['parts']['fet'])
self.assertAlmostEqual(s['powers']['charging'],r['output_current']**2*(row['req']-row['parts']['fsl']))
self.assertIsNone(s['unknown']['coss']);self.assertIsNone(s['unknown']['core_ripple'])
sc=next(s for s in d['score']['rows'] if s['id']=='2sbuck')
self.assertLess(sc['powers']['charging'],r['output_current']**2*by['2sbuck']['parts']['ssl'])
def test_score_direction_ties_and_bounds(self):
score=build()['score'];rows=score['rows']
for m in score['mechanisms']:
vals=[r['powers'][m['id']] for r in rows];ratings=[r['ratings'][m['id']] for r in rows]
self.assertTrue(all(0<=x<=99 for x in ratings));self.assertEqual(ratings[vals.index(min(vals))],99)
for a in rows:
for b in rows:
if a['powers'][m['id']]<b['powers'][m['id']]-1e-12:self.assertGreaterEqual(a['ratings'][m['id']],b['ratings'][m['id']])
self.assertTrue(all(r['ratings']['ind']==99 for r in rows))
def test_single_and_empty_comparisons(self):
d=build(one=True)['score'];self.assertEqual(len(d['rows']),1)
self.assertTrue(all(v is None for v in d['rows'][0]['ratings'].values()))
self.assertIn('Only one',d['rows'][0]['pro'])
self.assertEqual(build(dict(ratio=8))['score']['rows'],[])
if __name__=='__main__':unittest.main()