package com.example.kotlin_sample

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class StdlibTest {

    @Test
    fun testListOperations() {
        // 前置条件：准备原始数据
        val numbers = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

        // 步骤：执行链式操作
        // 1. filter: 过滤偶数
        // 2. map: 数值 * 10
        // 3. sortedDescending: 降序排列
        // 4. take: 取前3个
        val result = numbers
            .filter { it % 2 == 0 }     // [2, 4, 6, 8, 10]
            .map { it * 10 }            // [20, 40, 60, 80, 100]
            .sortedDescending()         // [100, 80, 60, 40, 20]
            .take(3)                    // [100, 80, 60]

        // 验证结果
        val expected = listOf(100, 80, 60)
        assertEquals(expected, result, "List chain operations (filter/map/sort) should match expected result")
    }

    @Test
    fun testComplexObjectList() {
        data class User(val name: String, val age: Int)

        // 前置条件：准备对象列表
        val users = listOf(
            User("Alice", 25),
            User("Bob", 30),
            User("Charlie", 20)
        )

        // 步骤：查找最年长的用户名称
        val oldestUserName = users.maxByOrNull { it.age }?.name

        // 验证
        assertEquals("Bob", oldestUserName, "Should find the user with max age")
    }

    @Test
    fun testStringManipulation() {
        // 前置条件
        val raw = "  Kotlin Multiplatform  "

        // 步骤
        val result = raw.trim().uppercase().replace("MULTIPLATFORM", "KMP")

        // 验证
        assertEquals("KOTLIN KMP", result, "String trimming and replacement should work")
    }
}
