/*----------------------------------------------------------------------
Prof-It for C#
Copyright (c) 2004 Klaus Lehner, University of Linz
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
----------------------------------------------------------------------*/
using System;
using System.Collections;
namespace at.jku.ssw.ProfIt.Components{
/// <summary>
/// A CounterCollection contains the blocks of a file. As an ID it holds
/// the absolute path of the file
/// </summary>
[Serializable]
public class CounterCollection {
public ArrayList blocks = new ArrayList();
internal ArrayList methods = new ArrayList();
internal ArrayList classes = new ArrayList();
private ArrayList braces = new ArrayList();
private DateTime lastParseTime;
/// <summary>
/// This is the unique ID of the CounterCollection
/// </summary>
private string fileName;
[NonSerialized]
private int numberOfStatements = -1;
[NonSerialized]
private int maximumCounter = -1;
[NonSerialized]
private int numberOfExecutedStatements = -1;
[NonSerialized]
private float codeCoverage = -1;
public CounterCollection(string fileName) {
this.fileName = fileName;
}
public CounterCollection() {
}
public string FileName {
get { return this.fileName; }
}
internal void SetFileName(string fileName) {
this.fileName = fileName;
}
public DateTime LastParseTime {
get { return lastParseTime; }
set { this.lastParseTime = value; }
}
public void AddBlock (IBlock block) {
blocks.Add(block);
}
public void AddMethod (MethodCounter block) {
methods.Add(block);
}
public void AddClass (ClassCounter block) {
classes.Add(block);
}
/// <summary>
/// only show "positive" blocks (blocks where start is less than end position)
/// </summary>
public ArrayList VisibleBlocks {
get {
ArrayList list = new ArrayList();
foreach (IBlock block in blocks) {
if (block.Start < block.End && block.Statements>0) list.Add(block);
}
return list;
}
}
public ArrayList MethodCounters {
get {
return this.methods;
}
}
public ArrayList ClassCounters {
get {
return this.classes;
}
}
/// <summary>
/// Removes all basic blocks that don't contain any statements and
/// are not refered by CalcBlocks
/// </summary>
public void RemoveEmptyBlocks() {
int idCounter = 0;
ArrayList negativeBlocks = new ArrayList();
ArrayList refBlocks = new ArrayList();
// first filter out all negative blocks and all refBlocks
foreach (IBlock block in blocks) {
if (block.Start >= block.End) {
negativeBlocks.Add(block);
}
if (block is CalcBlock) {
refBlocks.Add(((CalcBlock)block).Ref);
}
}
// copy all delegates among the negative blocks to another list, remove all other ones
foreach (IBlock block in negativeBlocks) {
if (!refBlocks.Contains(block)) {
int indexOfBlock = blocks.IndexOf(block);
if (indexOfBlock < blocks.Count -1) {
IBlock nextBlock = (IBlock)blocks[blocks.IndexOf(block)+1];
foreach (MethodCounter method in methods) {
if (method.Ref == block && method.blocks.Contains(nextBlock)) {
method.Ref = nextBlock;
}
}
}
blocks.Remove(block);
}
}
// set the id for all StatementBlocks to speed up the increment at runtime
foreach (IBlock block in blocks) {
if (block is StatementBlock) {
((StatementBlock)block).ID = idCounter;
}
idCounter++;
}
}
/// <summary>
/// Resets the counters of all blocks in the file
/// </summary>
public void ResetCounters() {
foreach (IBlock block in blocks) {
if (block is StatementBlock) {
((StatementBlock)block).Reset();
}
}
CalculateStatisticalValues();
}
public void AddBrace(int position, Brace brace) {
braces.Insert(0,new BracePosition(position, brace));
}
public ArrayList Braces { get { return this.braces; } }
#region statistical functions
public void CalculateStatisticalValues() {
this.numberOfStatements = 0;
this.maximumCounter = 0;
this.numberOfExecutedStatements = 0;
foreach (IBlock block in blocks) {
this.numberOfStatements += block.Statements;
if (block.Counter > maximumCounter) maximumCounter = block.Counter;
if (block.Counter > 0) numberOfExecutedStatements += block.Statements;
}
this.codeCoverage = ((float)NumberOfExecutedStatements / (float)NumberOfStatements) * 100;
}
public int NumberOfBlocks { get { return VisibleBlocks.Count; } }
public int NumberOfStatements { get { return this.numberOfStatements; } }
public int NumberOfMethods { get { return methods.Count; } }
public int NumberOfClasses { get { return classes.Count; } }
public int MaximumCounter { get { return maximumCounter; } }
public float CodeCoverage { get { return this.codeCoverage; } }
public int NumberOfExecutedStatements { get { return this.numberOfExecutedStatements; } }
#endregion
}
public enum Brace { None=1, Left=2, Right=4 }
[Serializable]
public class BracePosition {
private int pos;
private Brace brace;
public BracePosition(int pos, Brace brace) {
this.pos = pos; this.brace = brace;
}
public string Value {
get {
if (brace==Brace.Left) return "{";
if (brace==Brace.Right) return "}";
return "";
}
}
public int Position { get { return this.pos; } }
}
}
|