Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 2d3b7fb

Browse files
committed
fix: add tests for utf16string char and byte traversal
1 parent 294099c commit 2d3b7fb

File tree

4 files changed

+102
-37
lines changed

4 files changed

+102
-37
lines changed

android-tree-sitter/src/test/java/com/itsaky/androidide/treesitter/ParserTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import org.mockito.MockedStatic;
4747
import org.mockito.Mockito;
4848
import org.robolectric.RobolectricTestRunner;
49+
import static com.itsaky.androidide.treesitter.ResourceUtils.readResource;
4950

5051
@RunWith(RobolectricTestRunner.class)
5152
public class ParserTest extends TreeSitterTest {
@@ -641,8 +642,4 @@ private static int getTrimmedLength(CharSequence s) {
641642

642643
return end - start;
643644
}
644-
645-
private static String readResource(String... names) {
646-
return readString(Paths.get("./src/test/resources", names));
647-
}
648645
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of android-tree-sitter.
3+
*
4+
* android-tree-sitter library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* android-tree-sitter library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with android-tree-sitter. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.itsaky.androidide.treesitter;
19+
20+
import static com.itsaky.androidide.treesitter.TestUtils.readString;
21+
22+
import java.nio.file.Paths;
23+
24+
/**
25+
* @author Akash Yadav
26+
*/
27+
public class ResourceUtils {
28+
29+
private ResourceUtils() {
30+
throw new UnsupportedOperationException();
31+
}
32+
33+
public static String readResource(String... names) {
34+
return readString(Paths.get("./src/test/resources", names));
35+
}
36+
}

android-tree-sitter/src/test/java/com/itsaky/androidide/treesitter/UTF16StringTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.itsaky.androidide.treesitter;
1919

2020
import static com.google.common.truth.Truth.assertThat;
21+
import static com.itsaky.androidide.treesitter.ResourceUtils.readResource;
2122

2223
import com.itsaky.androidide.treesitter.string.UTF16String;
2324
import com.itsaky.androidide.treesitter.string.UTF16StringFactory;
@@ -182,4 +183,22 @@ public void testInsertAtLength() {
182183
assertThat(str.toString()).isEqualTo("Hello World!");
183184
str.close();
184185
}
186+
187+
@Test
188+
public void testCharIteration() {
189+
try (final var str = UTF16StringFactory.newString(readResource("View.java.txt"))) {
190+
assertThat(str).isNotNull();
191+
str.forEachChar(c -> {});
192+
str.forEachChar(100, 1000, c -> {});
193+
}
194+
}
195+
196+
@Test
197+
public void testByteIteration() {
198+
try (final var str = UTF16StringFactory.newString(readResource("View.java.txt"))) {
199+
assertThat(str).isNotNull();
200+
str.forEachByte(b -> {});
201+
str.forEachByte(100, 1000, b -> {});
202+
}
203+
}
185204
}

app/src/main/java/com/itsaky/androidide/androidtreesitter/MainActivity.kt

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,39 @@ class MainActivity : AppCompatActivity() {
8181
private var parseCount = 0L
8282
private var avgParse = 0L
8383

84+
companion object {
85+
86+
private const val TAG = "MainActivity"
87+
private const val DEF_ITERS = 1000
88+
89+
private val languageMap = hashMapOf<String, TSLanguage>()
90+
91+
init {
92+
TreeSitter.loadLibrary()
93+
94+
System.loadLibrary("tree-sitter-java")
95+
System.loadLibrary("tree-sitter-json")
96+
System.loadLibrary("tree-sitter-kotlin")
97+
System.loadLibrary("tree-sitter-log")
98+
System.loadLibrary("tree-sitter-python")
99+
System.loadLibrary("tree-sitter-xml")
100+
101+
languageMap["Java"] = TSLanguageJava.getInstance()
102+
languageMap["JSON"] = TSLanguageJson.getInstance()
103+
languageMap["Kotlin"] = TSLanguageKotlin.getInstance()
104+
languageMap["Log"] = TSLanguageLog.getInstance()
105+
languageMap["Python"] = TSLanguagePython.getInstance()
106+
languageMap["XML"] = TSLanguageXml.getInstance()
107+
}
108+
109+
private fun StringBuilder.repeatKt(text: String, indent: Int
110+
): StringBuilder {
111+
for (i in 1..indent) append(text)
112+
return this
113+
}
114+
}
115+
116+
84117
public override fun onCreate(savedInstanceState: Bundle?) {
85118
super.onCreate(savedInstanceState)
86119
val binding = ActivityMainBinding.inflate(layoutInflater)
@@ -202,6 +235,9 @@ class MainActivity : AppCompatActivity() {
202235

203236
// byteLength
204237
string.byteLength()
238+
239+
string.forEachChar { }
240+
string.forEachByte { }
205241
}
206242

207243
val duration = System.currentTimeMillis() - start
@@ -221,7 +257,7 @@ class MainActivity : AppCompatActivity() {
221257
Total duration: ${totalDuration / 1000} seconds
222258
""".trimIndent())
223259
}
224-
}
260+
}.logOnErr("doStringPerfTest")
225261
}
226262

227263
@Suppress("DEPRECATION")
@@ -293,7 +329,7 @@ class MainActivity : AppCompatActivity() {
293329
""".trimIndent())
294330
}
295331
}
296-
}
332+
}.logOnErr("doParserPerfTest")
297333
}
298334

299335
private inline fun viewJavaTxt(syncString: Boolean = true,
@@ -452,6 +488,14 @@ class MainActivity : AppCompatActivity() {
452488
}
453489
}
454490

491+
private fun Job.logOnErr(name: String) {
492+
invokeOnCompletion { err ->
493+
if (err != null) {
494+
Log.e(TAG, "logOnErr: Failed to run job: $name", err)
495+
}
496+
}
497+
}
498+
455499
private fun trace(err: Throwable): String {
456500
val sw = StringWriter()
457501
err.printStackTrace(PrintWriter(sw))
@@ -470,35 +514,4 @@ class MainActivity : AppCompatActivity() {
470514
) {
471515
}
472516
}
473-
474-
companion object {
475-
476-
private const val DEF_ITERS = 1000
477-
478-
private val languageMap = hashMapOf<String, TSLanguage>()
479-
480-
init {
481-
TreeSitter.loadLibrary()
482-
483-
System.loadLibrary("tree-sitter-java")
484-
System.loadLibrary("tree-sitter-json")
485-
System.loadLibrary("tree-sitter-kotlin")
486-
System.loadLibrary("tree-sitter-log")
487-
System.loadLibrary("tree-sitter-python")
488-
System.loadLibrary("tree-sitter-xml")
489-
490-
languageMap["Java"] = TSLanguageJava.getInstance()
491-
languageMap["JSON"] = TSLanguageJson.getInstance()
492-
languageMap["Kotlin"] = TSLanguageKotlin.getInstance()
493-
languageMap["Log"] = TSLanguageLog.getInstance()
494-
languageMap["Python"] = TSLanguagePython.getInstance()
495-
languageMap["XML"] = TSLanguageXml.getInstance()
496-
}
497-
498-
private fun StringBuilder.repeatKt(text: String, indent: Int
499-
): StringBuilder {
500-
for (i in 1..indent) append(text)
501-
return this
502-
}
503-
}
504517
}

0 commit comments

Comments
 (0)