Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions src/__tests__/lib/exampleData.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ const makeTime: () => number = (() => {
* The factory will throw if it ever runs out of integers in its set. Both
* initialization and use should be O(1).
*/
const makeUniqueRandInt = (itemsType: string, end: number): (() => number) => {
const makeUniqueRandInt = (itemsType: string, end: number, start: number = 0): (() => number) => {
// Sparse array. Pretends to be initialized to iota.
const deck = new Array(end);

return () => {
if (deck.length === 0) {
if (deck.length === start) {
throw new Error(`ran out of ${itemsType}`);
}
// Perform a single step of the Fisher-Yates shuffle...
const leftIndex = randInt(deck.length);
const leftIndex = randInt(deck.length, start);
const rightIndex = deck.length - 1;
const leftValue = deck[leftIndex] ?? leftIndex;
const rightValue = deck[rightIndex] ?? rightIndex;
Expand Down Expand Up @@ -142,10 +142,12 @@ const makeAvatarUrl = (tag: string) =>
// here with a full-blown URL object in the first place to prevent that.
new UploadedAvatarURL(new URL(`https://zulip.example.org/yo/avatar-${tag}.png`));

// Why start at 11? To reserve IDs 1 through 10 for use as constants in
// snapshot tests.
const randUserId: () => UserId = (
mk => () =>
makeUserId(mk())
)(makeUniqueRandInt('user IDs', 10000));
)(makeUniqueRandInt('user IDs', 10000, 11));
const userOrBotProperties = (args: UserOrBotPropertiesArgs) => {
const user_id = args.user_id != null ? makeUserId(args.user_id) : randUserId();
const randName = randString();
Expand Down Expand Up @@ -288,7 +290,9 @@ export const crossRealmBot: CrossRealmBot = makeCrossRealmBot({
* Streams and subscriptions
*/

const randStreamId: () => number = makeUniqueRandInt('stream IDs', 1000);
// Why start at 11? To reserve IDs 1 through 10 for use as constants in
// snapshot tests.
const randStreamId: () => number = makeUniqueRandInt('stream IDs', 1000, 11);
export const makeStream = (
args: {|
stream_id?: number,
Expand Down Expand Up @@ -418,7 +422,9 @@ const messagePropertiesFromSender = (user: User) => {
});
};

const randMessageId: () => number = makeUniqueRandInt('message ID', 10000000);
// Why start at 11? To reserve IDs 1 through 10 for use as constants in
// snapshot tests.
const randMessageId: () => number = makeUniqueRandInt('message ID', 10000000, 11);

/**
* A PM, by default a 1:1 from eg.otherUser to eg.selfUser.
Expand Down
5 changes: 3 additions & 2 deletions src/utils/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ export function deeperMerge<K, V>(

export const isValidEmailFormat = (email: string = ''): boolean => /\S+@\S+\.\S+/.test(email);

/** Return an integer 0 <= N < end, roughly uniformly at random. */
export const randInt = (end: number): number => Math.floor(Math.random() * end);
/** Return an integer start <= N < end, roughly uniformly at random. */
export const randInt = (end: number, start: number = 0): number =>
Math.floor(Math.random() * (end - start) + start);

/** Return a string that's almost surely different every time. */
export const randString = (): string => randInt(2 ** 54).toString(36);
Loading