我正在尝试了解Java断言。我有以下方法,我想添加一个后置条件断言。我不确定自己做的是否正确,是否有人可以对我说清楚并告诉我是否正确,或者为什么?
chain
是组成链的块的列表,每个块都由字节数组表示。
boolean addBlock (byte[] block) {
// Prepare for correctness check
byte[] last = getLast();
byte[] last_data_hash = new byte[hash_size];
byte[] current_link_hash = new byte[hash_size];
for (int i=0; i < hash_size; i++) {
last_data_hash[i] = last[hash_size+i];
current_link_hash[i] = block[i];
}
if (Arrays.equals(current_link_hash,last_data_hash)) {
// if it is correctly linked, add it
chain.add(block.clone());
return true;
} else // if it is not correctly linked, don't add it
return false;
assert (chain == null) : "chain list: " + chain ;
}
编辑:
boolean addBlock (byte[] block) {
// Prepare for correctness check
byte[] last = getLast();
byte[] last_data_hash = new byte[hash_size];
byte[] current_link_hash = new byte[hash_size];
for (int i=0; i < hash_size; i++) {
last_data_hash[i] = last[hash_size+i];
current_link_hash[i] = block[i];
}
if (Arrays.equals(current_link_hash,last_data_hash)) {
// if it is correctly linked, add it
chain.add(block.clone());
**assert (chain == null) : "chain list: " + chain ;**
return true;
} else // if it is not correctly linked, don't add it
return false;
}