Tim Baverstock | 5366632 | 2009-11-17 18:41:06 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright 2009 Google Inc. All Rights Reserved. |
| 4 | # Author: weasel@google.com (Tim Baverstock) |
| 5 | # |
| 6 | # This program and the accompanying materials are made available under |
| 7 | # the terms of the Common Public License v1.0 which accompanies this |
| 8 | # distribution, and is available at http://www.eclipse.org/legal/cpl-v10.html |
| 9 | # |
| 10 | # This script tests the emma jar from the sources in this directory. |
| 11 | # This script has to be run from its current directory ONLY. |
| 12 | # Sample usages: |
| 13 | # To just test emma.jar: |
| 14 | # ./test.sh |
| 15 | |
| 16 | TESTDIR=/tmp/test-emma/$$ |
| 17 | JAVADIR=$TESTDIR/android3/java |
| 18 | SOURCEDIR=$JAVADIR/com/android/bunnies |
| 19 | mkdir -p $SOURCEDIR |
| 20 | |
| 21 | cat <<END >$SOURCEDIR/Bunny.java |
| 22 | package com.android.bunnies; |
| 23 | |
| 24 | import java.util.Random; |
| 25 | |
| 26 | public class Bunny { |
| 27 | int randomNumber1 = (new Random()).nextInt(); |
| 28 | |
| 29 | int randomNumber2; |
| 30 | |
| 31 | { |
| 32 | Random r = new Random(); |
| 33 | randomNumber2 = r.nextInt(); |
| 34 | } |
| 35 | |
| 36 | int addOne(int a) { |
| 37 | int b = a + 1; |
| 38 | return identity(a + 1) |
| 39 | ? 1 |
| 40 | : 0; |
| 41 | } |
| 42 | |
| 43 | int dontAddOne(int a) { |
| 44 | return a; |
| 45 | } |
| 46 | |
| 47 | boolean identity(int a) { |
| 48 | return a != a; |
| 49 | } |
| 50 | |
| 51 | public static void main(String[] args) { |
| 52 | Bunny thisThing = new Bunny(); |
| 53 | SubBunny thatThing = new SubBunny(); |
| 54 | System.out.println(thisThing.addOne(2)); |
| 55 | System.out.println(thatThing.addOne(2)); |
| 56 | } |
| 57 | } |
| 58 | END |
| 59 | cat <<END >$SOURCEDIR/SubBunny.java |
| 60 | package com.android.bunnies; |
| 61 | import com.android.bunnies.Bunny; |
| 62 | class SubBunny extends Bunny { |
| 63 | int addOne(int a) { |
| 64 | int b = a + 2; |
| 65 | return identity(a) && identity(b) || identity(b) |
| 66 | ? 1 |
| 67 | : 0; |
| 68 | } |
| 69 | |
| 70 | boolean identity(int a) { |
| 71 | return a == a; |
| 72 | } |
| 73 | } |
| 74 | END |
| 75 | |
| 76 | GOLDEN=$TESTDIR/golden.lcov |
| 77 | cat <<END >$GOLDEN |
| 78 | SF:com/android/bunnies/SubBunny.java |
| 79 | FN:5,SubBunny::addOne (int): int |
| 80 | FNDA:1,SubBunny::addOne (int): int |
| 81 | FN:12,SubBunny::identity (int): boolean |
| 82 | FNDA:1,SubBunny::identity (int): boolean |
| 83 | FN:3,SubBunny::SubBunny (): void |
| 84 | FNDA:1,SubBunny::SubBunny (): void |
| 85 | DA:3,1 |
| 86 | DA:5,1 |
| 87 | DA:6,1 |
| 88 | DA:12,1 |
| 89 | end_of_record |
| 90 | SF:com/android/bunnies/Bunny.java |
| 91 | FN:23,Bunny::dontAddOne (int): int |
| 92 | FNDA:0,Bunny::dontAddOne (int): int |
| 93 | FN:27,Bunny::identity (int): boolean |
| 94 | FNDA:1,Bunny::identity (int): boolean |
| 95 | FN:16,Bunny::addOne (int): int |
| 96 | FNDA:1,Bunny::addOne (int): int |
| 97 | FN:5,Bunny::Bunny (): void |
| 98 | FNDA:1,Bunny::Bunny (): void |
| 99 | FN:31,Bunny::main (String []): void |
| 100 | FNDA:1,Bunny::main (String []): void |
| 101 | DA:5,1 |
| 102 | DA:6,1 |
| 103 | DA:11,1 |
| 104 | DA:12,1 |
| 105 | DA:13,1 |
| 106 | DA:16,1 |
| 107 | DA:17,1 |
| 108 | DA:23,0 |
| 109 | DA:27,1 |
| 110 | DA:31,1 |
| 111 | DA:32,1 |
| 112 | DA:33,1 |
| 113 | DA:34,1 |
| 114 | DA:35,1 |
| 115 | end_of_record |
| 116 | END |
| 117 | |
| 118 | javac -g $(find $SOURCEDIR -name \*.java) |
| 119 | |
| 120 | COVERAGE=$TESTDIR/coverage.dat |
| 121 | java -cp dist/emma.jar emmarun -r lcov -cp $JAVADIR \ |
| 122 | -sp $JAVADIR -Dreport.lcov.out.file=$COVERAGE com.android.bunnies.Bunny |
| 123 | |
| 124 | # Don't really need to test these separately, but it's useful to me for now. |
| 125 | |
| 126 | if ! diff <(sort $GOLDEN) <(sort $COVERAGE) >$TESTDIR/diff-sorted; then |
| 127 | echo Tests failed: Additional or missing lines: See $TESTDIR/diff-sorted |
| 128 | exit |
| 129 | fi |
| 130 | if ! diff $GOLDEN $COVERAGE >$TESTDIR/diff-ordered; then |
| 131 | echo Tests failed: same lines, different order: See $TESTDIR/diff-ordered |
| 132 | exit |
| 133 | fi |
| 134 | rm -rf $TESTDIR |
| 135 | echo Tests passed. |
| 136 | |