
- •Ход работы
- •1. Провёл процедуру code review (оценки качества программного кода) для проекта из лабораторной работы 1(а-б).
- •Class/interface documentation comment (/**...*/)
- •Wrapping Lines.When an expression will not fit on a single line, break it.
- •Dead comments
- •Volatile long _resizers; // count of threads attempting a resize
- •Too large tests with multiple assertions.
- •If (entry.GetKey().Equals(Long.ValueOf(100))) {
- •Tests swallowing exceptions.
Too large tests with multiple assertions.
public final void testNonBlockingHashMapSize() {
NonBlockingHashMapLong<String> items = new NonBlockingHashMapLong<String>();
items.put(Long.valueOf(100), "100");
items.put(Long.valueOf(101), "101");
assertEquals("keySet().size()", 2, items.keySet().size());
assertTrue("keySet().contains(100)", items.keySet().contains(Long.valueOf(100)));
assertTrue("keySet().contains(101)", items.keySet().contains(Long.valueOf(101)));
assertEquals("values().size()", 2, items.values().size());
assertTrue("values().contains(\"100\")", items.values().contains("100"));
assertTrue("values().contains(\"101\")", items.values().contains("101"));
assertEquals("entrySet().size()", 2, items.entrySet().size());
boolean found100 = false;
boolean found101 = false;
for (Entry<Long, String> entry : items.entrySet()) {
If (entry.GetKey().Equals(Long.ValueOf(100))) {
assertEquals("entry[100].getValue()==\"100\"", "100", entry.getValue());
found100 = true;
} else if (entry.getKey().equals(Long.valueOf(101))) {
assertEquals("entry[101].getValue()==\"101\"", "101", entry.getValue());
found101 = true;
}
}
assertTrue("entrySet().contains([100])", found100);
assertTrue("entrySet().contains([101])", found101);
}
Hidden tests functionality. The test should be clear by looking at the test method only – no initialization or assertion somewhere else.
public class NonBlockingHashMapTest extends TestCase {
private NonBlockingSetInt _nbsi;
protected void setUp () { _nbsi = new NonBlockingSetInt(); }
protected void tearDown() { _nbsi = null; }
public void testIteration() {
assertTrue ( _nbsi.isEmpty() );
assertTrue ( _nbsi.add(1) );
assertTrue ( _nbsi.add(2) );
StringBuffer buf = new StringBuffer();
for( Iterator<Integer> i = _nbsi.iterator(); i.hasNext(); ) {
Integer val = i.next();
buf.append(val);
}
assertThat("found all vals",buf.toString(),anyOf(is("12"),is("21")));
assertThat("toString works",_nbsi.toString(), anyOf(is("[1, 2]"),is("[2, 1]")));
}
}
Single scenario per test. One test checks one scenario only.
public void testBasic() {
assertTrue ( _nbhml.isEmpty() );
assertThat ( _nbhml.put(1,"v1"), nullValue() );
checkSizes (1);
assertThat ( _nbhml.putIfAbsent(2,"v2"), nullValue() );
checkSizes (2);
assertTrue ( _nbhml.containsKey(2) );
assertThat ( _nbhml.put(1,"v1a"), is("v1") );
assertThat ( _nbhml.put(2,"v2a"), is("v2") );
checkSizes (2);
assertThat ( _nbhml.putIfAbsent(2,"v2b"), is("v2a") );
assertThat ( _nbhml.remove(1), is("v1a") );
assertFalse( _nbhml.containsKey(1) );
checkSizes (1);
assertThat ( _nbhml.remove(1), nullValue() );
assertThat ( _nbhml.remove(2), is("v2a") );
checkSizes (0);
assertThat ( _nbhml.remove(2), nullValue() );
assertThat ( _nbhml.remove("k3"), nullValue() );
assertTrue ( _nbhml.isEmpty() );
assertThat ( _nbhml.put(0,"v0"), nullValue() );
assertTrue ( _nbhml.containsKey(0) );
checkSizes (1);
assertThat ( _nbhml.remove(0), is("v0") );
assertFalse( _nbhml.containsKey(0) );
checkSizes (0);
assertThat ( _nbhml.replace(0,"v0"), nullValue() );
assertFalse( _nbhml.containsKey(0) );
assertThat ( _nbhml.put(0,"v0"), nullValue() );
assertEquals(_nbhml.replace(0,"v0a"), "v0" );
assertEquals(_nbhml.get(0), "v0a" );
assertThat ( _nbhml.remove(0), is("v0a") );
assertFalse( _nbhml.containsKey(0) );
checkSizes (0);
assertThat ( _nbhml.replace(1,"v1"), nullValue() );
assertFalse( _nbhml.containsKey(1) );
assertThat ( _nbhml.put(1,"v1"), nullValue() );
assertEquals(_nbhml.replace(1,"v1a"), "v1" );
assertEquals(_nbhml.get(1), "v1a" );
assertThat ( _nbhml.remove(1), is("v1a") );
assertFalse( _nbhml.containsKey(1) );
checkSizes (0);
// Simple insert of simple keys, with no reprobing on insert until the
// table gets full exactly. Then do a 'get' on the totally full table.
NonBlockingHashMapLong<Object> map = new NonBlockingHashMapLong<Object>(32);
for( int i = 1; i < 32; i++ )
map.put(i, new Object());
map.get(33); // this causes a NPE
}
Unit Test Methods show whole truth. Unit test methods show all parts needed for the test. Do not use SetUp method or base classes to perform actions on testee or dependencies.
public class NonBlockingHashMapTest extends TestCase {
private NonBlockingHashMap<String,String> _nbhm;
protected void setUp () { _nbhm = new NonBlockingHashMap<String,String>(); }
protected void tearDown() { _nbhm = null; }
. . . . .
}