#1 ์ฝ์ ํจ์ ๋ฆฌํฉํ ๋ง
#1-1 ๋ฆฌํฉํ ๋งํ ํจ์
// in NutrientViewModel.kt
private fun findIndexToInsert(list: SnapshotStateList<DayMealView>, newItem: DayMealView): Int {
for(i: Int in list.indices) {
if(newItem < list[i]) {
return i
}
}
// ์ฝ์
ํ ์์น๊ฐ ์๋ค๋ฉด ๋งจ ๋(list.size)์ ์ฝ์
return list.size
}
NutrientViewModel์๋ ์ ๋ ฌ๋ ๋ฐฐ์ด์ ์ ์์๋ฅผ ์ฝ์
ํ๋ ํจ์ findIndexToInsert()๊ฐ ์๋ค. ๊ธฐ์กด findIndexToInsert()๋ ์ ์ฝ๋์์ ๋ณด๋ฏ ์ ํ ์ ๋ ฌ์ ์๋ฆฌ๋ก ์งฐ๋ค.
#1-2 ์ด์ง ํ์
์ด์ง ํ์ (Binary search)
#1 ์๊ณ ๋ฆฌ์ฆ #1-1 #1-2 ์ด๋ ๊ฒ ํด์ x์ ์ธ๋ฑ์ค i๋ฅผ ์ฐพ์ผ๋ฉด ์ข๊ฒ ์ง๋ง, ์ด x๊ฐ ๋ฐฐ์ด values ์ด๋์๋ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ๋ ์์ ๊ฒ์ด๋ค. ์ด ๊ฒฝ์ฐ์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํด์ผ ํ ๊น? #1-3 ์ ์ค ๋ค์ด ๊ฒ์์ ์ฐ๋ฆฌ๋
kenel.tistory.com
findIndexToInsert()๋ ์ ๋ ฌ๋ ๋ฐฐ์ด์์์ ์ฝ์
์ ์ํํ๋ค. ์ฆ, ์ด์ง ํ์์ ์๋ฆฌ๋ฅผ ์ ์ฉํ ์ ์๋ค.
#1-3 ๋ฆฌํฉํ ๋ง๋ ํจ์
// in NutrientViewModel.kt
private fun findIndexToInsert(list: SnapshotStateList<DayMealView>, newItem: DayMealView): Int {
var min = 0
var max = list.size - 1
while (min <= max) {
val mid = (min + max) / 2
// up
if (list[mid] < newItem) {
min = mid + 1
// down
} else if (list[mid] > newItem) {
max = mid - 1
// equal
} else { // list[mid] == newItem
return mid + 1
}
}
return max + 1
}
์ด์ง ํ์์ด์ง๋ง ๋ด๊ฐ ์ฐพ๋ ์์์ index๋ฅผ ๋ฐํํ์ง๋ ์๋๋ค. ๋์ , ๋ด๊ฐ ์ฝ์
ํ๊ณ ์ ํ๋ ์์๋ฅผ ์ด๋ ์ธ๋ฑ์ค์ ๋ฃ์ด์ผ ํ ์ง๋ฅผ ๋ฐํํ๋ค.
#2 ํ ์คํธ
#2-1 Unit Testing
[Android] Unit Testing - ๊ธฐ์ด
#1 ์ด์ ๊ธ#1-1 Unit Testing ๊ฐ์ [Android] Unit Testing - ๊ฐ์์ ํ๊ฒฝ ์ค์ #1 ์๋๋ก์ด๋ ์ฑ ํ ์คํธ#1-1 ์๋๋ก์ด๋ ์ฑ ํ ์คํธ์ ์ข ๋ฅ๋จผ์ , ์ฌ๊ธฐ์ ์๋ ๊ตฌ๊ธ ๊ณต์ ๋ฌธ์์์ ์๋๋ก์ด๋ ์ฑ ํ ์คํธ์ ๋ํ
kenel.tistory.com
๋ฆฌํฉํ ๋งํ ํจ์ ๋ก์ง์ ๋ฌธ์ ๊ฐ ์๊ฑฐ๋ ์ถํ ์ฌํ์ํฌ ๋ ๋ฌธ์ ๊ฐ ์๊ธธ ๊ฐ๋ฅ์ฑ์ด ์๋ค. ๋ฐ๋ผ์, ํ์คํ๊ฒ ๋งค๋ญ์ง๊ธฐ ์ํด์ ํ
์คํธ ํด๋์ค๋ฅผ ๋ง๋ค์ด ํ
์คํธ๋ฅผ ์งํํ๋ค. ์ถํ ๋ก์ง์ ์ฌํ์ํฌ ๋๋, ์ง๊ธ ๋ง๋ค์ด๋ ํ
์คํธ ํด๋์ค๋ฅผ ํ์ฉํ๋ฉด ๋ ๊ฒ์ด๋ค.
#2-2 ๊ฐ์ํํ findIndexToInsert()
// ๊ฐ์ํ ์ ์ธ์: (list: SnapshotStateList<DayMealView>, newItem: DayMealView)
private fun findIndexToInsert(list: List<Long>, newItem: Long): Int {
...
}
ํ
์คํธ ํด๋์ค์์ ์ฌ์ฉํ findIndexToInsert()๋ค. DayMealView๋ ์ด 3๊ฐ์ ๋น๊ต ์กฐ๊ฑด์ ๊ฐ์ง๊ณ ๋น๊ตํ์ง๋ง, ์ฌ๊ธฐ์์๋ mealId๋ง์ ๊ฐ์ง๊ณ ๋น๊ตํ๋ค. ์๋ํ๋ฉด, 3๊ฐ์ ๋น๊ต ์กฐ๊ฑด์ ๋จ์ํ "<", ">", "==" ๋น๊ต์ด๊ธฐ ๋๋ฌธ์ด๋ค. ํ๋๋ฅผ ๊ฒ์ฆํ๋ฉด ๋๋จธ์ง๋ ๋ฐ๋ผ์จ๋ค. ๋ฐ๋ผ์ findIndexToInsert๊ฐ (mealId๊ณผ ๋ค๋ฅธ ๋น๊ต ์กฐ๊ฑด์ด ์ ๋ถ ํฌํจ๋) DayMealView๊ฐ ์๋, mealId๋ฅผ ์ธ์๋ก ๋ฐ๋๋ก ์์ ํ๋ค. DayMealView๋ฅผ ๊ทธ๋๋ก ๋ฌ๋ ๋์ง๋ง ์๊ฐ์ด ๋ง์ด ๊ฑธ๋ฆด ๊ฒ์ด๋ค. ์ง๊ธ ๋ด๊ฒ ํ์ํ ๊ฒ์ ๋ก์ง ๊ฒ์ฆ์ด๋ค.
#2-3 ํ ์คํธ ์ผ์ด์ค
// 1
DayMealView.mealId์ ๋ฐฐ์ด: {} (๋น ๋ฐฐ์ด)
๋ฐฐ์ด์ ๋ฃ์ ์ mealId: 1
์ฝ์ ๋ mealId์ ์์น(index): 0
// 2
DayMealView.mealId์ ๋ฐฐ์ด: {1, 7, 13}
๋ฐฐ์ด์ ๋ฃ์ ์ mealId: 9
์ฝ์ ๋ mealId์ ์์น(index): 2
// 3
DayMealView.mealId์ ๋ฐฐ์ด: {2, 6, 7, 8, 10}
๋ฐฐ์ด์ ๋ฃ์ ์ mealId: 11
์ฝ์ ๋ mealId์ ์์น(index): 5
// 4
DayMealView.mealId์ ๋ฐฐ์ด: {1, 2, 3, 4}
๋ฐฐ์ด์ ๋ฃ์ ์ mealId: 2
์ฝ์ ๋ mealId์ ์์น(index): 2
ํ
์คํธ์ ์ฌ์ฉํ ์์ ๋ค. ์ด ๋ชจ๋ ์์ ๋ค์ ํต๊ณผํด์ผ ๋ฆฌํฉํ ๋ง์ด ์ ์๋ฃ๋ ๊ฒ์ผํ
๋ค.
#2-4 ํ ์คํธ ํด๋์ค
// package com.example.nutri_capture_new
import com.google.common.truth.Truth
import org.junit.Test
class FindIndexToInsertTest {
private data class TestCase(
val oldItems: List<Long>, val newItem: Long, val correctInsertedIndex: Int
)
private val testCases = listOf(
TestCase(
oldItems = emptyList(),
newItem = 1,
correctInsertedIndex = 0
),
TestCase(
oldItems = listOf(1, 7, 13),
newItem = 9,
correctInsertedIndex = 2
),
TestCase(
oldItems = listOf(2, 6, 7, 8, 10),
newItem = 11,
correctInsertedIndex = 5
),
TestCase(
oldItems = listOf(1, 2, 3, 4),
newItem = 2,
correctInsertedIndex = 2
),
)
@Test
fun findIndexToInsert_givenTestcases_returnsCorrectIndex() {
for (testCase in testCases) {
val result = findIndexToInsert(testCase.oldItems, testCase.newItem)
Truth.assertThat(result).isEqualTo(testCase.correctInsertedIndex)
}
}
}
private fun findIndexToInsert(list: List<Long>, newItem: Long): Int {
var min = 0
var max = list.size - 1
while (min <= max) {
val mid = (min + max) / 2
// up
if (list[mid] < newItem) {
min = mid + 1
// down
} else if (list[mid] > newItem) {
max = mid - 1
// equal
} else { // list[mid] == newItem
return mid + 1
}
}
return max + 1
}
์์ ๊ฐ์ด ์์ฑํด, Local unit test ์๋ฆฌ์ ๋ฃ๋๋ค. ์ฒ์์๋ NutrientViewModel ํด๋์ค์ ๋ณต์ฌ๋ณธ์ ํต์งธ๋ก ํ
์คํธ ํด๋์ค๋ก ๋ง๋ค๋ ค๊ณ ํ์ผ๋, ์์ง์ ๋ทฐ๋ชจ๋ธ์์ ํ
์คํธํ ๊ฒ ์๊ณ ์์ฑ ๋์ค์ ๋ถํ์ํ ๋
ธ๋ ฅ์ด๋ผ๋ ์๊ฐ์ด ๋ค์ด ์ค๊ฐ์ ๊ทธ๋ง๋๋ค.
#2-5 ํ ์คํธ ๊ฒฐ๊ณผ
๋ฌด์ฌํ ํต๊ณผํ๋ค. ๋, ์ฑ๋ ์ ๋์ํ๋ค.
#3 ์์ฝ
NutrientViewModel.findIndexToInsert()์ ๋ก์ง์ ์ด์ง ํ์์ ์ ์ฉํ๋ค.
#4 ์์ฑ๋ ์ฑ
#4-1 ์ด ๊ฒ์๊ธ ์์ ์ Commit
GitHub - Kanmanemone/nutri-capture-new
Contribute to Kanmanemone/nutri-capture-new development by creating an account on GitHub.
github.com
#4-2 ๋ณธ ํ๋ก์ ํธ์ ๊ฐ์ฅ ์ต์ Commit
GitHub - Kanmanemone/nutri-capture-new
Contribute to Kanmanemone/nutri-capture-new development by creating an account on GitHub.
github.com
'๊ฐ๋ฐ ์ผ์ง ๐ป > Nutri Capture' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Nutri Capture ํ๋ก ํธ์๋ - Typography (2) | 2024.12.28 |
---|---|
Nutri Capture ํ๋ก ํธ์๋ - bottomBar ๋์ ๋ณ๊ฒฝ (1) | 2024.12.18 |
Nutri Capture ํ๋ก ํธ์๋ - item ์ถ๊ฐใ์ ๊ฑฐ ์ ๋๋ฉ์ด์ (0) | 2024.12.15 |
Nutri Capture ํ๋ก ํธ์๋ - INSERT ๋ฐ DELETE ๋ฒํผ ๊ตฌํ (1) | 2024.11.28 |
Nutri Capture ๋ฐฑ์๋ - DeleteDayMeal ์ด๋ฒคํธ ์ถ๊ฐ (0) | 2024.11.28 |