public void TimeTaken() { var kmh = Velocity.Create(70m, Velocity.Kph); var duration = kmh.GetTimeTaken(Distance.Create(350m, Distance.Kilometer)); Assert.Equal(Time.Create(5, Time.Hour), duration); }
public void Accelerate() { var currentVelocity = Velocity.Create(0m, Velocity.Ms); var newVelocity = Acceleration.Accelerate(Meter.Create(10m), Second.Create(3m), currentVelocity); Assert.Equal(Velocity.Create(30m, Velocity.Ms), newVelocity); }
public static Result <Vehicle> Create(Velocity maxVelocity) { var id = IdGenerator.GetNextIdForType(IdGenerator.IdType.VEHICLE); return(Velocity.Create(0) .OnSuccess(velocity => Create(id, maxVelocity, velocity)) .OnBoth(vehicleResult => vehicleResult)); }
public void DistanceTraveled() { var kmh = Velocity.Create(70m, Velocity.Kph); var distance = kmh.GetDistanceAfter(Time.Create(5m, Time.Hour)); Assert.Equal(Distance.Create(350m, Distance.Kilometer), distance); }
public static Velocity Accelerate(Meter metersPerSecondSquared, Second durationOfAccelerationInSeconds, Velocity initialVelocity) { var velocityInMs = initialVelocity.ConvertTo(Velocity.Ms); var newVelocityInMs = velocityInMs.Value + (metersPerSecondSquared.Value * durationOfAccelerationInSeconds.Value); return(Velocity.Create(newVelocityInMs, Velocity.Ms).ConvertTo(initialVelocity.Unit)); }
public void MphToMs() { var velocity70Mph = Velocity.Create(70m, Velocity.Mph); var expectedVelocityMs = Velocity.Create(31.2928m, Velocity.Ms); var actualVelocityMs = velocity70Mph.ConvertTo(Velocity.Ms); Assert.Equal(expectedVelocityMs, actualVelocityMs); Assert.Equal(expectedVelocityMs, velocity70Mph); }
public void MphToKph() { var velocity70Mph = Velocity.Create(70m, Velocity.Mph); var expectedVelocityKph = Velocity.Create(112.65408m, Velocity.Kph); var actualVelocityKph = velocity70Mph.ConvertTo(Velocity.Kph); Assert.Equal(expectedVelocityKph, actualVelocityKph); Assert.Equal(expectedVelocityKph, velocity70Mph); }
public static IEnumerable <Result <Vehicle> > CreateRandomVehicles(Velocity minVelocity, Velocity maxVelocity) { while (true) { var randomVelocityValue = Utilities.GetRandomDouble(minVelocity.Value, maxVelocity.Value); yield return(Velocity.Create(randomVelocityValue) .OnSuccess(velocity => Vehicle.Create(velocity)) .OnBoth(vehicleResult => vehicleResult)); } }
public void FeetPerSecondToKpm() { var kmPerMinute = Velocity.Create(Distance.Kilometer, Time.Minute); var velocity13Fps = Velocity.Create(Distance.Create(13.6m, Distance.Feet), Time.Second); var expectedVelocityKpm = Velocity.Create(0.2487168m, kmPerMinute); var actualVelocityKpm = velocity13Fps.ConvertTo(kmPerMinute); Assert.Equal(expectedVelocityKpm, actualVelocityKpm); Assert.Equal(expectedVelocityKpm, velocity13Fps); }
public void VelocityCreateAndConvertTo(string distanceInput, string unitTimeInput, string targetDistanceInput, string targetUnitTimeInput) { Distance.TryParse(distanceInput, out var distance); Time.TryParseUnit(unitTimeInput, out var time); var velocity = Velocity.Create(distance, time); Distance.TryParse(targetDistanceInput, out var targetDistance); Time.TryParseUnit(targetUnitTimeInput, out var targetTime); var targetVelocity = Velocity.Create(targetDistance, targetTime); Assert.Equal(velocity, targetVelocity); }
public static Result <Vehicle> SpeedUp(this Vehicle vehicle) { var randomSpeedToAdd = Utilities.GetRandomDouble(1, 10); var velocityToSetResult = vehicle.CurrentVelocity + Velocity.Create(randomSpeedToAdd); if (velocityToSetResult.IsFailure) { return(Result.Fail <Vehicle>($"Cannot speed up vehicle [{vehicle}]. Reason: {velocityToSetResult.Error}")); } var velocityToSet = velocityToSetResult.Value <= vehicle.MaxVelocity ? velocityToSetResult.Value : vehicle.MaxVelocity; return(Vehicle.Create(vehicle.Id, vehicle.MaxVelocity, velocityToSet)); }
private static Result <(Velocity minVelocity, Velocity maxVelocity)> SetupMinAndMaxVelocities(IConfig configuration) { var minVelocityResult = Velocity.Create(configuration.GetMinAllowedVelocity()); var maxVelocityResult = Velocity.Create(configuration.GetMaxAllowedVelocity()); if (minVelocityResult.IsFailure) { Result.Fail <(Velocity, Velocity)>($"Error setting min. velocity. Reason: {minVelocityResult.Error}"); } if (maxVelocityResult.IsFailure) { Result.Fail <(Velocity, Velocity)>($"Error setting max. velocity. Reason: {maxVelocityResult.Error}"); } return(Result.Ok((minVelocityResult.Value, maxVelocityResult.Value))); }
public void SortedVelocities() { var unsorted = new[] { Velocity.Create(98m, Velocity.Ms), Velocity.Create(100m, Velocity.Kph), Velocity.Create(99m, Velocity.Mph), Velocity.Create(Distance.Create(97m, Distance.Feet), Time.Minute), Velocity.Create(1000m, Velocity.Mph) }; var sorted = unsorted.OrderBy(i => i); var expected = new[] { Velocity.Create(Distance.Create(97m, Distance.Feet), Time.Minute), Velocity.Create(100m, Velocity.Kph), Velocity.Create(99m, Velocity.Mph), Velocity.Create(98m, Velocity.Ms), Velocity.Create(1000m, Velocity.Mph) }; Assert.Equal(expected, sorted); }