All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jvmTest.contract.set.PersistentSetGenerator.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2016-2019 JetBrains s.r.o.
 * Use of this source code is governed by the Apache 2.0 License that can be found in the LICENSE.txt file.
 */

package tests.contract.set

import com.google.common.collect.testing.TestStringSetGenerator
import kotlinx.collections.immutable.mutate
import kotlinx.collections.immutable.persistentHashSetOf
import kotlinx.collections.immutable.persistentSetOf

class PersistentSetGenerator {
    object HashSet {
        object Of : TestStringSetGenerator() {
            override fun create(elements: Array): Set {
                return persistentHashSetOf(*elements)
            }
        }

        object AddAll : TestStringSetGenerator() {
            override fun create(elements: Array): Set {
                return persistentHashSetOf().addAll(elements.toList())
            }
        }

        object AddEach : TestStringSetGenerator() {
            override fun create(elements: Array): Set {
                return elements.fold(persistentHashSetOf()) { set, element -> set.add(element) }
            }
        }

        object MutateAddAll : TestStringSetGenerator() {
            override fun create(elements: Array): Set {
                return persistentHashSetOf().mutate { it.addAll(elements) }
            }
        }

        object MutateAddEach : TestStringSetGenerator() {
            override fun create(elements: Array): Set {
                return persistentHashSetOf().mutate { builder -> elements.forEach { builder.add(it) } }
            }
        }


        object Builder {
            object Of : TestStringSetGenerator() {
                override fun create(elements: Array): MutableSet {
                    return persistentHashSetOf(*elements).builder()
                }
            }

            object AddAll : TestStringSetGenerator() {
                override fun create(elements: Array): MutableSet {
                    return persistentHashSetOf().builder().apply { addAll(elements) }
                }
            }

            object AddEach : TestStringSetGenerator() {
                override fun create(elements: Array): MutableSet {
                    return persistentHashSetOf().builder().apply { elements.forEach { add(it) } }
                }
            }
        }
    }


    object OrderedSet {
        object Of : TestStringSetGenerator() {
            override fun create(elements: Array): Set {
                return persistentSetOf(*elements)
            }
        }

        object AddAll : TestStringSetGenerator() {
            override fun create(elements: Array): Set {
                return persistentSetOf().addAll(elements.toList())
            }
        }

        object AddEach : TestStringSetGenerator() {
            override fun create(elements: Array): Set {
                return elements.fold(persistentSetOf()) { set, element -> set.add(element) }
            }
        }

        object MutateAddAll : TestStringSetGenerator() {
            override fun create(elements: Array): Set {
                return persistentSetOf().mutate { it.addAll(elements) }
            }
        }

        object MutateAddEach : TestStringSetGenerator() {
            override fun create(elements: Array): Set {
                return persistentSetOf().mutate { builder -> elements.forEach { builder.add(it) } }
            }
        }


        object Builder {
            object Of : TestStringSetGenerator() {
                override fun create(elements: Array): MutableSet {
                    return persistentSetOf(*elements).builder()
                }
            }

            object AddAll : TestStringSetGenerator() {
                override fun create(elements: Array): MutableSet {
                    return persistentSetOf().builder().apply { addAll(elements) }
                }
            }

            object AddEach : TestStringSetGenerator() {
                override fun create(elements: Array): MutableSet {
                    return persistentSetOf().builder().apply { elements.forEach { add(it) } }
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy