示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
0
        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));
        }
示例#4
0
        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);
        }
示例#5
0
        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));
        }
示例#6
0
        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);
        }
示例#7
0
        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);
        }
示例#8
0
 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));
     }
 }
示例#9
0
        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);
        }
示例#10
0
        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);
        }
示例#11
0
        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));
        }
示例#12
0
        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)));
        }
示例#13
0
        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);
        }