| 1 |
/* |
| 2 |
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/> |
| 3 |
* (C) 2020 Vladimir Sadovnikov <[email protected]> |
| 4 |
* |
| 5 |
* This file is part of lsp-runtime-lib |
| 6 |
* Created on: 30 авг. 2019 г. |
| 7 |
* |
| 8 |
* lsp-runtime-lib is free software: you can redistribute it and/or modify |
| 9 |
* it under the terms of the GNU Lesser General Public License as published by |
| 10 |
* the Free Software Foundation, either version 3 of the License, or |
| 11 |
* any later version. |
| 12 |
* |
| 13 |
* lsp-runtime-lib is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
* GNU Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public License |
| 19 |
* along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <lsp-plug.in/test-fw/utest.h> |
| 23 |
#include <lsp-plug.in/fmt/java/const.h> |
| 24 |
#include <lsp-plug.in/fmt/java/Handles.h> |
| 25 |
#include <lsp-plug.in/fmt/java/ObjectStream.h> |
| 26 |
|
| 27 |
UTEST_BEGIN("runtime.fmt", java) |
| 28 |
|
| 29 |
#pragma pack(push, 1) |
| 30 |
union tptype |
| 31 |
{ |
| 32 |
java::byte_t v_byte; |
| 33 |
java::short_t v_short; |
| 34 |
java::int_t v_int; |
| 35 |
java::long_t v_long; |
| 36 |
java::char_t v_char; |
| 37 |
java::double_t v_double; |
| 38 |
java::float_t v_float; |
| 39 |
java::bool_t v_bool; |
| 40 |
java::Object *v_object; |
| 41 |
}; |
| 42 |
#pragma pack(pop) |
| 43 |
|
| 44 |
void read_jdk8_file() |
| 45 |
{ |
| 46 |
// Open file |
| 47 |
java::Handles handles; |
| 48 |
java::ObjectStream os(&handles); |
| 49 |
io::Path path; |
| 50 |
|
| 51 |
UTEST_ASSERT(path.set(resources()) == STATUS_OK); |
| 52 |
UTEST_ASSERT(path.append_child("fmt/java/test-jdk8.bin") == STATUS_OK); |
| 53 |
printf("Reading JDK8 file: %s\n", path.as_native()); |
| 54 |
UTEST_ASSERT(os.open(&path) == STATUS_OK); |
| 55 |
UTEST_ASSERT(os.version() == 5); |
| 56 |
|
| 57 |
uint8_t u8; |
| 58 |
uint32_t u32; |
| 59 |
LSPString lstr; |
| 60 |
LSPString tmp; |
| 61 |
java::String *jstr = NULL; |
| 62 |
java::RawArray *arr = NULL; |
| 63 |
|
| 64 |
// Read block data |
| 65 |
UTEST_ASSERT(os.current_token() == java::JST_BLOCK_DATA); |
| 66 |
UTEST_ASSERT(os.read_int(&u32) == STATUS_OK); |
| 67 |
UTEST_ASSERT(u32 == 0x11332244); |
| 68 |
UTEST_ASSERT(os.current_token() == java::JST_BLOCK_DATA); |
| 69 |
UTEST_ASSERT(os.read_byte(&u8) == STATUS_OK); |
| 70 |
UTEST_ASSERT(u8 == 0xce); |
| 71 |
UTEST_ASSERT(os.current_token() == java::JST_BLOCK_DATA); |
| 72 |
UTEST_ASSERT(os.read_utf(&lstr) == STATUS_OK); |
| 73 |
UTEST_ASSERT(lstr.equals_ascii("writeUTF string")); |
| 74 |
UTEST_ASSERT(os.current_token() == java::JST_STRING); |
| 75 |
UTEST_ASSERT(os.read_string(&jstr) == STATUS_OK); |
| 76 |
UTEST_ASSERT(jstr->string()->equals_ascii("writeObject string")); |
| 77 |
UTEST_ASSERT(jstr->to_string(&tmp) == STATUS_OK); |
| 78 |
printf("Read string: %s", tmp.get_utf8()); |
| 79 |
jstr = NULL; |
| 80 |
tmp.clear(); |
| 81 |
|
| 82 |
// Read array |
| 83 |
UTEST_ASSERT(os.current_token() == java::JST_ARRAY); |
| 84 |
UTEST_ASSERT(os.read_array(&arr) == STATUS_OK); |
| 85 |
UTEST_ASSERT(arr->to_string(&tmp) == STATUS_OK); |
| 86 |
printf("Read array: %s", tmp.get_utf8()); |
| 87 |
UTEST_ASSERT(arr->length() == 2); |
| 88 |
|
| 89 |
const java::Object **vobj = arr->get_objects(); |
| 90 |
UTEST_ASSERT(vobj[0] != NULL); |
| 91 |
UTEST_ASSERT(vobj[1] != NULL); |
| 92 |
|
| 93 |
// Check item[0] |
| 94 |
const java::Object *obj = vobj[0]; |
| 95 |
UTEST_ASSERT(::strcmp(obj->class_name(), "test.TestObject") == 0); |
| 96 |
|
| 97 |
tptype x; |
| 98 |
UTEST_ASSERT(obj->get_bool("xbool", &x.v_bool) == STATUS_OK); |
| 99 |
UTEST_ASSERT(bool(x.v_bool) == false); |
| 100 |
UTEST_ASSERT(obj->get_byte("xbyte", &x.v_byte) == STATUS_OK); |
| 101 |
UTEST_ASSERT(x.v_byte == java::byte_t(0x5a)); |
| 102 |
UTEST_ASSERT(obj->get_char("xchar", &x.v_char) == STATUS_OK); |
| 103 |
UTEST_ASSERT(x.v_char == 'X'); |
| 104 |
UTEST_ASSERT(obj->get_double("xdouble", &x.v_double) == STATUS_OK); |
| 105 |
UTEST_ASSERT(x.v_double == 440.0); |
| 106 |
UTEST_ASSERT(obj->get_float("xfloat", &x.v_float) == STATUS_OK); |
| 107 |
UTEST_ASSERT(x.v_float == 48000.0f); |
| 108 |
UTEST_ASSERT(obj->get_int("xint", &x.v_int) == STATUS_OK); |
| 109 |
UTEST_ASSERT(x.v_int == 0x10203040); |
| 110 |
UTEST_ASSERT(obj->get_long("xlong", &x.v_long) == STATUS_OK); |
| 111 |
UTEST_ASSERT(x.v_long == java::long_t(0x1122334455667788L)); |
| 112 |
UTEST_ASSERT(obj->get_short("xshort", &x.v_short) == STATUS_OK); |
| 113 |
UTEST_ASSERT(x.v_short == 0x55aa); |
| 114 |
|
| 115 |
const java::RawArray *narr = NULL; |
| 116 |
UTEST_ASSERT(obj->get_array("iarray", &narr) == STATUS_OK); |
| 117 |
UTEST_ASSERT(narr != NULL); |
| 118 |
UTEST_ASSERT(narr->length() == 8); |
| 119 |
|
| 120 |
const java::int_t* ints = narr->get_ints(); |
| 121 |
for (size_t i=0; i<8; ++i) |
| 122 |
UTEST_ASSERT(ints[i] == java::int_t(i+1)); |
| 123 |
|
| 124 |
UTEST_ASSERT(obj->get_string("iref", &tmp) == STATUS_OK); |
| 125 |
UTEST_ASSERT(tmp.equals_ascii("Referenced string")); |
| 126 |
UTEST_ASSERT(obj->get_object("inull", NULL) == STATUS_NULL); |
| 127 |
UTEST_ASSERT(obj->get_enum("xenum", &tmp) == STATUS_OK); |
| 128 |
UTEST_ASSERT(tmp.equals_ascii("ONE")); |
| 129 |
|
| 130 |
// Check item[1] |
| 131 |
obj = vobj[1]; |
| 132 |
UTEST_ASSERT(::strcmp(obj->class_name(), "test.TestObject") == 0); |
| 133 |
|
| 134 |
UTEST_ASSERT(obj->get_bool("xbool", &x.v_bool) == STATUS_OK); |
| 135 |
UTEST_ASSERT(bool(x.v_bool) == true); |
| 136 |
UTEST_ASSERT(obj->get_byte("xbyte", &x.v_byte) == STATUS_OK); |
| 137 |
UTEST_ASSERT(x.v_byte == java::byte_t(0xa5)); |
| 138 |
UTEST_ASSERT(obj->get_char("xchar", &x.v_char) == STATUS_OK); |
| 139 |
UTEST_ASSERT(x.v_char == 'Y'); |
| 140 |
UTEST_ASSERT(obj->get_double("xdouble", &x.v_double) == STATUS_OK); |
| 141 |
UTEST_ASSERT(x.v_double == 880.0); |
| 142 |
UTEST_ASSERT(obj->get_float("xfloat", &x.v_float) == STATUS_OK); |
| 143 |
UTEST_ASSERT(x.v_float == 41000.0f); |
| 144 |
UTEST_ASSERT(obj->get_int("xint", &x.v_int) == STATUS_OK); |
| 145 |
UTEST_ASSERT(x.v_int == 0x20304050); |
| 146 |
UTEST_ASSERT(obj->get_long("xlong", &x.v_long) == STATUS_OK); |
| 147 |
UTEST_ASSERT(x.v_long == java::long_t(0x5566778811223344L)); |
| 148 |
UTEST_ASSERT(obj->get_short("xshort", &x.v_short) == STATUS_OK); |
| 149 |
UTEST_ASSERT(x.v_short == java::short_t(0xaa55)); |
| 150 |
|
| 151 |
UTEST_ASSERT(obj->get_array("iarray", &narr) == STATUS_OK); |
| 152 |
UTEST_ASSERT(narr != NULL); |
| 153 |
UTEST_ASSERT(narr->length() == 4); |
| 154 |
|
| 155 |
ints = narr->get_ints(); |
| 156 |
for (size_t i=0; i<4; ++i) |
| 157 |
UTEST_ASSERT(ints[i] == java::int_t(5-i)); |
| 158 |
|
| 159 |
UTEST_ASSERT(obj->get_array("iref", &narr) == STATUS_OK); |
| 160 |
UTEST_ASSERT(narr != NULL); |
| 161 |
UTEST_ASSERT(narr->length() == 6); |
| 162 |
|
| 163 |
UTEST_ASSERT(obj->get_enum("inull", &tmp) == STATUS_OK); |
| 164 |
UTEST_ASSERT(tmp.equals_ascii("FOUR")); |
| 165 |
UTEST_ASSERT(obj->get_enum("xenum", &tmp) == STATUS_OK); |
| 166 |
UTEST_ASSERT(tmp.equals_ascii("TWO")); |
| 167 |
|
| 168 |
// Close file |
| 169 |
UTEST_ASSERT(os.close() == STATUS_OK); |
| 170 |
handles.flush(); |
| 171 |
} |
| 172 |
|
| 173 |
void read_rew_file() |
| 174 |
{ |
| 175 |
// Open file |
| 176 |
java::Handles handles; |
| 177 |
java::ObjectStream os(&handles); |
| 178 |
io::Path path; |
| 179 |
|
| 180 |
UTEST_ASSERT(path.set(resources()) == STATUS_OK); |
| 181 |
UTEST_ASSERT(path.append_child("fmt/rew/Test11.req") == STATUS_OK); |
| 182 |
printf("Reading REW file: %s\n", path.as_native()); |
| 183 |
UTEST_ASSERT(os.open(&path) == STATUS_OK); |
| 184 |
UTEST_ASSERT(os.version() == 5); |
| 185 |
|
| 186 |
java::String *str = NULL; |
| 187 |
java::Object *array = NULL; |
| 188 |
uint32_t value = 0; |
| 189 |
LSPString tmp; |
| 190 |
|
| 191 |
// Perform read |
| 192 |
UTEST_ASSERT(os.current_token() == java::JST_STRING); |
| 193 |
UTEST_ASSERT(os.read_string(&str) == STATUS_OK); |
| 194 |
UTEST_ASSERT(str->string()->equals_ascii("TMreq Filters File:Equaliser:Generic")); |
| 195 |
str = NULL; |
| 196 |
|
| 197 |
UTEST_ASSERT(os.current_token() == java::JST_BLOCK_DATA); |
| 198 |
UTEST_ASSERT(os.read_int(&value) == STATUS_OK); |
| 199 |
UTEST_ASSERT(value == 5); |
| 200 |
|
| 201 |
UTEST_ASSERT(os.current_token() == java::JST_BLOCK_DATA); |
| 202 |
UTEST_ASSERT(os.read_int(&value) == STATUS_OK); |
| 203 |
UTEST_ASSERT(value == 19); |
| 204 |
|
| 205 |
UTEST_ASSERT(os.current_token() == java::JST_STRING); |
| 206 |
UTEST_ASSERT(os.read_string(&str) == STATUS_OK); |
| 207 |
UTEST_ASSERT(str->string()->equals_ascii("Notes:test notes")); |
| 208 |
str = NULL; |
| 209 |
|
| 210 |
UTEST_ASSERT(os.current_token() == java::JST_BLOCK_DATA); |
| 211 |
UTEST_ASSERT(os.read_int(&value) == STATUS_OK); |
| 212 |
UTEST_ASSERT(value == 1); |
| 213 |
|
| 214 |
UTEST_ASSERT(os.current_token() == java::JST_ARRAY); |
| 215 |
UTEST_ASSERT(os.read_object(&array) == STATUS_OK); |
| 216 |
|
| 217 |
UTEST_ASSERT(array->to_string(&tmp) == STATUS_OK); |
| 218 |
printf("Read array: %s", tmp.get_utf8()); |
| 219 |
|
| 220 |
// Close file |
| 221 |
UTEST_ASSERT(os.close() == STATUS_OK); |
| 222 |
handles.flush(); |
| 223 |
} |
| 224 |
|
| 225 |
UTEST_MAIN |
| 226 |
{ |
| 227 |
printf("Testing JDK8 file...\n"); |
| 228 |
read_jdk8_file(); |
| 229 |
|
| 230 |
printf("Testing REW file...\n"); |
| 231 |
read_rew_file(); |
| 232 |
} |
| 233 |
|
| 234 |
UTEST_END |
| 235 |
|
| 236 |
|