"""Check that the public-field matcher uses distinct candidates optimally."""
import sys
import unittest
from pathlib import Path
from unittest.mock import patch

sys.path.insert(0,str(Path(__file__).resolve().parents[2]/'research'))
import r44_score_fields

class MatchingTest(unittest.TestCase):
 def test_maximizes_target_count_before_iou(self):
  scores={(0,0):.9,(0,1):.7,(1,0):.8,(1,1):.1}
  with patch.object(r44_score_fields.base,'iou',side_effect=lambda r,c:scores[r,c]):
   result=r44_score_fields.match([0,1],[{'mask':0},{'mask':1}],.5)
  self.assertEqual(result['matched'],2);self.assertAlmostEqual(sum(result['ious']),1.5)
 def test_prediction_cannot_match_two_targets(self):
  with patch.object(r44_score_fields.base,'iou',return_value=.9):
   result=r44_score_fields.match([0,1],[{'mask':0}],.5)
  self.assertEqual(result['matched'],1)
 def test_empty_references(self):
  self.assertEqual(r44_score_fields.match([],[],.5)['matched'],0)

if __name__=='__main__':unittest.main()
