Software Development (CSC 321 2016F) : EBoards

CSC321.01 2016F, Class 15: Test-Driven Development


Overview

Preliminaries

Admin

Upcoming Work

Good Things to Do

See http://www.cs.grinnell.edu/~rebelsky/s2d@g/

Questions

Refresher: Incorrect code

What does this program try to do?

class TimeSetter
  def self.convert(d)
    y = 1980
    while (d > 365) do
      if ((y % 400 == 0) || (y % 4 == 0) && (y % 100 != 0))
        if (d > 366)
          d -= 366
          y += 1
        end
      else
        d -= 365
        y += 1
      end
    end
    return [d,y]
  end
end

What does this program try to do?

What tests would you run?

What is wrong with this program?

How would we find it?

How would we fix it?

class TimeSetter
  def self.convert(d)
    // Set y to the start of time
    y = 1980
    // As long as we are beyond the end of the year
    while (d > 365) do
      // If it's a leap year
      if ((y % 400 == 0) || (y % 4 == 0) && (y % 100 != 0))
        // And there are enough days left in the year
        if (d > 366)
          // Move on to the next year
          d -= 366
          y += 1
        end
      else
        // Move on to the next year
        d -= 365
        y += 1
      end
    end
    return [d,y]
  end
end

Loop invariants

class TimeSetter
  def self.convert(d)
    // Let Y be 1980 and D be the initial value of d
    y = 1980
    // Invariant: Day D of 1980 is equivalent to day d of y.
    // "Size": d
    while (d > 365) do
      if ((y % 400 == 0) || (y % 4 == 0) && (y % 100 != 0))
        if (d > 366)
          d -= 366
          y += 1
          // Maintains invariant
          // Shrinks d
        end
        // Maintains the invariant
        // DOESN'T CHANGE d
      else
        d -= 365
        y += 1
        // Maintains invariant
        // Shrinks d
      end
    end
    return [d,y]
  end
end

FIX IT!

class TimeSetter
  def self.convert(d)
    y = 1980
    while (d > 365) do
      if ((y % 400 == 0) || (y % 4 == 0) && (y % 100 != 0))
        if (d >= 366)
          d -= 366
          y += 1
        end
      else
        d -= 365
        y += 1
      end
    end
    return [d,y]
  end
end
class TimeSetter
  def self.convert(d)
    y = 1980
    while (d > 365) do
      if ((y % 400 == 0) || (y % 4 == 0) && (y % 100 != 0))
        if (d > 366)
          d -= 366
          y += 1
        else if (d == 366)
          return [d,y] // UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH  UGH 
        end
      else
        d -= 365
        y += 1
      end
    end
    return [d,y]
  end
end
class TimeSetter
  def self.convert(d)
    y = 1980
    while (d > 366) do
      if ((y % 400 == 0) || (y % 4 == 0) && (y % 100 != 0))
        d -= 366
        y += 1
      else
        d -= 365
        y += 1
      end
    end
    return [d,y]
  end
end
class TimeSetter
  def self.convert(d)
    y = 1980
    while (d > 365) do
      if ((y % 400 == 0) || (y % 4 == 0) && (y % 100 != 0))
        if (d > 366)
          d -= 366
          y += 1
        end
      else
        d -= 365
        y += 1
      end
    end
    return [d,y]
  end
end
  while (d > daysInYear(y)) {
    d -= daysInYear(y);
    y += 1;
  }
  return [d,y]

  def self.daysInYear(y) {
    if ((y % 400 == 0) || (y % 4 == 0) && (y % 100 != 0)) {
      return 366;
    }
    else {
      return 365;
    }
  }

Global Surveillance

"I would like to discuss the idea of global surveillance and where to draw the line. It's a grey area for me and I'd like to get more opinions on it."