### Question:
IdGenerator { public int nextJobSchedulerIdWithRange(int minInclusive, int maxInclusive) { synchronized (IdGenerator.class) { int id = nextId(NEXT_JOB_SCHEDULER_ID_KEY); if (id < minInclusive || id > maxInclusive) { id = minInclusive; update(NEXT_JOB_SCHEDULER_ID_KEY, id + 1); } return id; } } IdGenerator(Context context); int nextJobSchedulerIdWithRange(int minInclusive, int maxInclusive); int nextFirebaseAlarmId(); int nextAlarmManagerId(); static final int INITIAL_ID; }### Answer:
@Test public void testNextId_returnsInitialIdWhenNoStoredNextId() { int nextId = mIdGenerator.nextJobSchedulerIdWithRange(INITIAL_ID, MAX_VALUE); assertThat(nextId, is(INITIAL_ID)); }
@Test public void testNextId_returnsStoredNextId() { int expectedId = 100; storeNextIdInSharedPrefs(expectedId); int nextId = mIdGenerator.nextJobSchedulerIdWithRange(INITIAL_ID, MAX_VALUE); assertThat(nextId, is(expectedId)); }
@Test public void testNextId_returnsInitialIdAfterReturningMaxInteger() { int expectedId = MAX_VALUE; storeNextIdInSharedPrefs(expectedId); int nextId = mIdGenerator.nextJobSchedulerIdWithRange(INITIAL_ID, MAX_VALUE); assertThat(nextId, is(MAX_VALUE)); nextId = mIdGenerator.nextJobSchedulerIdWithRange(INITIAL_ID, MAX_VALUE); assertThat(nextId, is(INITIAL_ID)); }
@Test public void testNextId_belowMinRange() { storeNextIdInSharedPrefs(2); assertThat(mIdGenerator.nextJobSchedulerIdWithRange(10, 100), is(10)); }
@Test public void testNextId_aboveMaxRange() { storeNextIdInSharedPrefs(100); assertThat(mIdGenerator.nextJobSchedulerIdWithRange(10, 100), is(100)); }
@Test public void testNextId_aboveMaxRange2() { storeNextIdInSharedPrefs(110); assertThat(mIdGenerator.nextJobSchedulerIdWithRange(10, 100), is(10)); }
@Test public void testNextId_withinRange() { storeNextIdInSharedPrefs(20); assertThat(mIdGenerator.nextJobSchedulerIdWithRange(10, 100), is(20)); }