forked from kherud/java-llama.cpp
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLlamaLoaderTest.java
More file actions
259 lines (221 loc) · 10.8 KB
/
Copy pathLlamaLoaderTest.java
File metadata and controls
259 lines (221 loc) · 10.8 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
// SPDX-FileCopyrightText: 2026 Bernard Ladenthin <bernard.ladenthin@gmail.com>
// SPDX-FileCopyrightText: 2023-2025 Konstantin Herud
//
// SPDX-License-Identifier: MIT
package net.ladenthin.llama.loader;
import static org.junit.jupiter.api.Assertions.*;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import net.ladenthin.llama.ClaudeGenerated;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
@ClaudeGenerated(
purpose = "Verify the helper statics extracted from LlamaLoader without requiring any "
+ "native library: shouldCleanPath detects jllama/llama-prefixed files for "
+ "cleanup; contentsEquals performs a correct byte-level stream comparison "
+ "including BufferedInputStream wrapping and length mismatches; getTempDir "
+ "honours the 'net.ladenthin.llama.tmpdir' system-property override; and "
+ "getNativeResourcePath produces the expected classpath resource prefix; and "
+ "resourceMatchesFile compares a classpath resource to an on-disk file byte-for-byte.")
public class LlamaLoaderTest {
private static final String TMPDIR_PROP = LlamaSystemProperties.PREFIX + ".tmpdir";
/** A small file present on the test classpath, used as a byte-comparison fixture. */
private static final String EXISTING_TEST_RESOURCE = "/images/test-image.jpg";
private String previousTmpDir;
@BeforeEach
public void saveTmpDirProp() {
previousTmpDir = System.getProperty(TMPDIR_PROP);
}
@AfterEach
public void restoreTmpDirProp() {
if (previousTmpDir == null) {
System.clearProperty(TMPDIR_PROP);
} else {
System.setProperty(TMPDIR_PROP, previousTmpDir);
}
}
// -------------------------------------------------------------------------
// shouldCleanPath
// -------------------------------------------------------------------------
@Test
public void testShouldCleanPathJllamaPrefix() {
assertTrue(LlamaLoader.shouldCleanPath(Paths.get("/tmp/jllama.so")));
}
@Test
public void testShouldCleanPathJllamaWithSuffix() {
assertTrue(LlamaLoader.shouldCleanPath(Paths.get("/tmp/jllama-abc123.dylib")));
}
@Test
public void testShouldCleanPathLlamaPrefix() {
assertTrue(LlamaLoader.shouldCleanPath(Paths.get("/tmp/llama.dll")));
}
@Test
public void testShouldCleanPathLlamaWithSuffix() {
assertTrue(LlamaLoader.shouldCleanPath(Paths.get("/tmp/llama-model.so")));
}
@Test
public void testShouldCleanPathUnrelatedFile() {
assertFalse(LlamaLoader.shouldCleanPath(Paths.get("/tmp/somefile.so")));
}
@Test
public void testShouldCleanPathEmptyFilename() {
assertFalse(LlamaLoader.shouldCleanPath(Paths.get("/tmp/")));
}
@Test
public void testShouldCleanPathPartialMatchInMiddle() {
// "myJllama" does not start with "jllama" so should not be cleaned
assertFalse(LlamaLoader.shouldCleanPath(Paths.get("/tmp/myjllama.so")));
}
@Test
public void testShouldCleanPathCaseSensitive() {
// "Jllama" does not start with lowercase "jllama"
assertFalse(LlamaLoader.shouldCleanPath(Paths.get("/tmp/Jllama.so")));
}
// -------------------------------------------------------------------------
// contentsEquals
// -------------------------------------------------------------------------
@Test
public void testContentsEqualsIdenticalContent() throws IOException {
byte[] data = {1, 2, 3, 4, 5};
assertTrue(LlamaLoader.contentsEquals(new ByteArrayInputStream(data), new ByteArrayInputStream(data)));
}
@Test
public void resourceMatchesFileFalseWhenResourceAbsent() throws IOException {
java.nio.file.Path tmp = java.nio.file.Files.createTempFile("llama-loader-test", ".bin");
try {
java.nio.file.Files.write(tmp, new byte[] {1, 2, 3});
// A missing classpath resource must compare as "not matching", never throw.
assertFalse(LlamaLoader.resourceMatchesFile("/net/ladenthin/llama/does-not-exist.bin", tmp));
} finally {
java.nio.file.Files.deleteIfExists(tmp);
}
}
@Test
public void resourceMatchesFileTrueWhenBytesIdentical() throws IOException {
// The fast-path reuse predicate: a present resource and a byte-identical on-disk copy match.
java.nio.file.Path tmp = java.nio.file.Files.createTempFile("llama-loader-test", ".bin");
try {
try (java.io.InputStream in = LlamaLoader.class.getResourceAsStream(EXISTING_TEST_RESOURCE)) {
assertNotNull(in, "fixture must be on the test classpath: " + EXISTING_TEST_RESOURCE);
java.nio.file.Files.copy(in, tmp, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
}
assertTrue(LlamaLoader.resourceMatchesFile(EXISTING_TEST_RESOURCE, tmp));
} finally {
java.nio.file.Files.deleteIfExists(tmp);
}
}
@Test
public void resourceMatchesFileFalseWhenContentDiffers() throws IOException {
// A present resource whose on-disk copy diverges (here: one extra trailing byte) must NOT match,
// so a stale/partial file is never mistaken for the shipped library on the reuse fast path.
java.nio.file.Path tmp = java.nio.file.Files.createTempFile("llama-loader-test", ".bin");
try {
try (java.io.InputStream in = LlamaLoader.class.getResourceAsStream(EXISTING_TEST_RESOURCE)) {
assertNotNull(in, "fixture must be on the test classpath: " + EXISTING_TEST_RESOURCE);
java.nio.file.Files.copy(in, tmp, java.nio.file.StandardCopyOption.REPLACE_EXISTING);
}
java.nio.file.Files.write(tmp, new byte[] {0}, java.nio.file.StandardOpenOption.APPEND);
assertFalse(LlamaLoader.resourceMatchesFile(EXISTING_TEST_RESOURCE, tmp));
} finally {
java.nio.file.Files.deleteIfExists(tmp);
}
}
@Test
public void testContentsEqualsBothEmpty() throws IOException {
assertTrue(LlamaLoader.contentsEquals(
new ByteArrayInputStream(new byte[0]), new ByteArrayInputStream(new byte[0])));
}
@Test
public void testContentsEqualsDifferentContent() throws IOException {
assertFalse(LlamaLoader.contentsEquals(
new ByteArrayInputStream(new byte[] {1, 2, 3}), new ByteArrayInputStream(new byte[] {1, 2, 4})));
}
@Test
public void testContentsEqualsFirstLonger() throws IOException {
assertFalse(LlamaLoader.contentsEquals(
new ByteArrayInputStream(new byte[] {1, 2, 3}), new ByteArrayInputStream(new byte[] {1, 2})));
}
@Test
public void testContentsEqualsSecondLonger() throws IOException {
assertFalse(LlamaLoader.contentsEquals(
new ByteArrayInputStream(new byte[] {1, 2}), new ByteArrayInputStream(new byte[] {1, 2, 3})));
}
@Test
public void testContentsEqualsAlreadyBuffered() throws IOException {
// Passes BufferedInputStreams directly — should not double-wrap
byte[] data = {10, 20, 30};
assertTrue(LlamaLoader.contentsEquals(
new BufferedInputStream(new ByteArrayInputStream(data)),
new BufferedInputStream(new ByteArrayInputStream(data))));
}
@Test
public void testContentsEqualsDifferentAtFirstByte() throws IOException {
assertFalse(LlamaLoader.contentsEquals(
new ByteArrayInputStream(new byte[] {0}), new ByteArrayInputStream(new byte[] {1})));
}
@Test
public void testContentsEqualsSingleByteMatch() throws IOException {
assertTrue(LlamaLoader.contentsEquals(
new ByteArrayInputStream(new byte[] {42}), new ByteArrayInputStream(new byte[] {42})));
}
// -------------------------------------------------------------------------
// getTempDir
// -------------------------------------------------------------------------
@Test
public void testGetTempDirDefaultsToJavaIoTmpdir() {
System.clearProperty(TMPDIR_PROP);
File expected = new File(System.getProperty("java.io.tmpdir"));
assertEquals(expected, LlamaLoader.getTempDir());
}
@Test
public void testGetTempDirUsesOverrideProperty() {
// Build path with platform separator so File.getPath() round-trips correctly
String customPath = new File(System.getProperty("java.io.tmpdir"), "llama-test-custom").getPath();
System.setProperty(TMPDIR_PROP, customPath);
assertEquals(new File(customPath), LlamaLoader.getTempDir());
}
// -------------------------------------------------------------------------
// getNativeResourcePath
// -------------------------------------------------------------------------
@Test
public void testGetNativeResourcePathStartsWithSlash() {
String path = LlamaLoader.getNativeResourcePath();
assertTrue(path.startsWith("/"), "Resource path should start with '/'");
}
@Test
public void testGetNativeResourcePathContainsPackage() {
String path = LlamaLoader.getNativeResourcePath();
// Package net.ladenthin.llama maps to net/ladenthin/llama
assertTrue(path.contains("net/ladenthin/llama"), "Resource path should contain package");
}
@Test
public void testGetNativeResourcePathContainsOsAndArch() {
String path = LlamaLoader.getNativeResourcePath();
// Should end with OS/arch from OSInfo
String osArch = OSInfo.getNativeLibFolderPathForCurrentOS();
assertTrue(path.endsWith(osArch), "Resource path should end with OS/arch: " + path);
}
/**
* Regression for the layered-restructure bug: the native-library classpath
* root is fixed at {@code /net/ladenthin/llama/<os>/<arch>} by CMakeLists +
* the publish workflow, so it must NOT track the loader's own Java package
* (which moved to {@code net.ladenthin.llama.loader}). Deriving it from
* {@code LlamaLoader.class.getPackage()} produced {@code .../llama/loader/...},
* one level too deep, so {@code getResource(...)} returned null and every
* native-backed test failed with "No native library found".
*/
@Test
public void testGetNativeResourcePathIsPackageIndependent() {
String path = LlamaLoader.getNativeResourcePath();
String osArch = OSInfo.getNativeLibFolderPathForCurrentOS();
assertEquals("/net/ladenthin/llama/" + osArch, path);
assertFalse(
path.contains("/loader/"),
"Resource path must not include the loader subpackage — the native libs live at "
+ "/net/ladenthin/llama/<os>/<arch>, not under the loader package: " + path);
}
}