File: IndexOverflowJVMTest.kt

package info (click to toggle)
kotlin 1.3.31%2Bds1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 109,908 kB
  • sloc: java: 454,756; xml: 18,599; javascript: 10,452; sh: 513; python: 97; makefile: 69; ansic: 4
file content (159 lines) | stat: -rw-r--r-- 5,899 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
 * that can be found in the license/LICENSE.txt file.
 */

package test.collections

import kotlin.test.*

class IndexOverflowJVMTest {

    companion object {
        fun <T> repeatCounted(value: T, count: Long = Int.MAX_VALUE + 1L): Sequence<T> = Sequence {
            object : Iterator<T> {
                var counter = count
                override fun hasNext(): Boolean = counter > 0
                override fun next(): T = value.also { counter-- }
            }
        }

        val maxIndexSequence = repeatCounted("k", (Int.MAX_VALUE + 1L) + 1L) // here the last index is one greater than Int.MAX_VALUE
        val maxIndexIterable = maxIndexSequence.asIterable()


        val longCountSequence = Sequence {
            object : Iterator<Long> {
                var counter = 0L
                override fun hasNext(): Boolean = true
                override fun next(): Long = counter++
            }
        }

        fun assertIndexOverflow(f: () -> Unit) {
            val ex = assertFailsWith<ArithmeticException>(block = f)
            assertTrue(ex.message!!.contains("index", ignoreCase = true))
        }

        fun assertCountOverflow(f: () -> Unit) {
            val ex = assertFailsWith<ArithmeticException>(block = f)
            assertTrue(ex.message!!.contains("count", ignoreCase = true))
        }

        fun checkIndexPositive(index: Int) {
            if (index < 0) fail("Encountered negative index")
        }
    }

    @Test
    fun indexOfOverflowSequence() {
        assertIndexOverflow { maxIndexSequence.indexOf("j") }
        assertIndexOverflow { maxIndexSequence.lastIndexOf("k") }
        assertIndexOverflow { maxIndexSequence.indexOfFirst { false } }
        assertIndexOverflow { maxIndexSequence.indexOfLast { false } }
    }

    @Test
    fun indexOfOverflowIterable() {
        assertIndexOverflow { maxIndexIterable.indexOf("j") }
        assertIndexOverflow { maxIndexIterable.lastIndexOf("k") }
        assertIndexOverflow { maxIndexIterable.indexOfFirst { false } }
        assertIndexOverflow { maxIndexIterable.indexOfLast { false } }
    }


    @Test
    fun forEachIndexedOverflow() {
        assertIndexOverflow { maxIndexSequence.forEachIndexed { index, _ -> checkIndexPositive(index) } }
        assertIndexOverflow { maxIndexIterable.forEachIndexed { index, _ -> checkIndexPositive(index) } }
    }

    @Test
    fun withIndexOverflow() {
        assertIndexOverflow { maxIndexSequence.withIndex().forEach { (index, _) -> checkIndexPositive(index) } }
        assertIndexOverflow { maxIndexIterable.withIndex().forEach { (index, _) -> checkIndexPositive(index) } }
    }


    @Test
    fun countOverflow() {
        assertCountOverflow { repeatCounted("k").count() }
        assertCountOverflow { repeatCounted("k").count { true } }
        assertCountOverflow { repeatCounted("k").asIterable().count() }
        assertCountOverflow { repeatCounted("k").asIterable().count { true } }
    }

    @Test
    fun averageCountOverflow() {
        assertCountOverflow { repeatCounted(1.0).average() }
        assertCountOverflow { repeatCounted(1L).asIterable().average() }
    }


    private class CountingCollection<T> : AbstractMutableCollection<T>() {
        private var _size = 0

        override fun add(element: T): Boolean {
            if (_size < 0) error("Collection is too long")
            _size++
            return true
        }

        override val size: Int get() = _size

        override fun iterator(): MutableIterator<T> = error("not implemented")
    }

    @Test
    fun mapIndexedOverflow() {
        assertIndexOverflow { maxIndexSequence.mapIndexed { index, _ -> checkIndexPositive(index) }.forEach { } }
        assertIndexOverflow { maxIndexSequence.mapIndexedTo(CountingCollection()) { index, _ -> checkIndexPositive(index) } }
        assertIndexOverflow { maxIndexIterable.mapIndexedTo(CountingCollection()) { index, _ -> checkIndexPositive(index) } }
    }

    @Test
    fun mapNotNullIndexedOverflow() {
        assertIndexOverflow { maxIndexSequence.mapIndexedNotNull { index, _ -> checkIndexPositive(index) }.forEach { } }
        assertIndexOverflow { maxIndexSequence.mapIndexedNotNullTo(CountingCollection()) { index, _ -> checkIndexPositive(index) } }
        assertIndexOverflow { maxIndexIterable.mapIndexedNotNullTo(CountingCollection()) { index, _ -> checkIndexPositive(index) } }
    }

    @Test
    fun filterIndexedOverflow() {
        assertIndexOverflow { maxIndexSequence.filterIndexed { index, _ -> checkIndexPositive(index); true }.forEach { } }
        assertIndexOverflow { maxIndexSequence.filterIndexedTo(CountingCollection()) { index, _ -> checkIndexPositive(index); true } }
        assertIndexOverflow { maxIndexIterable.filterIndexedTo(CountingCollection()) { index, _ -> checkIndexPositive(index); true } }
    }


    @Test
    fun foldIndexedOverflow() {
        assertIndexOverflow { maxIndexSequence.foldIndexed("") { index, _, s -> checkIndexPositive(index); s } }
        assertIndexOverflow { maxIndexIterable.foldIndexed("") { index, _, s -> checkIndexPositive(index); s } }
    }

    @Test
    fun reduceIndexedOverflow() {
        assertIndexOverflow { maxIndexSequence.reduceIndexed { index, _, s -> checkIndexPositive(index); s } }
        assertIndexOverflow { maxIndexIterable.reduceIndexed { index, _, s -> checkIndexPositive(index); s } }
    }


    @Test
    fun dropTwiceMaxValue() {

        val halfMax = (1 shl 30) + 1

        val dropOnce = longCountSequence.drop(halfMax)
        val dropTwice = dropOnce.drop(halfMax)

        val expectedEnd = halfMax.toLong() * 2

        assertEquals(expectedEnd, dropTwice.first())

        val dropTake = dropOnce.take(halfMax + 1)

        assertEquals(expectedEnd, dropTake.last())
    }

}