Merged latest develop.

This commit is contained in:
Adrian Tiberius 2015-06-23 21:36:35 +02:00
parent 50ee6adb5b
commit 877e237a27
27 changed files with 186 additions and 94 deletions

View File

@ -21,6 +21,7 @@ android {
minSdkVersion 14 minSdkVersion 14
targetSdkVersion 22 targetSdkVersion 22
multiDexEnabled true multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
} }
packagingOptions { packagingOptions {
@ -54,7 +55,12 @@ dependencies {
compile project(':ethereumj-core-android') compile project(':ethereumj-core-android')
compile 'com.android.support:multidex:1.0.0' compile 'com.android.support:multidex:1.0.0'
compile fileTree(include: ['*.jar'], dir: '../libraries') compile fileTree(include: ['*.jar'], dir: '../libraries')
compile 'com.android.support:support-v4:22.1.1' compile 'com.android.support:support-v4:22.2.0'
compile 'com.android.support:appcompat-v7:22.1.1' compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:22.1.1' 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'
} }

View File

@ -2,15 +2,19 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.ethereum.android_app" > package="org.ethereum.android_app" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application <application
android:name="org.ethereum.android_app.EthereumApplication" android:name=".EthereumApplication"
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:largeHeap="true" android:largeHeap="true"
android:theme="@style/AppTheme" > android:theme="@style/AppTheme" >
<activity <activity
android:name="org.ethereum.android_app.MainActivity" android:name=".MainActivity"
android:label="@string/app_name" > android:label="@string/app_name" >
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
@ -18,8 +22,10 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".TestActivity"
android:label="@string/title_activity_test" >
</activity>
</application> </application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
</manifest>

View File

@ -95,7 +95,9 @@ public class MainActivity extends ActionBarActivity {
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
ethereumManager.onDestroy(); if (ethereumManager != null) {
ethereumManager.onDestroy();
}
} }
// The definition of our task class // The definition of our task class
@ -112,7 +114,14 @@ public class MainActivity extends ActionBarActivity {
Log.v(TAG, "111"); Log.v(TAG, "111");
Log.v(TAG, "222"); 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); //ConsoleFragment consoleeFrag = (ConsoleFragment)getSupportFragmentManager().findFragmentById(R.id.console);
//consoleeFrag.updateDuration(duration); //consoleeFrag.updateDuration(duration);
Log.v(TAG, "333"); Log.v(TAG, "333");
@ -158,7 +167,9 @@ public class MainActivity extends ActionBarActivity {
protected String doInBackground(Context... params) { protected String doInBackground(Context... params) {
Log.v(TAG, "444"); Log.v(TAG, "444");
try { try {
ethereumManager.startJsonRpc(); if (ethereumManager != null) {
ethereumManager.startJsonRpc();
}
} catch (Exception e) { } catch (Exception e) {
} }
Log.v(TAG, "555"); Log.v(TAG, "555");

View File

@ -1,3 +1,6 @@
<resources> <resources>
<dimen name="toolbar_elevation">4dp</dimen> <dimen name="toolbar_elevation">4dp</dimen>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources> </resources>

View File

@ -6,5 +6,8 @@
<string name="console_output">Console Output:</string> <string name="console_output">Console Output:</string>
<string name="ethereumj_tests">EthereumJ Tests</string> <string name="ethereumj_tests">EthereumJ Tests</string>
<string name="title_activity_test">TestActivity</string>
<string name="hello_world">Hello world!</string>
</resources> </resources>

View File

@ -22,8 +22,6 @@ import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import android.content.Context;
/** /**
* @author Roman Mandeleil * @author Roman Mandeleil
* @since 18.01.2015 * @since 18.01.2015
@ -34,7 +32,6 @@ public class LevelDbDataSource implements KeyValueDataSource {
String name; String name;
private DB db; private DB db;
private Context context;
public LevelDbDataSource() { public LevelDbDataSource() {
} }
@ -43,10 +40,6 @@ public class LevelDbDataSource implements KeyValueDataSource {
this.name = name; this.name = name;
} }
public void setContext(Context context) {
this.context = context;
}
@Override @Override
public void init() { public void init() {

View File

@ -83,9 +83,7 @@ public class EthereumModule {
@Singleton @Singleton
Repository provideRepository() { Repository provideRepository() {
LevelDbDataSource detailsDS = new LevelDbDataSource(); LevelDbDataSource detailsDS = new LevelDbDataSource();
detailsDS.setContext(context);
LevelDbDataSource stateDS = new LevelDbDataSource(); LevelDbDataSource stateDS = new LevelDbDataSource();
stateDS.setContext(context);
return new RepositoryImpl(detailsDS, stateDS); return new RepositoryImpl(detailsDS, stateDS);
} }

View File

@ -150,6 +150,7 @@ dependencies {
compile "commons-dbcp:commons-dbcp:1.4" compile "commons-dbcp:commons-dbcp:1.4"
compile "redis.clients:jedis:2.6.0" compile "redis.clients:jedis:2.6.0"
compile "com.h2database:h2:1.4.187" compile "com.h2database:h2:1.4.187"
compile "org.mapdb:mapdb:1.0.7"
compile "org.slf4j:slf4j-log4j12:${slf4jVersion}" compile "org.slf4j:slf4j-log4j12:${slf4jVersion}"
compile "log4j:apache-log4j-extras:${log4jVersion}" compile "log4j:apache-log4j-extras:${log4jVersion}"

View File

@ -47,6 +47,8 @@ public class SystemProperties {
private final static String DEFAULT_KEY_VALUE_DATA_SOURCE = "leveldb"; private final static String DEFAULT_KEY_VALUE_DATA_SOURCE = "leveldb";
private final static boolean DEFAULT_REDIS_ENABLED = true; private final static boolean DEFAULT_REDIS_ENABLED = true;
private static final String DEFAULT_BLOCKS_LOADER = ""; 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 */ /* Testing */
@ -254,6 +256,19 @@ public class SystemProperties {
private boolean boolProperty(String key, Boolean defaultValue) { private boolean boolProperty(String key, Boolean defaultValue) {
return Boolean.parseBoolean(prop.getProperty(key, String.valueOf(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() { public String vmTraceDir() {
return prop.getProperty("vm.structured.dir", DEFAULT_VM_TRACE_DIR); return prop.getProperty("vm.structured.dir", DEFAULT_VM_TRACE_DIR);

View File

@ -294,9 +294,7 @@ public class BlockchainImpl implements Blockchain {
track.commit(); track.commit();
storeBlock(block, receipts); storeBlock(block, receipts);
if (needFlush(block)) {
if (adminInfo.isConsensus() &&
block.getNumber() % 5_000 == 0) {
repository.flush(); repository.flush();
blockStore.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<TransactionReceipt> receipts){ private byte[] calcReceiptsTrie(List<TransactionReceipt> receipts){
//TODO Fix Trie hash for receipts - doesnt match cpp //TODO Fix Trie hash for receipts - doesnt match cpp
Trie receiptsTrie = new TrieImpl(null); Trie receiptsTrie = new TrieImpl(null);

View File

@ -179,9 +179,6 @@ public class Wallet {
} }
public void applyTransaction(Transaction transaction) { public void applyTransaction(Transaction transaction) {
transactionMap.put(new ByteArrayWrapper(transaction.getHash()), transaction);
byte[] senderAddress = transaction.getSender(); byte[] senderAddress = transaction.getSender();
Account sender = rows.get(Hex.toHexString(senderAddress)); Account sender = rows.get(Hex.toHexString(senderAddress));
if (sender != null) { if (sender != null) {

View File

@ -2,8 +2,6 @@ package org.ethereum.datasource;
import org.ethereum.config.SystemProperties; 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.CompressionType;
import org.iq80.leveldb.DB; import org.iq80.leveldb.DB;
import org.iq80.leveldb.DBIterator; import org.iq80.leveldb.DBIterator;
@ -64,23 +62,7 @@ public class LevelDbDataSource implements KeyValueDataSource {
} }
logger.debug("Initializing new or existing database: '{}'", name); logger.debug("Initializing new or existing database: '{}'", name);
db = factory.open(fileLocation, options);
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);
}
} catch (IOException ioe) { } catch (IOException ioe) {
logger.error(ioe.getMessage(), ioe); logger.error(ioe.getMessage(), ioe);

View File

@ -1,5 +1,6 @@
package org.ethereum.datasource.redis; package org.ethereum.datasource.redis;
import org.apache.commons.collections4.CollectionUtils;
import redis.clients.jedis.Jedis; import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPool;
@ -74,7 +75,7 @@ public class RedisSet<T> extends RedisStorage<T> implements Set<T> {
@Override @Override
public boolean remove(Object o) { public boolean remove(Object o) {
return removeAll(Arrays.asList(o)); return (o == null) || removeAll(Arrays.asList(o));
} }
@Override @Override
@ -110,7 +111,7 @@ public class RedisSet<T> extends RedisStorage<T> implements Set<T> {
@Override @Override
public boolean removeAll(final Collection<?> c) { public boolean removeAll(final Collection<?> c) {
return pooledWithResult(new Function<Jedis, Boolean>() { return CollectionUtils.isEmpty(c) || pooledWithResult(new Function<Jedis, Boolean>() {
@Override @Override
public Boolean apply(Jedis jedis) { public Boolean apply(Jedis jedis) {
return jedis.srem(getName(), serialize(c)) == c.size(); return jedis.srem(getName(), serialize(c)) == c.size();

View File

@ -15,7 +15,7 @@ import static org.ethereum.util.Functional.*;
public abstract class RedisStorage<T> { public abstract class RedisStorage<T> {
private static final Logger logger = LoggerFactory.getLogger("db"); protected static final Logger log = LoggerFactory.getLogger("redis");
private byte[] name; private byte[] name;
private final JedisPool pool; private final JedisPool pool;

View File

@ -4,13 +4,14 @@ import org.ethereum.util.FastByteComparisons;
import org.spongycastle.util.encoders.Hex; import org.spongycastle.util.encoders.Hex;
import java.io.Serializable;
import java.util.Arrays; import java.util.Arrays;
/** /**
* @author Roman Mandeleil * @author Roman Mandeleil
* @since 11.06.2014 * @since 11.06.2014
*/ */
public class ByteArrayWrapper implements Comparable<ByteArrayWrapper> { public class ByteArrayWrapper implements Comparable<ByteArrayWrapper>, Serializable {
private final byte[] data; private final byte[] data;
private int hashCode = 0; private int hashCode = 0;

View File

@ -5,6 +5,7 @@ import org.ethereum.core.BlockchainImpl;
import org.ethereum.core.Wallet; import org.ethereum.core.Wallet;
import org.ethereum.datasource.KeyValueDataSource; import org.ethereum.datasource.KeyValueDataSource;
import org.ethereum.datasource.LevelDbDataSource; import org.ethereum.datasource.LevelDbDataSource;
import org.ethereum.datasource.mapdb.MapDBDataSource;
import org.ethereum.db.BlockStore; import org.ethereum.db.BlockStore;
import org.ethereum.db.InMemoryBlockStore; import org.ethereum.db.InMemoryBlockStore;
import org.ethereum.db.RepositoryImpl; import org.ethereum.db.RepositoryImpl;
@ -76,8 +77,8 @@ public class EthereumModule {
@Provides @Provides
@Singleton @Singleton
Repository provideRepository() { Repository provideRepository() {
LevelDbDataSource detailsDS = new LevelDbDataSource(); MapDBDataSource detailsDS = new MapDBDataSource();
LevelDbDataSource stateDS = new LevelDbDataSource(); MapDBDataSource stateDS = new MapDBDataSource();
return new RepositoryImpl(detailsDS, stateDS); return new RepositoryImpl(detailsDS, stateDS);
} }

View File

@ -53,20 +53,22 @@ public class Memory implements ProgramTraceListenerAware {
return data; 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) if (!limited)
extend(address, data.length); extend(address, dataSize);
int chunkIndex = address / CHUNK_SIZE; int chunkIndex = address / CHUNK_SIZE;
int chunkOffset = address % CHUNK_SIZE; int chunkOffset = address % CHUNK_SIZE;
int toCapture = 0; int toCapture = 0;
if (limited) if (limited)
toCapture = (address + data.length > softSize) ? softSize - address : data.length; toCapture = (address + dataSize > softSize) ? softSize - address : dataSize;
else else
toCapture = data.length; toCapture = dataSize;
int start = 0; int start = 0;
@ -82,12 +84,12 @@ public class Memory implements ProgramTraceListenerAware {
start += captured; 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) { public void extendAndWrite(int address, int allocSize, byte[] data) {
extend(address, allocSize); extend(address, allocSize);
write(address, data, false); write(address, data, data.length, false);
} }
public void extend(int address, int size) { public void extend(int address, int size) {

View File

@ -215,15 +215,15 @@ public class Program {
} }
public void memorySave(DataWord addrB, DataWord value) { 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) { public void memorySaveLimited(int addr, byte[] data, int dataSize) {
memory.write(addr, value, true); memory.write(addr, data, dataSize, true);
} }
public void memorySave(int addr, byte[] value) { 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) { public void memoryExpand(DataWord outDataOffs, DataWord outDataSize) {
@ -500,7 +500,9 @@ public class Program {
if (result != null) { if (result != null) {
byte[] buffer = result.getHReturn(); byte[] buffer = result.getHReturn();
int offset = msg.getOutDataOffs().intValue(); 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 // 4. THE FLAG OF SUCCESS IS ONE PUSHED INTO THE STACK
@ -1023,7 +1025,7 @@ public class Program {
* used mostly for testing reasons * used mostly for testing reasons
*/ */
public void initMem(byte[] data) { public void initMem(byte[] data) {
this.memory.write(0, data, false); this.memory.write(0, data, data.length, false);
} }

View File

@ -113,10 +113,10 @@ public class OpActions {
.addParam("delta", delta); .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) return addAction(memory, Action.Name.write)
.addParam("address", address) .addParam("address", address)
.addParam("data", toHexString(data)); .addParam("data", toHexString(data).substring(0, size));
} }
public Action addStoragePut(DataWord key, DataWord value) { public Action addStoragePut(DataWord key, DataWord value) {

View File

@ -13,8 +13,8 @@ public class ProgramTraceListener {
if (enabled) actions.addMemoryExtend(delta); if (enabled) actions.addMemoryExtend(delta);
} }
public void onMemoryWrite(int address, byte[] data) { public void onMemoryWrite(int address, byte[] data, int size) {
if (enabled) actions.addMemoryWrite(address, data); if (enabled) actions.addMemoryWrite(address, data, size);
} }
public void onStackPop() { public void onStackPop() {

View File

@ -64,19 +64,19 @@ peer.discovery.workers = 3
# connection timeout for trying to # connection timeout for trying to
# connect to a peer [seconds] # connect to a peer [seconds]
peer.connection.timeout = 300 peer.connection.timeout = 2
# the time we wait to the network # the time we wait to the network
# to approve the transaction, the # to approve the transaction, the
# transaction got approved when # transaction got approved when
# include into a transactions msg # include into a transactions msg
# retrieved from the peer [seconds] # retrieved from the peer [seconds]
transaction.approve.timeout = 300 transaction.approve.timeout = 15
# the parameter specifies how much # the parameter specifies how much
# time we will wait for a message # time we will wait for a message
# to come before closing the channel # to come before closing the channel
peer.channel.read.timeout = 300 peer.channel.read.timeout = 30
# default directory where we keep # default directory where we keep
# basic Serpent samples relative # basic Serpent samples relative
@ -184,8 +184,8 @@ root.hash.start = -1
# The protocols supported by peer # The protocols supported by peer
peer.capabilities = eth, shh peer.capabilities = eth, shh
# Key value data source values: [leveldb/redis] # Key value data source values: [leveldb/redis/mapdb]
keyvalue.datasource = leveldb keyvalue.datasource = mapdb
# Redis cloud enabled flag. # Redis cloud enabled flag.
# Allows using RedisConnection for creating cloud based data structures. # Allows using RedisConnection for creating cloud based data structures.
@ -200,3 +200,6 @@ blockchain.only=false
# the net # the net
blocks.loader= blocks.loader=
#E:\\temp\\_poc-9-blocks\\poc-9-492k_.dmp #E:\\temp\\_poc-9-blocks\\poc-9-492k_.dmp
flush.batch.size=5000
flush.ignore.consensus=false

View File

@ -45,7 +45,7 @@ public class MemoryTest {
Memory memoryBuffer = new Memory(); Memory memoryBuffer = new Memory();
byte[] data = {1, 1, 1, 1}; byte[] data = {1, 1, 1, 1};
memoryBuffer.write(0, data, false); memoryBuffer.write(0, data, data.length, false);
assertTrue(1 == memoryBuffer.getChunks().size()); assertTrue(1 == memoryBuffer.getChunks().size());
@ -65,7 +65,7 @@ public class MemoryTest {
Memory memoryBuffer = new Memory(); Memory memoryBuffer = new Memory();
byte[] data = Hex.decode("0101010101010101010101010101010101010101010101010101010101010101"); byte[] data = Hex.decode("0101010101010101010101010101010101010101010101010101010101010101");
memoryBuffer.write(0, data, false); memoryBuffer.write(0, data, data.length, false);
assertTrue(1 == memoryBuffer.getChunks().size()); assertTrue(1 == memoryBuffer.getChunks().size());
@ -86,7 +86,7 @@ public class MemoryTest {
Memory memoryBuffer = new Memory(); Memory memoryBuffer = new Memory();
byte[] data = Hex.decode("010101010101010101010101010101010101010101010101010101010101010101"); byte[] data = Hex.decode("010101010101010101010101010101010101010101010101010101010101010101");
memoryBuffer.write(0, data, false); memoryBuffer.write(0, data, data.length, false);
assertTrue(1 == memoryBuffer.getChunks().size()); assertTrue(1 == memoryBuffer.getChunks().size());
@ -109,7 +109,7 @@ public class MemoryTest {
byte[] data = new byte[1024]; byte[] data = new byte[1024];
Arrays.fill(data, (byte) 1); Arrays.fill(data, (byte) 1);
memoryBuffer.write(0, data, false); memoryBuffer.write(0, data, data.length, false);
assertTrue(1 == memoryBuffer.getChunks().size()); assertTrue(1 == memoryBuffer.getChunks().size());
@ -131,7 +131,7 @@ public class MemoryTest {
byte[] data = new byte[1025]; byte[] data = new byte[1025];
Arrays.fill(data, (byte) 1); Arrays.fill(data, (byte) 1);
memoryBuffer.write(0, data, false); memoryBuffer.write(0, data, data.length, false);
assertTrue(2 == memoryBuffer.getChunks().size()); assertTrue(2 == memoryBuffer.getChunks().size());
@ -160,8 +160,8 @@ public class MemoryTest {
byte[] data2 = new byte[1024]; byte[] data2 = new byte[1024];
Arrays.fill(data2, (byte) 2); Arrays.fill(data2, (byte) 2);
memoryBuffer.write(0, data1, false); memoryBuffer.write(0, data1, data1.length, false);
memoryBuffer.write(1024, data2, false); memoryBuffer.write(1024, data2, data2.length, false);
assertTrue(2 == memoryBuffer.getChunks().size()); assertTrue(2 == memoryBuffer.getChunks().size());
@ -196,9 +196,9 @@ public class MemoryTest {
byte[] data3 = new byte[1]; byte[] data3 = new byte[1];
Arrays.fill(data3, (byte) 3); Arrays.fill(data3, (byte) 3);
memoryBuffer.write(0, data1, false); memoryBuffer.write(0, data1, data1.length, false);
memoryBuffer.write(1024, data2, false); memoryBuffer.write(1024, data2, data2.length, false);
memoryBuffer.write(2048, data3, false); memoryBuffer.write(2048, data3, data3.length, false);
assertTrue(3 == memoryBuffer.getChunks().size()); assertTrue(3 == memoryBuffer.getChunks().size());
@ -295,8 +295,8 @@ public class MemoryTest {
byte[] data2 = new byte[1024]; byte[] data2 = new byte[1024];
Arrays.fill(data2, (byte) 2); Arrays.fill(data2, (byte) 2);
memoryBuffer.write(0, data1, false); memoryBuffer.write(0, data1, data1.length, false);
memoryBuffer.write(1024, data2, false); memoryBuffer.write(1024, data2, data2.length, false);
assertTrue(memoryBuffer.getChunks().size() == 2); assertTrue(memoryBuffer.getChunks().size() == 2);
assertTrue(memoryBuffer.size() == 2048); assertTrue(memoryBuffer.size() == 2048);
@ -330,8 +330,8 @@ public class MemoryTest {
byte[] data2 = new byte[32]; byte[] data2 = new byte[32];
Arrays.fill(data2, (byte) 2); Arrays.fill(data2, (byte) 2);
memoryBuffer.write(0, data1, false); memoryBuffer.write(0, data1, data1.length, false);
memoryBuffer.write(32, data2, false); memoryBuffer.write(32, data2, data2.length, false);
byte[] data = memoryBuffer.read(0, 64); byte[] data = memoryBuffer.read(0, 64);
@ -352,7 +352,7 @@ public class MemoryTest {
byte[] data1 = new byte[32]; byte[] data1 = new byte[32];
Arrays.fill(data1, (byte) 1); Arrays.fill(data1, (byte) 1);
memoryBuffer.write(0, data1, false); memoryBuffer.write(0, data1, data1.length, false);
assertTrue(32 == memoryBuffer.size()); assertTrue(32 == memoryBuffer.size());
byte[] data = memoryBuffer.read(0, 64); byte[] data = memoryBuffer.read(0, 64);
@ -377,8 +377,8 @@ public class MemoryTest {
byte[] data2 = new byte[1024]; byte[] data2 = new byte[1024];
Arrays.fill(data2, (byte) 2); Arrays.fill(data2, (byte) 2);
memoryBuffer.write(0, data1, false); memoryBuffer.write(0, data1, data1.length, false);
memoryBuffer.write(1024, data2, false); memoryBuffer.write(1024, data2, data2.length, false);
byte[] data = memoryBuffer.read(0, 2048); byte[] data = memoryBuffer.read(0, 2048);
@ -403,8 +403,8 @@ public class MemoryTest {
byte[] data2 = new byte[1024]; byte[] data2 = new byte[1024];
Arrays.fill(data2, (byte) 2); Arrays.fill(data2, (byte) 2);
memoryBuffer.write(0, data1, false); memoryBuffer.write(0, data1, data1.length, false);
memoryBuffer.write(1024, data2, false); memoryBuffer.write(1024, data2, data2.length, false);
byte[] data = memoryBuffer.read(0, 2049); byte[] data = memoryBuffer.read(0, 2049);
@ -430,12 +430,12 @@ public class MemoryTest {
byte[] data1 = new byte[6272]; byte[] data1 = new byte[6272];
Arrays.fill(data1, (byte) 1); Arrays.fill(data1, (byte) 1);
memoryBuffer.write(2720, data1, true); memoryBuffer.write(2720, data1, data1.length, true);
byte lastZero = memoryBuffer.readByte(2719); byte lastZero = memoryBuffer.readByte(2719);
byte firstOne = memoryBuffer.readByte(2721); byte firstOne = memoryBuffer.readByte(2721);
assertTrue(memoryBuffer.internalSize() == 3072); assertTrue(memoryBuffer.size() == 3072);
assertTrue(lastZero == 0); assertTrue(lastZero == 0);
assertTrue(firstOne == 1); assertTrue(firstOne == 1);
@ -451,4 +451,66 @@ public class MemoryTest {
assertTrue(zero == 0); 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);
}
} }