Skip to content

Commit

Permalink
Generate more possible random float values
Browse files Browse the repository at this point in the history
Instead of only generating integers, generate any possible float
representation, negatives, decimals, integers, NaNs, and Infs included.
This is achieved by simply bitcasting a random `uXX` to a `fXX`.
  • Loading branch information
InKryption committed Aug 6, 2024
1 parent 951c32f commit 32c3aeb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/accountsdb/genesis_config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ pub const Rent = extern struct {
pub fn random(rand: std.Random) Rent {
return .{
.lamports_per_byte_year = rand.int(u64),
.exemption_threshold = @floatFromInt(rand.int(u32)),
.exemption_threshold = @bitCast(rand.int(u64)),
.burn_percent = rand.uintAtMost(u8, 100),
};
}
Expand Down Expand Up @@ -112,12 +112,12 @@ pub const Inflation = struct {

pub fn random(rand: std.Random) Inflation {
return .{
.initial = @floatFromInt(rand.int(u32)),
.terminal = @floatFromInt(rand.int(u32)),
.taper = @floatFromInt(rand.int(u32)),
.foundation = @floatFromInt(rand.int(u32)),
.foundation_term = @floatFromInt(rand.int(u32)),
.__unused = @floatFromInt(rand.int(u32)),
.initial = @bitCast(rand.int(u64)),
.terminal = @bitCast(rand.int(u64)),
.taper = @bitCast(rand.int(u64)),
.foundation = @bitCast(rand.int(u64)),
.foundation_term = @bitCast(rand.int(u64)),
.__unused = @bitCast(rand.int(u64)),
};
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/accountsdb/snapshots.zig
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ pub const Delegation = struct {
.stake = rand.int(u64),
.activation_epoch = rand.int(Epoch),
.deactivation_epoch = rand.int(Epoch),
.warmup_cooldown_rate = @floatFromInt(rand.int(u32)),
.warmup_cooldown_rate = @bitCast(rand.int(u64)),
};
}
};
Expand All @@ -345,7 +345,7 @@ pub const RentCollector = struct {
return .{
.epoch = rand.int(Epoch),
.epoch_schedule = EpochSchedule.random(rand),
.slots_per_year = @floatFromInt(rand.int(u32)),
.slots_per_year = @bitCast(rand.int(u64)),
.rent = Rent.random(rand),
};
}
Expand Down Expand Up @@ -870,7 +870,7 @@ pub const BankFields = struct {
.ticks_per_slot = rand.int(u64),
.ns_per_slot = rand.int(u128),
.genesis_creation_time = rand.int(sig.accounts_db.genesis_config.UnixTimestamp),
.slots_per_year = @floatFromInt(rand.int(u32)),
.slots_per_year = @bitCast(rand.int(u64)),
.accounts_data_len = rand.int(u64),
.slot = rand.int(Slot),
.epoch = rand.int(Epoch),
Expand Down

0 comments on commit 32c3aeb

Please sign in to comment.