private static string generateFlightId(FlightPlan flightPlan) { StringBuilder sb = new StringBuilder(); int mod = ModNumber; string trimmedCompanyName = flightPlan.CompanyName.Replace(" ", string.Empty); int length = trimmedCompanyName.Length; if (length < 5) { for (int i = 0; i < 5 - length; i++) { mod *= 10; } sb.Append(trimmedCompanyName.Substring(0, length)); } else { sb.Append(trimmedCompanyName.Substring(0, 5)); } // var key = BitConverter.ToUInt64(hashedValue) % mod; var key = Math.Abs(flightPlan.GetHashCode()) % mod; return(sb.Append($"-{key}").ToString().ToUpper()); }
public void GetHashCode_WhenTwoDifferentInstancesAreCreated_ThenInstanceHashCodesAreNotEqual() { // arrange FlightPlanBuilder builder = FlightPlanBuilder.Create() .WithActualDepartureTime(1) .WithAircraft("B738") .WithAlternateAerodrome(new Aerodrome("LZKZ")) .WithCruisingLevel("320") .WithCruisingSpeed("250") .WithDepartureAerodrome(new Aerodrome("LZKZ")) .WithDepartureTime(2) .WithDestinationAerodrome(new Aerodrome("LZIB")) .WithEETHours(2) .WithEETMinutes(0) .WithEnduranceHours(5) .WithEnduranceMinutes(2) .WithFlightRules(FlightRules.IFR) .WithOtherInfo("NONE") .WithPersonsOnBoard(20) .WithRevision(22) .WithRoute("route") .WithSecondAlternateAerodrome(new Aerodrome("EPWA")) .WithTypeOfFlight(FlightType.GeneralAviation); FlightPlanBuilder builder1 = FlightPlanBuilder.Create() .WithActualDepartureTime(1) .WithAircraft("B738") .WithAlternateAerodrome(new Aerodrome("LZKZ")) .WithCruisingLevel("3230") .WithCruisingSpeed("2350") .WithDepartureAerodrome(new Aerodrome("LZKZ")) .WithDepartureTime(2) .WithDestinationAerodrome(new Aerodrome("LKPR")) .WithEETHours(2) .WithEETMinutes(0) .WithEnduranceHours(5) .WithEnduranceMinutes(2) .WithFlightRules(FlightRules.MVFR) .WithOtherInfo("N3ONE") .WithPersonsOnBoard(20) .WithRevision(22) .WithRoute("route") .WithSecondAlternateAerodrome(new Aerodrome("EPWA")) .WithTypeOfFlight(FlightType.GeneralAviation); FlightPlan instance0 = builder.Build(); FlightPlan instance1 = builder1.Build(); // act int result0 = instance0.GetHashCode(); int result1 = instance1.GetHashCode(); // assert Assert.That(instance0, Is.Not.Null); Assert.That(instance1, Is.Not.Null); Assert.That(ReferenceEquals(instance0, instance1), Is.Not.True); Assert.That(Equals(result0, result1), Is.False); }
public static void FlightPlan_HashCodeShouldBeAffectedByPointerAddress() { // Arrange FlightPlan flightPlan = getTestFlightPlan(); FlightPlan secondFlightPlan = new FlightPlan() { CompanyName = flightPlan.CompanyName, Passengers = flightPlan.Passengers, InitialLocation = new Location() { Latitude = flightPlan.InitialLocation.Latitude, Longitude = flightPlan.InitialLocation.Longitude, DateTime = flightPlan.InitialLocation.DateTime }, Segments = new List <Segment>(flightPlan.Segments) }; // Act int expected = flightPlan.GetHashCode(); int actual = secondFlightPlan.GetHashCode(); // Assert Assert.Equal(expected, actual); }