Skip to content

Commit

Permalink
Merge pull request #3 from js-labs/datablock-wr-order
Browse files Browse the repository at this point in the history
Both DataBlock.rd and DataBlock.wr inherits byte order from the source byte buffer.
  • Loading branch information
js-labs authored Mar 30, 2024
2 parents 6ceb6b4 + 604c211 commit e791f2d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
9 changes: 7 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<sequential>
<property name="testName" value=""/>
<get-test-name propertyName="testName"/>
<java classname="org.jsl.tests.${testName}.Main" fork="true">
<java classname="org.jsl.tests.${testName}.Main" fork="true" failonerror="true">
<classpath>
<pathelement location="${build}/classes"/>
<pathelement location="${build}/tests"/>
Expand Down Expand Up @@ -191,6 +191,10 @@
<run-test/>
</target>

<target name="test.unit" depends="compile_tests">
<run-test/>
</target>

<target name="tests"
depends="test.buffer_overlap_copy,
test.byte_buffer_pool,
Expand All @@ -209,5 +213,6 @@
test.sched_latency,
test.thread_pool,
test.thread_pool_throughput,
test.timer_queue"/>
test.timer_queue,
test.unit"/>
</project>
1 change: 1 addition & 0 deletions src/main/java/org/jsl/collider/DataBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public DataBlock(ByteBuffer buf)
{
wr = buf;
rd = buf.duplicate();
rd.order(buf.order());
}

public final DataBlock reset()
Expand Down
61 changes: 61 additions & 0 deletions tests/src/org/jsl/tests/unit/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2013 Sergey Zubarev, info@js-labs.org
*
* This file is a part of JS-Collider test.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.jsl.tests.unit;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.jsl.collider.DataBlock;

interface TestFunc {
void run() throws Exception;
}

public class Main {
private static void dataBlockInheritsByteOrder() throws Exception {
final ByteOrder [] orders = {ByteOrder.LITTLE_ENDIAN, ByteOrder.BIG_ENDIAN};
for (ByteOrder byteOrder: orders) {
final ByteBuffer byteBuffer = ByteBuffer.allocateDirect(32);
byteBuffer.order(byteOrder);
final DataBlock dataBlock = new DataBlock(byteBuffer);
if (dataBlock.rd.order() != byteOrder) {
throw new Exception("wrong dataBlock.rd.order()");
}
if (dataBlock.wr.order() != byteOrder) {
throw new Exception("wrong dataBlock.wr.order()");
}
}
}

private static int runTest(TestFunc testFunc) {
try {
testFunc.run();
return 0;
} catch (final Exception ex) {
System.out.println(ex.getMessage());
return 1;
}
}

public static void main(String [] args) {
int failedTests = 0;
failedTests += runTest(Main::dataBlockInheritsByteOrder);
System.out.println(failedTests + " tests failed");
System.exit((failedTests == 0) ? 0 : -1);
}
}
12 changes: 12 additions & 0 deletions tests/tests.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module" module-name="js-collider" />
</component>
</module>

0 comments on commit e791f2d

Please sign in to comment.