Skip to content

Commit f2286c2

Browse files
add tests
1 parent f386e7f commit f2286c2

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

cidre/src/commonMain/kotlin/at/asitplus/cidre/IpInterface.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ constructor(override val prefix: Prefix, val network: IpNetwork<N, S>) :
2323

2424
if (prefix != other.prefix) return false
2525
if (network != other.network) return false
26+
if (address != other.address) return false
2627

2728
return true
2829
}
2930

3031
override fun hashCode(): Int {
3132
var result = prefix.hashCode()
3233
result = 31 * result + network.hashCode()
34+
result = 31 * result + address.hashCode()
3335
return result
3436
}
3537

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import at.asitplus.cidre.IpInterface
2+
import kotlin.test.Test
3+
import kotlin.test.assertEquals
4+
import kotlin.test.assertNotEquals
5+
6+
class IpInterfaceEqualsTest {
7+
8+
@Test
9+
fun `IPv4 equals and hashCode`() {
10+
val ip1 = IpInterface.V4("192.168.1.1/24")
11+
val ip2 = IpInterface.V4("192.168.1.1/24")
12+
val ip3 = IpInterface.V4("192.168.1.2/24")
13+
val ip4 = IpInterface.V4("192.168.1.1/16")
14+
15+
assertEquals(ip1, ip2, "Should be equal")
16+
assertNotEquals(ip1, ip3, "Different addresses should not be equal")
17+
assertNotEquals(ip1, ip4, "Different prefixes should not be equal")
18+
19+
assertEquals(ip1.hashCode(), ip2.hashCode(), "Equal objects must have equal hashCodes")
20+
assertNotEquals(ip1.hashCode(), ip3.hashCode(), "Should have different hashCodes")
21+
assertNotEquals(ip1.hashCode(), ip4.hashCode(), "Should have different hashCodes")
22+
}
23+
24+
@Test
25+
fun `IPv6 equals and hashCode`() {
26+
val ip1 = IpInterface.V6("2001:db8::1/64")
27+
val ip2 = IpInterface.V6("2001:db8::1/64")
28+
val ip3 = IpInterface.V6("2001:db8::2/64")
29+
val ip4 = IpInterface.V6("2001:db8::1/48")
30+
31+
assertEquals(ip1, ip2, "Should be equal")
32+
assertNotEquals(ip1, ip3, "Different addresses should not be equal")
33+
assertNotEquals(ip1, ip4, "Different prefixes should not be equal")
34+
35+
assertEquals(ip1.hashCode(), ip2.hashCode(), "Equal objects must have equal hashCodes")
36+
assertNotEquals(ip1.hashCode(), ip3.hashCode(), "Should have different hashCodes")
37+
assertNotEquals(ip1.hashCode(), ip4.hashCode(), "Should have different hashCodes")
38+
}
39+
40+
@Test
41+
fun `IPv4 and IPv6 are not equal`() {
42+
val ipv4 = IpInterface.V4("192.168.1.1/24")
43+
val ipv6 = IpInterface.V6("2001:db8::1/64")
44+
45+
assertNotEquals<IpInterface<*, *>>(ipv4, ipv6, "IPv4 and IPv6 should never be equal")
46+
}
47+
}

0 commit comments

Comments
 (0)