diff --git a/app/build.gradle b/app/build.gradle
index a2c1fcda..c3f04c47 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -21,6 +21,7 @@ android {
minSdkVersion 14
targetSdkVersion 22
multiDexEnabled true
+ testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
packagingOptions {
@@ -54,7 +55,12 @@ dependencies {
compile project(':ethereumj-core-android')
compile 'com.android.support:multidex:1.0.0'
compile fileTree(include: ['*.jar'], dir: '../libraries')
- compile 'com.android.support:support-v4:22.1.1'
- compile 'com.android.support:appcompat-v7:22.1.1'
- compile 'com.android.support:recyclerview-v7:22.1.1'
+ compile 'com.android.support:support-v4:22.2.0'
+ compile 'com.android.support:appcompat-v7:22.2.0'
+ compile 'com.android.support:recyclerview-v7:22.2.0'
+
+ androidTestCompile 'com.android.support.test:runner:0.3'
+ // Set this dependency to use JUnit 4 rules
+ androidTestCompile 'com.android.support.test:rules:0.3'
+
}
\ No newline at end of file
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 4e667f73..a5870d5a 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -2,15 +2,19 @@
+
+
+
+
@@ -18,8 +22,10 @@
+
+
-
-
-
-
\ No newline at end of file
+
+
diff --git a/app/src/main/java/org/ethereum/android_app/MainActivity.java b/app/src/main/java/org/ethereum/android_app/MainActivity.java
index 1838048e..d13ee787 100644
--- a/app/src/main/java/org/ethereum/android_app/MainActivity.java
+++ b/app/src/main/java/org/ethereum/android_app/MainActivity.java
@@ -95,7 +95,9 @@ public class MainActivity extends ActionBarActivity {
@Override
protected void onDestroy() {
super.onDestroy();
- ethereumManager.onDestroy();
+ if (ethereumManager != null) {
+ ethereumManager.onDestroy();
+ }
}
// The definition of our task class
@@ -112,7 +114,14 @@ public class MainActivity extends ActionBarActivity {
Log.v(TAG, "111");
Log.v(TAG, "222");
- long duration = ethereumManager.connect(SystemProperties.CONFIG.databaseDir() + File.separator + "poc-9-492k.dmp");
+ ThreadGroup group = new ThreadGroup("threadGroup");
+ new Thread(group, new Runnable() {
+ @Override
+ public void run() {
+ long duration = ethereumManager.connect(SystemProperties.CONFIG.databaseDir() + File.separator + "poc-9-492k.dmp");
+ }
+ }, "EthereumConnect", 32768000).start();
+
//ConsoleFragment consoleeFrag = (ConsoleFragment)getSupportFragmentManager().findFragmentById(R.id.console);
//consoleeFrag.updateDuration(duration);
Log.v(TAG, "333");
@@ -158,7 +167,9 @@ public class MainActivity extends ActionBarActivity {
protected String doInBackground(Context... params) {
Log.v(TAG, "444");
try {
- ethereumManager.startJsonRpc();
+ if (ethereumManager != null) {
+ ethereumManager.startJsonRpc();
+ }
} catch (Exception e) {
}
Log.v(TAG, "555");
diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml
index 27233ab7..10f43447 100644
--- a/app/src/main/res/values/dimens.xml
+++ b/app/src/main/res/values/dimens.xml
@@ -1,3 +1,6 @@
4dp
+
+ 16dp
+ 16dp
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index f6f2585e..32214cd2 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -6,5 +6,8 @@
Console Output:
EthereumJ Tests
+ TestActivity
+
+ Hello world!
diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java b/ethereumj-core-android/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java
index 267f4ec8..bfaf235d 100644
--- a/ethereumj-core-android/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java
+++ b/ethereumj-core-android/src/main/java/org/ethereum/android/datasource/LevelDbDataSource.java
@@ -22,8 +22,6 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
-import android.content.Context;
-
/**
* @author Roman Mandeleil
* @since 18.01.2015
@@ -34,7 +32,6 @@ public class LevelDbDataSource implements KeyValueDataSource {
String name;
private DB db;
- private Context context;
public LevelDbDataSource() {
}
@@ -43,10 +40,6 @@ public class LevelDbDataSource implements KeyValueDataSource {
this.name = name;
}
- public void setContext(Context context) {
- this.context = context;
- }
-
@Override
public void init() {
diff --git a/ethereumj-core-android/src/main/java/org/ethereum/android/di/modules/EthereumModule.java b/ethereumj-core-android/src/main/java/org/ethereum/android/di/modules/EthereumModule.java
index a4b94458..3dfb740b 100644
--- a/ethereumj-core-android/src/main/java/org/ethereum/android/di/modules/EthereumModule.java
+++ b/ethereumj-core-android/src/main/java/org/ethereum/android/di/modules/EthereumModule.java
@@ -83,9 +83,7 @@ public class EthereumModule {
@Singleton
Repository provideRepository() {
LevelDbDataSource detailsDS = new LevelDbDataSource();
- detailsDS.setContext(context);
LevelDbDataSource stateDS = new LevelDbDataSource();
- stateDS.setContext(context);
return new RepositoryImpl(detailsDS, stateDS);
}
diff --git a/ethereumj-core/build.gradle b/ethereumj-core/build.gradle
index 7f9b27dd..c9e70e88 100644
--- a/ethereumj-core/build.gradle
+++ b/ethereumj-core/build.gradle
@@ -150,6 +150,7 @@ dependencies {
compile "commons-dbcp:commons-dbcp:1.4"
compile "redis.clients:jedis:2.6.0"
compile "com.h2database:h2:1.4.187"
+ compile "org.mapdb:mapdb:1.0.7"
compile "org.slf4j:slf4j-log4j12:${slf4jVersion}"
compile "log4j:apache-log4j-extras:${log4jVersion}"
diff --git a/ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java b/ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java
index a0a96494..aaa1bfef 100644
--- a/ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java
+++ b/ethereumj-core/src/main/java/org/ethereum/config/SystemProperties.java
@@ -47,6 +47,8 @@ public class SystemProperties {
private final static String DEFAULT_KEY_VALUE_DATA_SOURCE = "leveldb";
private final static boolean DEFAULT_REDIS_ENABLED = true;
private static final String DEFAULT_BLOCKS_LOADER = "";
+ private static final int DEFAULT_FLUSH_BATCH_SIZE = 5_000;
+ private static final boolean DEFAULT_FLUSH_IGNORE_CONSENSUS = false;
/* Testing */
@@ -254,6 +256,19 @@ public class SystemProperties {
private boolean boolProperty(String key, Boolean defaultValue) {
return Boolean.parseBoolean(prop.getProperty(key, String.valueOf(defaultValue)));
}
+
+ private int intProperty(String key, int defaultValue) {
+ return Integer.parseInt(prop.getProperty(key, String.valueOf(defaultValue)));
+ }
+
+ public int flushBatchSize() {
+ return intProperty("flush.batch.size", DEFAULT_FLUSH_BATCH_SIZE);
+ }
+
+ public boolean flushIgnoreConsensus() {
+ return boolProperty("flush.ignore.consensus", DEFAULT_FLUSH_IGNORE_CONSENSUS);
+
+ }
public String vmTraceDir() {
return prop.getProperty("vm.structured.dir", DEFAULT_VM_TRACE_DIR);
diff --git a/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java b/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java
index 68b68ad6..d9107aa2 100644
--- a/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java
+++ b/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java
@@ -294,9 +294,7 @@ public class BlockchainImpl implements Blockchain {
track.commit();
storeBlock(block, receipts);
-
- if (adminInfo.isConsensus() &&
- block.getNumber() % 5_000 == 0) {
+ if (needFlush(block)) {
repository.flush();
blockStore.flush();
}
@@ -321,6 +319,13 @@ public class BlockchainImpl implements Blockchain {
}
}
+ private boolean needFlush(Block block) {
+ boolean isBatchReached = block.getNumber() % CONFIG.flushBatchSize() == 0;
+ boolean isConsensus = CONFIG.flushIgnoreConsensus() || adminInfo.isConsensus();
+
+ return isConsensus && isBatchReached;
+ }
+
private byte[] calcReceiptsTrie(List receipts){
//TODO Fix Trie hash for receipts - doesnt match cpp
Trie receiptsTrie = new TrieImpl(null);
diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Wallet.java b/ethereumj-core/src/main/java/org/ethereum/core/Wallet.java
index baa6b030..c004dbda 100644
--- a/ethereumj-core/src/main/java/org/ethereum/core/Wallet.java
+++ b/ethereumj-core/src/main/java/org/ethereum/core/Wallet.java
@@ -179,9 +179,6 @@ public class Wallet {
}
public void applyTransaction(Transaction transaction) {
-
- transactionMap.put(new ByteArrayWrapper(transaction.getHash()), transaction);
-
byte[] senderAddress = transaction.getSender();
Account sender = rows.get(Hex.toHexString(senderAddress));
if (sender != null) {
diff --git a/ethereumj-core/src/main/java/org/ethereum/datasource/LevelDbDataSource.java b/ethereumj-core/src/main/java/org/ethereum/datasource/LevelDbDataSource.java
index 413d1724..12761db4 100644
--- a/ethereumj-core/src/main/java/org/ethereum/datasource/LevelDbDataSource.java
+++ b/ethereumj-core/src/main/java/org/ethereum/datasource/LevelDbDataSource.java
@@ -2,8 +2,6 @@ package org.ethereum.datasource;
import org.ethereum.config.SystemProperties;
-import org.fusesource.leveldbjni.JniDBFactory;
-import org.fusesource.leveldbjni.internal.JniDB;
import org.iq80.leveldb.CompressionType;
import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBIterator;
@@ -64,23 +62,7 @@ public class LevelDbDataSource implements KeyValueDataSource {
}
logger.debug("Initializing new or existing database: '{}'", name);
-
- try {
- db = JniDBFactory.factory.open(fileLocation, options);
- } catch (Throwable e) {
- System.out.println("No native version of LevelDB found");
- }
-
- String cpu = System.getProperty("sun.arch.data.model");
- String os = System.getProperty("os.name");
-
- if (db instanceof JniDB)
- System.out.println("Native version of LevelDB loaded for: " + os + "." + cpu + "bit");
- else{
- System.out.println("Pure Java version of LevelDB loaded");
- db = Iq80DBFactory.factory.open(fileLocation, options);
- }
-
+ db = factory.open(fileLocation, options);
} catch (IOException ioe) {
logger.error(ioe.getMessage(), ioe);
diff --git a/ethereumj-core/src/main/java/org/ethereum/datasource/redis/RedisSet.java b/ethereumj-core/src/main/java/org/ethereum/datasource/redis/RedisSet.java
index 63882579..1bd0319e 100644
--- a/ethereumj-core/src/main/java/org/ethereum/datasource/redis/RedisSet.java
+++ b/ethereumj-core/src/main/java/org/ethereum/datasource/redis/RedisSet.java
@@ -1,5 +1,6 @@
package org.ethereum.datasource.redis;
+import org.apache.commons.collections4.CollectionUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@@ -74,7 +75,7 @@ public class RedisSet extends RedisStorage implements Set {
@Override
public boolean remove(Object o) {
- return removeAll(Arrays.asList(o));
+ return (o == null) || removeAll(Arrays.asList(o));
}
@Override
@@ -110,7 +111,7 @@ public class RedisSet extends RedisStorage implements Set {
@Override
public boolean removeAll(final Collection> c) {
- return pooledWithResult(new Function() {
+ return CollectionUtils.isEmpty(c) || pooledWithResult(new Function() {
@Override
public Boolean apply(Jedis jedis) {
return jedis.srem(getName(), serialize(c)) == c.size();
diff --git a/ethereumj-core/src/main/java/org/ethereum/datasource/redis/RedisStorage.java b/ethereumj-core/src/main/java/org/ethereum/datasource/redis/RedisStorage.java
index 5303c36f..8d16d758 100644
--- a/ethereumj-core/src/main/java/org/ethereum/datasource/redis/RedisStorage.java
+++ b/ethereumj-core/src/main/java/org/ethereum/datasource/redis/RedisStorage.java
@@ -15,7 +15,7 @@ import static org.ethereum.util.Functional.*;
public abstract class RedisStorage {
- private static final Logger logger = LoggerFactory.getLogger("db");
+ protected static final Logger log = LoggerFactory.getLogger("redis");
private byte[] name;
private final JedisPool pool;
diff --git a/ethereumj-core/src/main/java/org/ethereum/db/ByteArrayWrapper.java b/ethereumj-core/src/main/java/org/ethereum/db/ByteArrayWrapper.java
index 7c910e3d..a5716c31 100644
--- a/ethereumj-core/src/main/java/org/ethereum/db/ByteArrayWrapper.java
+++ b/ethereumj-core/src/main/java/org/ethereum/db/ByteArrayWrapper.java
@@ -4,13 +4,14 @@ import org.ethereum.util.FastByteComparisons;
import org.spongycastle.util.encoders.Hex;
+import java.io.Serializable;
import java.util.Arrays;
/**
* @author Roman Mandeleil
* @since 11.06.2014
*/
-public class ByteArrayWrapper implements Comparable {
+public class ByteArrayWrapper implements Comparable, Serializable {
private final byte[] data;
private int hashCode = 0;
diff --git a/ethereumj-core/src/main/java/org/ethereum/di/modules/EthereumModule.java b/ethereumj-core/src/main/java/org/ethereum/di/modules/EthereumModule.java
index 1239b070..c838b254 100644
--- a/ethereumj-core/src/main/java/org/ethereum/di/modules/EthereumModule.java
+++ b/ethereumj-core/src/main/java/org/ethereum/di/modules/EthereumModule.java
@@ -5,6 +5,7 @@ import org.ethereum.core.BlockchainImpl;
import org.ethereum.core.Wallet;
import org.ethereum.datasource.KeyValueDataSource;
import org.ethereum.datasource.LevelDbDataSource;
+import org.ethereum.datasource.mapdb.MapDBDataSource;
import org.ethereum.db.BlockStore;
import org.ethereum.db.InMemoryBlockStore;
import org.ethereum.db.RepositoryImpl;
@@ -76,8 +77,8 @@ public class EthereumModule {
@Provides
@Singleton
Repository provideRepository() {
- LevelDbDataSource detailsDS = new LevelDbDataSource();
- LevelDbDataSource stateDS = new LevelDbDataSource();
+ MapDBDataSource detailsDS = new MapDBDataSource();
+ MapDBDataSource stateDS = new MapDBDataSource();
return new RepositoryImpl(detailsDS, stateDS);
}
diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/Memory.java b/ethereumj-core/src/main/java/org/ethereum/vm/Memory.java
index 5c738415..abcd1d1c 100644
--- a/ethereumj-core/src/main/java/org/ethereum/vm/Memory.java
+++ b/ethereumj-core/src/main/java/org/ethereum/vm/Memory.java
@@ -53,20 +53,22 @@ public class Memory implements ProgramTraceListenerAware {
return data;
}
+ public void write(int address, byte[] data, int dataSize, boolean limited) {
- public void write(int address, byte[] data, boolean limited) {
+ if (data.length < dataSize)
+ dataSize = data.length;
if (!limited)
- extend(address, data.length);
+ extend(address, dataSize);
int chunkIndex = address / CHUNK_SIZE;
int chunkOffset = address % CHUNK_SIZE;
int toCapture = 0;
if (limited)
- toCapture = (address + data.length > softSize) ? softSize - address : data.length;
+ toCapture = (address + dataSize > softSize) ? softSize - address : dataSize;
else
- toCapture = data.length;
+ toCapture = dataSize;
int start = 0;
@@ -82,12 +84,12 @@ public class Memory implements ProgramTraceListenerAware {
start += captured;
}
- if (traceListener != null) traceListener.onMemoryWrite(address, data);
+ if (traceListener != null) traceListener.onMemoryWrite(address, data, dataSize);
}
public void extendAndWrite(int address, int allocSize, byte[] data) {
extend(address, allocSize);
- write(address, data, false);
+ write(address, data, data.length, false);
}
public void extend(int address, int size) {
diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/Program.java b/ethereumj-core/src/main/java/org/ethereum/vm/Program.java
index ee7bffee..4e6cac7b 100644
--- a/ethereumj-core/src/main/java/org/ethereum/vm/Program.java
+++ b/ethereumj-core/src/main/java/org/ethereum/vm/Program.java
@@ -215,15 +215,15 @@ public class Program {
}
public void memorySave(DataWord addrB, DataWord value) {
- memory.write(addrB.intValue(), value.getData(), false);
+ memory.write(addrB.intValue(), value.getData(), value.getData().length, false);
}
- public void memorySaveLimited(int addr, byte[] value) {
- memory.write(addr, value, true);
+ public void memorySaveLimited(int addr, byte[] data, int dataSize) {
+ memory.write(addr, data, dataSize, true);
}
public void memorySave(int addr, byte[] value) {
- memory.write(addr, value, false);
+ memory.write(addr, value, value.length, false);
}
public void memoryExpand(DataWord outDataOffs, DataWord outDataSize) {
@@ -500,7 +500,9 @@ public class Program {
if (result != null) {
byte[] buffer = result.getHReturn();
int offset = msg.getOutDataOffs().intValue();
- this.memorySaveLimited(offset, buffer);
+ int size = msg.getOutDataSize().intValue();
+
+ this.memorySaveLimited(offset, buffer, size);
}
// 4. THE FLAG OF SUCCESS IS ONE PUSHED INTO THE STACK
@@ -1023,7 +1025,7 @@ public class Program {
* used mostly for testing reasons
*/
public void initMem(byte[] data) {
- this.memory.write(0, data, false);
+ this.memory.write(0, data, data.length, false);
}
diff --git a/ethereumj-core/src/main/java/org/ethereum/vmtrace/OpActions.java b/ethereumj-core/src/main/java/org/ethereum/vmtrace/OpActions.java
index 0d3394d8..40c238a6 100644
--- a/ethereumj-core/src/main/java/org/ethereum/vmtrace/OpActions.java
+++ b/ethereumj-core/src/main/java/org/ethereum/vmtrace/OpActions.java
@@ -113,10 +113,10 @@ public class OpActions {
.addParam("delta", delta);
}
- public Action addMemoryWrite(int address, byte[] data) {
+ public Action addMemoryWrite(int address, byte[] data, int size) {
return addAction(memory, Action.Name.write)
.addParam("address", address)
- .addParam("data", toHexString(data));
+ .addParam("data", toHexString(data).substring(0, size));
}
public Action addStoragePut(DataWord key, DataWord value) {
diff --git a/ethereumj-core/src/main/java/org/ethereum/vmtrace/ProgramTraceListener.java b/ethereumj-core/src/main/java/org/ethereum/vmtrace/ProgramTraceListener.java
index deb2e9f7..a6a1cd10 100644
--- a/ethereumj-core/src/main/java/org/ethereum/vmtrace/ProgramTraceListener.java
+++ b/ethereumj-core/src/main/java/org/ethereum/vmtrace/ProgramTraceListener.java
@@ -13,8 +13,8 @@ public class ProgramTraceListener {
if (enabled) actions.addMemoryExtend(delta);
}
- public void onMemoryWrite(int address, byte[] data) {
- if (enabled) actions.addMemoryWrite(address, data);
+ public void onMemoryWrite(int address, byte[] data, int size) {
+ if (enabled) actions.addMemoryWrite(address, data, size);
}
public void onStackPop() {
diff --git a/ethereumj-core/src/main/resources/META-INF/native/linux32/libleveldbjni.so b/ethereumj-core/src/main/resources/META-INF/native/linux32/libleveldbjni.so
deleted file mode 100644
index 9000896e..00000000
Binary files a/ethereumj-core/src/main/resources/META-INF/native/linux32/libleveldbjni.so and /dev/null differ
diff --git a/ethereumj-core/src/main/resources/META-INF/native/linux64/libleveldbjni.so b/ethereumj-core/src/main/resources/META-INF/native/linux64/libleveldbjni.so
deleted file mode 100644
index 91b1a492..00000000
Binary files a/ethereumj-core/src/main/resources/META-INF/native/linux64/libleveldbjni.so and /dev/null differ
diff --git a/ethereumj-core/src/main/resources/META-INF/native/osx/leveldbjni.jnilib b/ethereumj-core/src/main/resources/META-INF/native/osx/leveldbjni.jnilib
deleted file mode 100644
index e67c7a22..00000000
Binary files a/ethereumj-core/src/main/resources/META-INF/native/osx/leveldbjni.jnilib and /dev/null differ
diff --git a/ethereumj-core/src/main/resources/META-INF/native/windows32/leveldbjni.dll b/ethereumj-core/src/main/resources/META-INF/native/windows32/leveldbjni.dll
deleted file mode 100644
index 88a8e5b9..00000000
Binary files a/ethereumj-core/src/main/resources/META-INF/native/windows32/leveldbjni.dll and /dev/null differ
diff --git a/ethereumj-core/src/main/resources/META-INF/native/windows64/leveldbjni.dll b/ethereumj-core/src/main/resources/META-INF/native/windows64/leveldbjni.dll
deleted file mode 100644
index 8dd9fb8e..00000000
Binary files a/ethereumj-core/src/main/resources/META-INF/native/windows64/leveldbjni.dll and /dev/null differ
diff --git a/ethereumj-core/src/main/resources/system.properties b/ethereumj-core/src/main/resources/system.properties
index 9ccd282f..d4006021 100644
--- a/ethereumj-core/src/main/resources/system.properties
+++ b/ethereumj-core/src/main/resources/system.properties
@@ -64,19 +64,19 @@ peer.discovery.workers = 3
# connection timeout for trying to
# connect to a peer [seconds]
-peer.connection.timeout = 300
+peer.connection.timeout = 2
# the time we wait to the network
# to approve the transaction, the
# transaction got approved when
# include into a transactions msg
# retrieved from the peer [seconds]
-transaction.approve.timeout = 300
+transaction.approve.timeout = 15
# the parameter specifies how much
# time we will wait for a message
# to come before closing the channel
-peer.channel.read.timeout = 300
+peer.channel.read.timeout = 30
# default directory where we keep
# basic Serpent samples relative
@@ -184,8 +184,8 @@ root.hash.start = -1
# The protocols supported by peer
peer.capabilities = eth, shh
-# Key value data source values: [leveldb/redis]
-keyvalue.datasource = leveldb
+# Key value data source values: [leveldb/redis/mapdb]
+keyvalue.datasource = mapdb
# Redis cloud enabled flag.
# Allows using RedisConnection for creating cloud based data structures.
@@ -200,3 +200,6 @@ blockchain.only=false
# the net
blocks.loader=
#E:\\temp\\_poc-9-blocks\\poc-9-492k_.dmp
+
+flush.batch.size=5000
+flush.ignore.consensus=false
\ No newline at end of file
diff --git a/ethereumj-core/src/test/java/org/ethereum/vm/MemoryTest.java b/ethereumj-core/src/test/java/org/ethereum/vm/MemoryTest.java
index 90cdc9c4..c6f332ee 100644
--- a/ethereumj-core/src/test/java/org/ethereum/vm/MemoryTest.java
+++ b/ethereumj-core/src/test/java/org/ethereum/vm/MemoryTest.java
@@ -45,7 +45,7 @@ public class MemoryTest {
Memory memoryBuffer = new Memory();
byte[] data = {1, 1, 1, 1};
- memoryBuffer.write(0, data, false);
+ memoryBuffer.write(0, data, data.length, false);
assertTrue(1 == memoryBuffer.getChunks().size());
@@ -65,7 +65,7 @@ public class MemoryTest {
Memory memoryBuffer = new Memory();
byte[] data = Hex.decode("0101010101010101010101010101010101010101010101010101010101010101");
- memoryBuffer.write(0, data, false);
+ memoryBuffer.write(0, data, data.length, false);
assertTrue(1 == memoryBuffer.getChunks().size());
@@ -86,7 +86,7 @@ public class MemoryTest {
Memory memoryBuffer = new Memory();
byte[] data = Hex.decode("010101010101010101010101010101010101010101010101010101010101010101");
- memoryBuffer.write(0, data, false);
+ memoryBuffer.write(0, data, data.length, false);
assertTrue(1 == memoryBuffer.getChunks().size());
@@ -109,7 +109,7 @@ public class MemoryTest {
byte[] data = new byte[1024];
Arrays.fill(data, (byte) 1);
- memoryBuffer.write(0, data, false);
+ memoryBuffer.write(0, data, data.length, false);
assertTrue(1 == memoryBuffer.getChunks().size());
@@ -131,7 +131,7 @@ public class MemoryTest {
byte[] data = new byte[1025];
Arrays.fill(data, (byte) 1);
- memoryBuffer.write(0, data, false);
+ memoryBuffer.write(0, data, data.length, false);
assertTrue(2 == memoryBuffer.getChunks().size());
@@ -160,8 +160,8 @@ public class MemoryTest {
byte[] data2 = new byte[1024];
Arrays.fill(data2, (byte) 2);
- memoryBuffer.write(0, data1, false);
- memoryBuffer.write(1024, data2, false);
+ memoryBuffer.write(0, data1, data1.length, false);
+ memoryBuffer.write(1024, data2, data2.length, false);
assertTrue(2 == memoryBuffer.getChunks().size());
@@ -196,9 +196,9 @@ public class MemoryTest {
byte[] data3 = new byte[1];
Arrays.fill(data3, (byte) 3);
- memoryBuffer.write(0, data1, false);
- memoryBuffer.write(1024, data2, false);
- memoryBuffer.write(2048, data3, false);
+ memoryBuffer.write(0, data1, data1.length, false);
+ memoryBuffer.write(1024, data2, data2.length, false);
+ memoryBuffer.write(2048, data3, data3.length, false);
assertTrue(3 == memoryBuffer.getChunks().size());
@@ -295,8 +295,8 @@ public class MemoryTest {
byte[] data2 = new byte[1024];
Arrays.fill(data2, (byte) 2);
- memoryBuffer.write(0, data1, false);
- memoryBuffer.write(1024, data2, false);
+ memoryBuffer.write(0, data1, data1.length, false);
+ memoryBuffer.write(1024, data2, data2.length, false);
assertTrue(memoryBuffer.getChunks().size() == 2);
assertTrue(memoryBuffer.size() == 2048);
@@ -330,8 +330,8 @@ public class MemoryTest {
byte[] data2 = new byte[32];
Arrays.fill(data2, (byte) 2);
- memoryBuffer.write(0, data1, false);
- memoryBuffer.write(32, data2, false);
+ memoryBuffer.write(0, data1, data1.length, false);
+ memoryBuffer.write(32, data2, data2.length, false);
byte[] data = memoryBuffer.read(0, 64);
@@ -352,7 +352,7 @@ public class MemoryTest {
byte[] data1 = new byte[32];
Arrays.fill(data1, (byte) 1);
- memoryBuffer.write(0, data1, false);
+ memoryBuffer.write(0, data1, data1.length, false);
assertTrue(32 == memoryBuffer.size());
byte[] data = memoryBuffer.read(0, 64);
@@ -377,8 +377,8 @@ public class MemoryTest {
byte[] data2 = new byte[1024];
Arrays.fill(data2, (byte) 2);
- memoryBuffer.write(0, data1, false);
- memoryBuffer.write(1024, data2, false);
+ memoryBuffer.write(0, data1, data1.length, false);
+ memoryBuffer.write(1024, data2, data2.length, false);
byte[] data = memoryBuffer.read(0, 2048);
@@ -403,8 +403,8 @@ public class MemoryTest {
byte[] data2 = new byte[1024];
Arrays.fill(data2, (byte) 2);
- memoryBuffer.write(0, data1, false);
- memoryBuffer.write(1024, data2, false);
+ memoryBuffer.write(0, data1, data1.length, false);
+ memoryBuffer.write(1024, data2, data2.length, false);
byte[] data = memoryBuffer.read(0, 2049);
@@ -430,12 +430,12 @@ public class MemoryTest {
byte[] data1 = new byte[6272];
Arrays.fill(data1, (byte) 1);
- memoryBuffer.write(2720, data1, true);
+ memoryBuffer.write(2720, data1, data1.length, true);
byte lastZero = memoryBuffer.readByte(2719);
byte firstOne = memoryBuffer.readByte(2721);
- assertTrue(memoryBuffer.internalSize() == 3072);
+ assertTrue(memoryBuffer.size() == 3072);
assertTrue(lastZero == 0);
assertTrue(firstOne == 1);
@@ -451,4 +451,66 @@ public class MemoryTest {
assertTrue(zero == 0);
}
+ @Test
+ public void memoryWriteLimited_2(){
+
+ Memory memoryBuffer = new Memory();
+ memoryBuffer.extend(0, 3072);
+
+ byte[] data1 = new byte[6272];
+ Arrays.fill(data1, (byte) 1);
+
+ memoryBuffer.write(2720, data1, 300, true);
+
+ byte lastZero = memoryBuffer.readByte(2719);
+ byte firstOne = memoryBuffer.readByte(2721);
+
+ assertTrue(memoryBuffer.size() == 3072);
+ assertTrue(lastZero == 0);
+ assertTrue(firstOne == 1);
+
+ byte[] data = memoryBuffer.read(2720, 352);
+
+ int ones = 0; int zero = 0;
+ for (int i = 0; i < data.length; ++i){
+ if (data[i] == 1) ++ones;
+ if (data[i] == 0) ++zero;
+ }
+
+ assertTrue(ones == 300);
+ assertTrue(zero == 52);
+ }
+
+ @Test
+ public void memoryWriteLimited_3(){
+
+ Memory memoryBuffer = new Memory();
+ memoryBuffer.extend(0, 128);
+
+ byte[] data1 = new byte[20];
+ Arrays.fill(data1, (byte) 1);
+
+ memoryBuffer.write(10, data1, 40, true);
+
+ byte lastZero = memoryBuffer.readByte(9);
+ byte firstOne = memoryBuffer.readByte(10);
+
+ assertTrue(memoryBuffer.size() == 128);
+ assertTrue(lastZero == 0);
+ assertTrue(firstOne == 1);
+
+ byte[] data = memoryBuffer.read(10, 30);
+
+ int ones = 0; int zero = 0;
+ for (int i = 0; i < data.length; ++i){
+ if (data[i] == 1) ++ones;
+ if (data[i] == 0) ++zero;
+ }
+
+ assertTrue(ones == 20);
+ assertTrue(zero == 10);
+ }
+
+
+
}
\ No newline at end of file