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
16TESTDIR=/tmp/test-emma/$$
17JAVADIR=$TESTDIR/android3/java
18SOURCEDIR=$JAVADIR/com/android/bunnies
19mkdir -p $SOURCEDIR
20
21cat <<END >$SOURCEDIR/Bunny.java
22package com.android.bunnies;
23
24import java.util.Random;
25
26public 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}
58END
59cat <<END >$SOURCEDIR/SubBunny.java
60package com.android.bunnies;
61import com.android.bunnies.Bunny;
62class 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}
74END
75
76GOLDEN=$TESTDIR/golden.lcov
77cat <<END >$GOLDEN
78SF:com/android/bunnies/SubBunny.java
79FN:5,SubBunny::addOne (int): int
80FNDA:1,SubBunny::addOne (int): int
81FN:12,SubBunny::identity (int): boolean
82FNDA:1,SubBunny::identity (int): boolean
83FN:3,SubBunny::SubBunny (): void
84FNDA:1,SubBunny::SubBunny (): void
85DA:3,1
86DA:5,1
87DA:6,1
88DA:12,1
89end_of_record
90SF:com/android/bunnies/Bunny.java
91FN:23,Bunny::dontAddOne (int): int
92FNDA:0,Bunny::dontAddOne (int): int
93FN:27,Bunny::identity (int): boolean
94FNDA:1,Bunny::identity (int): boolean
95FN:16,Bunny::addOne (int): int
96FNDA:1,Bunny::addOne (int): int
97FN:5,Bunny::Bunny (): void
98FNDA:1,Bunny::Bunny (): void
99FN:31,Bunny::main (String []): void
100FNDA:1,Bunny::main (String []): void
101DA:5,1
102DA:6,1
103DA:11,1
104DA:12,1
105DA:13,1
106DA:16,1
107DA:17,1
108DA:23,0
109DA:27,1
110DA:31,1
111DA:32,1
112DA:33,1
113DA:34,1
114DA:35,1
115end_of_record
116END
117
118javac -g $(find $SOURCEDIR -name \*.java)
119
120COVERAGE=$TESTDIR/coverage.dat
121java -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
126if ! diff <(sort $GOLDEN) <(sort $COVERAGE) >$TESTDIR/diff-sorted; then
127  echo Tests failed: Additional or missing lines: See $TESTDIR/diff-sorted
128  exit
129fi
130if ! diff $GOLDEN $COVERAGE >$TESTDIR/diff-ordered; then
131  echo Tests failed: same lines, different order: See $TESTDIR/diff-ordered
132  exit
133fi
134rm -rf $TESTDIR
135echo Tests passed.
136
137