5 days ago

Two assert statements on lines 43 and 44 fail.
Should these assert statements be correct, and if so, what changes to make them correct.
Or are they incorrect, and should be removed?

Or have I made a mistake in implementing this exercise solution?

Console output

areas:[TriangularArea (1, 2), (1, 2), (-1, 10):1.25]
area1: TriangularArea (1, 2), (1, 2), (-1, 10)
area2: TriangularArea (1, 2), (1, 2), (-1, 10)
area1.opCmp(area2): 0
foo

core.exception.AssertError@c:\dev\D\ex 51 - 60\c56_p352_3\source\app.d(43): Assertion failure

source/app.d

import std.stdio;
import std.string;
import std.exception;

void main()
{
	auto bluePoint = new Point(1, 2, Color.blue);
	auto greenPoint = new Point(1, 2, Color.green);

	auto redPoint1 = new Point(-1, 10, Color.red);
	auto redPoint2 = new Point(-2, 10, Color.red);
	auto redPoint3 = new Point(-2, 7, Color.red);

	assert(redPoint1 < bluePoint);
	assert(redPoint3 < redPoint2);

	// Even though blue is before green in enum Color,
	// because color is being ignored,
	// bluePoint must not be before greenPoint.
	assert(!(bluePoint < greenPoint));

	// area1 and area2 are constructed by distinct points that happen to have the same values.
	// (Remember that bluePoint and greenPoint should be considered equal.)
	auto area1 = new TriangularArea(bluePoint, greenPoint, redPoint1);
	auto area2 = new TriangularArea(greenPoint, bluePoint, redPoint1);

	// The areas should be equal
	assert(area1 == area2);

	// An associative array
	double[TriangularArea] areas;

	// A value is being entered by area1
	areas[area1] = 1.25;

	// The value is being accessed by area2
	writeln("areas:", areas);
	writeln("area1: ", area1);
	writeln("area2: ", area2);
	writeln("area1.opCmp(area2): ", area1.opCmp(area2));
	writeln("foo");
	
	assert(area2 in areas);			// Breaks here
	assert(areas[area2] == 1.25);	// and here
}

enum Color
{
	blue,
	green,
	red
}

class Point
{
	int x;
	int y;
	Color color;

	this(int x, int y, Color color)
	{
		this.x = x;
		this.y = y;
		this.color = color;
	}

	override bool opEquals(Object o) const
	{
		const rhs = cast(const Point) o;
		return rhs && (x == rhs.x) && (y == rhs.y);
	}

	// toHash should also be implemented.  See later exercise.

	override int opCmp(Object o) const
	{
		const rhs = cast(const Point) o;
		enforce(rhs);

		return (x != rhs.x ? x - rhs.x : y - rhs.y);
	}

	override string toString() const {
		return format("(%s, %s)", x, y);
	}
}

class TriangularArea
{
	Point[3] points;

	this(Point one, Point two, Point three)
	{
		points = [one, two, three];
	}

	override bool opEquals(Object o) const
	{
		const rhs = cast(const TriangularArea) o;
		return rhs && (points == rhs.points);
	}

	override int opCmp(Object o) const
	{
		auto rhs = cast(TriangularArea) o;
		enforce(rhs);

		foreach (i, point; points)
		{
			immutable comparison = point.opCmp(rhs.points[i]);
			if (comparison != 0)
			{
				// The sort order has already been determined. Simply return the result.
				return comparison;
			}
		}

		// The objects are considered equal because all of their points have been equal.
		return 0;
	}

	override size_t toHash() const
	{
		// Since the 'points' member is an array,
		// we can take advantage of the existing toHash algorithm for array types.
		return typeid(points).getHash(&points);
	}

	override string toString() const {
		return format("TriangularArea %s, %s, %s", points[0], points[1], points[2]);
	}
}

5 days ago

On Sunday, 7 September 2025 at 12:53:58 UTC, Brother Bill wrote:

>

Two assert statements on lines 43 and 44 fail.
Should these assert statements be correct, and if so, what changes to make them correct.
Or are they incorrect, and should be removed?

Or have I made a mistake in implementing this exercise solution?

Ad the following code at line 35:

areas[area2] = 1.25;

Assert is rightfully failing because the areas map did not have that object. Same goes for the second assert.