def test_get_spots_for_rectangle():
	r1, r2, r3 = R(0, 10, 10, 10), R(10, 0, 10, 30), R(20, 10, 10, 10)
	cloud = RectangleCloud([r1, r2, r3])
	occ = cloud.get_occupied_rect()
	
	tobefit = R(0, 0, 20, 20)
	
	spots = cloud.get_spots_for_rectangle(tobefit)
	
	expected_spots = [
		## inner spots
		R(r2.x + r2.w, r3.y + r3.h, 
			occ.x + occ.w - (r2.x + r2.w) + INF,
			occ.y + occ.h - (r3.y + r3.h) + INF),
		R(r2.x + r2.w, occ.y - INF, 
			occ.x + occ.w - (r2.x + r2.w) + INF,
			r3.y - occ.y + INF),
		R(occ.x - INF, occ.y - INF,
			r2.x - occ.x + INF,
			r1.y - occ.y + INF),
		R(occ.x - INF, r1.y + r1.h,
			r2.x - occ.x + INF,
			occ.y + occ.h - (r1.y + r1.h) + INF),
		## outer spots
		R(occ.x + occ.w, occ.y - INF,
			INF,
			occ.h + 2 * INF),
		R(occ.x - INF, occ.y + occ.h,
			occ.w + 2 * INF,
			INF),
		R(occ.x - INF, occ.y - INF,
			INF,
			occ.h + 2 * INF),
		R(occ.x - INF, occ.y - INF,
			occ.w + 2 * INF,
			INF)
	]
	
	print(len(spots), len(expected_spots))
	print(sorted(spots, key=tuple))
	print(sorted(expected_spots, key=tuple))

	assert sorted(spots, key=tuple) == sorted(expected_spots, key=tuple)
def test_rate_candidates():
	# r1, r2, r3 = R(0, 10, 10, 10), R(10, 0, 10, 30), R(20, 10, 10, 10)
	# cloud = RectangleCloud([r1, r2, r3], steps=12)
	# occ = cloud.get_occupied_rect()
	tobefit = R(0, 0, 20, 20)
	# spots = cloud.get_spots_for_rectangle(tobefit)

	# candidates_data = set()
	# for sp in spots:
		# data = cloud.make_candidates_data(sp, tobefit, occ)
		# candidates_data.update(data)

	# candidates_data = list(candidates_data)
	# rated = cloud.rate_candidates(candidates_data, occ)

	# expected_candidates = [
			# R(r2.x + r2.w, r3.y + r3.h, tobefit.w, tobefit.h),
			# R(r2.x - tobefit.w, r1.y + r1.h, tobefit.w, tobefit.h),
			# R(r2.x - tobefit.w, r1.y - tobefit.h, tobefit.w, tobefit.h),
			# R(r2.x + r2.w, r3.y - tobefit.h, tobefit.w, tobefit.h)
		# ]

	# assert (sorted([n[1] for n in sorted(rated, key=lambda t:t[0])][-4:], key=tuple)
		# == sorted(expected_candidates, key=tuple))


	## Try again with different coordinates.
	r1, r2, r3 = R(0, 15, 15, 10), R(15, 0, 10, 30), R(25, 10, 5, 10)
	cloud = RectangleCloud([r1, r2, r3])
	occ = cloud.get_occupied_rect()

	spots = cloud.get_spots_for_rectangle(tobefit)

	print("\n\n>>>>>>>>>>>>>>>>>>>>candidates_data>>>>>>>>>>>>>>>>>>>")
	print("spots", spots)
	
	candidates_data = set()
	for sp in spots:
		data = cloud.make_candidates_data(sp, tobefit)
		print("sp", sp)
		print("data", data)
		candidates_data.update(data)

	candidates_data = list(candidates_data)

	print(candidates_data)
	print("<<<<<<<<<<<<<<<<<<<<<candidates_data<<<<<<<<<<<<<<<<<<<")

	rated = cloud.rate_candidates(candidates_data)

	for i, (ratio, c) in enumerate(rated):
		print(i, ratio, c)
		print(c.debuginfo)
		print()
	print([n for n in sorted(rated, key=lambda t:t[0])])

	expected_candidates = [
		R(r2.x + r2.w, r3.y + r3.h, tobefit.w, tobefit.h),
		R(r2.x - tobefit.w, r1.y + r1.h, tobefit.w, tobefit.h),
		R(r2.x - tobefit.w, r1.y - tobefit.h, tobefit.w, tobefit.h),
		R(r2.x + r2.w, r3.y - tobefit.h, tobefit.w, tobefit.h)
	]

	print("\n", "spots", spots)
	print("expected_candidates", expected_candidates, len(expected_candidates))
	print("actual candidates", rated, len(rated))
	print()

	assert (sorted([n[1] for n in sorted(rated,
											key=lambda t:t[0])][-4:],
											key=tuple)
		== sorted(expected_candidates, key=tuple))