use std::net::{IpAddr, Ipv4Addr};
use std::time::Duration;
mod types {
use serde::{Serialize, Deserialize};
use std::time::Duration;
use std::net::{IpAddr, Ipv4Addr};
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct NetworkAddress {
pub ip: IpAddr,
pub port: u16,
}
impl NetworkAddress {
pub fn new(ip_parts: [u8; 4], port: u16) -> Self {
Self {
ip: IpAddr::V4(Ipv4Addr::new(ip_parts[0], ip_parts[1], ip_parts[2], ip_parts[3])),
port,
}
}
pub fn from_ip_port(ip: IpAddr, port: u16) -> Self {
Self { ip, port }
}
pub fn to_socket_addr(&self) -> String {
format!("{}:{}", self.ip, self.port)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MessagePriority {
High,
Normal,
Low,
}
#[derive(Debug, Clone)]
pub struct NetworkMetrics {
pub messages_per_second: f64,
pub connections: usize,
pub avg_latency: Duration,
pub memory_usage: usize,
}
impl Default for NetworkMetrics {
fn default() -> Self {
Self {
messages_per_second: 0.0,
connections: 0,
avg_latency: Duration::from_millis(0),
memory_usage: 0,
}
}
}
}
fn main() {
println!("๐งช Running QuDAG Network Types Tests");
println!("====================================");
test_network_address();
test_message_priority();
test_network_metrics();
println!("\nโ
All basic type tests passed!");
}
fn test_network_address() {
println!("\n๐ก Testing NetworkAddress...");
let addr1 = types::NetworkAddress::new([192, 168, 1, 1], 8080);
assert_eq!(addr1.ip, IpAddr::V4(Ipv4Addr::new(192, 168, 1, 1)));
assert_eq!(addr1.port, 8080);
println!(" โ Creation from IP parts");
let ip = IpAddr::V4(Ipv4Addr::new(10, 0, 0, 1));
let addr2 = types::NetworkAddress::from_ip_port(ip, 9090);
assert_eq!(addr2.ip, ip);
assert_eq!(addr2.port, 9090);
println!(" โ Creation from IP and port");
let socket_str = addr1.to_socket_addr();
assert_eq!(socket_str, "192.168.1.1:8080");
println!(" โ Socket address string formatting");
let addr3 = types::NetworkAddress::new([192, 168, 1, 1], 8080);
assert_eq!(addr1, addr3);
println!(" โ Address equality");
}
fn test_message_priority() {
println!("\n๐ Testing MessagePriority...");
let high = types::MessagePriority::High;
let normal = types::MessagePriority::Normal;
let low = types::MessagePriority::Low;
assert_eq!(high, types::MessagePriority::High);
assert_ne!(high, normal);
println!(" โ Priority equality");
println!(" โ Priority types created successfully");
}
fn test_network_metrics() {
println!("\n๐ Testing NetworkMetrics...");
let metrics = types::NetworkMetrics::default();
assert_eq!(metrics.messages_per_second, 0.0);
assert_eq!(metrics.connections, 0);
assert_eq!(metrics.avg_latency, Duration::from_millis(0));
assert_eq!(metrics.memory_usage, 0);
println!(" โ Default metrics creation");
let custom_metrics = types::NetworkMetrics {
messages_per_second: 100.5,
connections: 10,
avg_latency: Duration::from_millis(50),
memory_usage: 1024,
};
assert_eq!(custom_metrics.messages_per_second, 100.5);
assert_eq!(custom_metrics.connections, 10);
println!(" โ Custom metrics creation");
}
#[cfg(test)]
mod tests {
use super::types::*;
use std::net::{IpAddr, Ipv4Addr};
use std::time::Duration;
#[test]
fn test_network_address_serialization() {
let addr = NetworkAddress::new([127, 0, 0, 1], 8080);
assert_eq!(addr.port, 8080);
assert_eq!(addr.ip, IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)));
}
#[test]
fn test_message_priority_memory_safety() {
let priorities = vec![
MessagePriority::High,
MessagePriority::Normal,
MessagePriority::Low,
];
for priority in priorities {
match priority {
MessagePriority::High => {},
MessagePriority::Normal => {},
MessagePriority::Low => {},
}
}
}
#[test]
fn test_network_metrics_performance() {
let mut metrics = NetworkMetrics::default();
for i in 0..1000 {
metrics.connections = i;
metrics.messages_per_second = i as f64;
}
assert_eq!(metrics.connections, 999);
assert_eq!(metrics.messages_per_second, 999.0);
}
}