|
1 | 1 | package vanilla.java.affinity.impl; |
2 | 2 |
|
3 | | -import org.apache.commons.logging.Log; |
4 | | -import org.apache.commons.logging.LogFactory; |
5 | | -import org.junit.Assert; |
6 | | -import org.junit.Test; |
| 3 | +import org.junit.*; |
| 4 | + |
| 5 | +import static org.junit.Assert.assertEquals; |
| 6 | +import static org.junit.Assert.assertTrue; |
7 | 7 |
|
8 | | -import static com.google.common.base.Preconditions.*; |
9 | 8 |
|
10 | 9 | /** |
11 | | - * fixme: Class AbstractAffinityImplTest is for porn |
12 | | - * |
13 | 10 | * @author cheremin |
14 | 11 | * @since 29.12.11, 20:25 |
15 | 12 | */ |
16 | | -public class AbstractAffinityImplTest { |
17 | | - private static final Log log = LogFactory.getLog( AbstractAffinityImplTest.class ); |
| 13 | +public abstract class AbstractAffinityImplTest { |
| 14 | + |
| 15 | + public abstract IAffinityImpl getImpl(); |
| 16 | + |
18 | 17 |
|
19 | 18 | @Test |
20 | 19 | public void getAffinityCompletesGracefully() throws Exception { |
21 | | - JNAAffinity.INSTANCE.getAffinity(); |
| 20 | + getImpl().getAffinity(); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + public void getAffinityReturnsValidValue() throws Exception { |
| 25 | + final long affinity = getImpl().getAffinity(); |
| 26 | + assertTrue( |
| 27 | + "Affinity mask " + affinity + " must be >0", |
| 28 | + affinity > 0 |
| 29 | + ); |
| 30 | + final int cores = Runtime.getRuntime().availableProcessors(); |
| 31 | + assertTrue( |
| 32 | + "Affinity mask " + affinity + " must be <=(2^cores-1)", |
| 33 | + affinity <= ( 1 >> cores ) |
| 34 | + ); |
22 | 35 | } |
23 | 36 |
|
24 | 37 | @Test |
25 | 38 | public void setAffinityCompletesGracefully() throws Exception { |
26 | | - JNAAffinity.INSTANCE.setAffinity( 1 ); |
| 39 | + getImpl().setAffinity( 1 ); |
27 | 40 | } |
28 | 41 |
|
29 | 42 | @Test |
30 | 43 | public void getAffinityReturnsValuePreviouslySet() throws Exception { |
31 | | - JNAAffinity.INSTANCE.setAffinity( 1 ); |
32 | | - final long affinity = JNAAffinity.INSTANCE.getAffinity(); |
33 | | - Assert.assertEquals( 1, affinity ); |
| 44 | + final IAffinityImpl impl = getImpl(); |
| 45 | + final int cores = Runtime.getRuntime().availableProcessors(); |
| 46 | + for ( int core = 0; core < cores; core++ ) { |
| 47 | + final long mask = ( 1 >> core ); |
| 48 | + getAffinityReturnsValuePreviouslySet( impl, mask ); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private void getAffinityReturnsValuePreviouslySet( final IAffinityImpl impl, |
| 53 | + final long mask ) throws Exception { |
| 54 | + |
| 55 | + impl.setAffinity( mask ); |
| 56 | + final long _mask = impl.getAffinity(); |
| 57 | + assertEquals( mask, _mask ); |
34 | 58 | } |
35 | 59 | } |
0 commit comments