当前位置:   article > 正文

慕测-软件测试NextDay_软件测试nextday源代码

软件测试nextday源代码

QUESTION

  1. package net.mooctest;
  2. public class Date {
  3. private Day d;
  4. private Month m;
  5. private Year y;
  6. public Date(int pMonth, int pDay, int pYear) {
  7. y = new Year(pYear);
  8. m = new Month(pMonth, y);
  9. d = new Day(pDay, m);
  10. }
  11. public void increment() {
  12. if (!d.increment()) {
  13. if (!m.increment()) {
  14. y.increment();
  15. m.setMonth(1, y);
  16. }
  17. d.setDay(1, m);
  18. }
  19. }
  20. public void printDate() {
  21. System.out.println(m.getMonth() + "/" + d.getDay() + "/" + y.getYear());
  22. }
  23. public Day getDay() {
  24. return d;
  25. }
  26. public Month getMonth() {
  27. return m;
  28. }
  29. public Year getYear() {
  30. return y;
  31. }
  32. public boolean equals(Object o) {
  33. if (o instanceof Date) {
  34. if (this.y.equals(((Date) o).y) && this.m.equals(((Date) o).m)
  35. && this.d.equals(((Date) o).d))
  36. return true;
  37. }
  38. return false;
  39. }
  40. public String toString() {
  41. return (m.getMonth() + "/" + d.getDay() + "/" + y.getYear());
  42. }
  43. }
  1. package net.mooctest;
  2. public class Month extends CalendarUnit {
  3. private Year y;
  4. private int[] sizeIndex = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  5. public Month(int pMonth, Year y) {
  6. setMonth(pMonth, y);
  7. }
  8. public void setMonth(int pMonth, Year y) {
  9. setCurrentPos(pMonth);
  10. this.y = y;
  11. if (!this.isValid()) {
  12. throw new IllegalArgumentException("Not a valid month");
  13. }
  14. }
  15. public int getMonth() {
  16. return currentPos;
  17. }
  18. public int getMonthSize() {
  19. if (y.isLeap())
  20. sizeIndex[1] = 29;
  21. else
  22. sizeIndex[1] = 28;
  23. return sizeIndex[currentPos - 1];
  24. }
  25. public boolean increment() {
  26. currentPos += 1;
  27. if (currentPos > 12)
  28. return false;
  29. else
  30. return true;
  31. }
  32. public boolean isValid() {
  33. if (y != null && y.isValid())
  34. if (this.currentPos >= 1 && this.currentPos <= 12)
  35. return true;
  36. return false;
  37. }
  38. public boolean equals(Object o) {
  39. if (o instanceof Month) {
  40. if (this.currentPos == ((Month) o).currentPos
  41. && this.y.equals(((Month) o).y))
  42. return true;
  43. }
  44. return false;
  45. }
  46. }
  1. package net.mooctest;
  2. public class Year extends CalendarUnit {
  3. public Year(int pYear) {
  4. setYear(pYear);
  5. }
  6. public void setYear(int pYear) {
  7. setCurrentPos(pYear);
  8. if (!this.isValid()) {
  9. throw new IllegalArgumentException("Not a valid month");
  10. }
  11. }
  12. public int getYear() {
  13. return currentPos;
  14. }
  15. public boolean increment() {
  16. currentPos = currentPos + 1;
  17. if (currentPos == 0)
  18. currentPos = 1;
  19. return true;
  20. }
  21. public boolean isLeap() {
  22. if (currentPos >= 0
  23. && (((currentPos % 4 == 0) && (currentPos % 100 != 0)) || (currentPos % 400 == 0)))
  24. return true;
  25. else if (currentPos < 0
  26. && ((((currentPos * -1) % 4 == 1) && ((currentPos * -1) % 100 != 1)) || ((currentPos * -1) % 400 == 1)))
  27. return true;
  28. return false;
  29. }
  30. protected boolean isValid() {
  31. if (this.currentPos != 0)
  32. return true;
  33. return false;
  34. }
  35. public boolean equals(Object o) {
  36. if (o instanceof Year) {
  37. if (this.currentPos == ((Year) o).currentPos)
  38. return true;
  39. }
  40. return false;
  41. }
  42. }
  1. package net.mooctest;
  2. public abstract class CalendarUnit {
  3. protected int currentPos;
  4. protected void setCurrentPos(int pCurrentPos) {
  5. currentPos = pCurrentPos;
  6. }
  7. protected int getCurrentPos() {
  8. return currentPos;
  9. }
  10. protected abstract boolean increment();
  11. protected abstract boolean isValid();
  12. }
  1. package net.mooctest;
  2. public class Nextday {
  3. public static Date nextDay(Date d) {
  4. Date dd = new Date(d.getMonth().getCurrentPos(), d.getDay().getCurrentPos(), d.getYear().getCurrentPos());
  5. dd.increment();
  6. return dd;
  7. }
  8. }

ANSWER ()

  1. package net.mooctest;
  2. import static org.junit.Assert.*;
  3. import org.junit.Test;
  4. //我习惯从功能分析,次日函数是干嘛的,是输入今天告诉我明天。
  5. //所以我们需要考虑今天到明天的日期变化。
  6. public class NextdayTest {
  7. @Test
  8. public void testValid() {
  9. Nextday n = new Nextday();
  10. //首先是最简单的只增长date,我们可以随意挑选一个日期,
  11. //验证次日函数给我们的结果是否和已知的次日日期相同:
  12. Date currentday=new Date(2,22,2023);
  13. Date nextday=Nextday.nextDay(currentday);
  14. Date expectday=new Date(2,23,2023);
  15. assertEquals(expectday,nextday);
  16. //然后考虑date满了进到月份,相同的方法去验证:
  17. currentday=new Date(1,31,2023);
  18. nextday=Nextday.nextDay(currentday);
  19. expectday=new Date(2,1,2023);
  20. assertEquals(expectday,nextday);
  21. //如果说月份也满了,就要加年份
  22. currentday=new Date(12,31,2022);
  23. nextday=Nextday.nextDay(currentday);
  24. expectday=new Date(1,1,2023);
  25. assertEquals(expectday,nextday);
  26. //这些都是理所应当一定会发生的,非常好想
  27. //然后我们需要考虑一些奇奇怪怪的日子
  28. //首先能想到就是2月的分界点28、29
  29. //注意我们这里都是先讨论的可以发生的情况,像5201314月10086日这种日子我们先不管他
  30. currentday=new Date(2,28,2020);
  31. nextday=Nextday.nextDay(currentday);
  32. expectday=new Date(2,29,2020);
  33. assertEquals(expectday,nextday);
  34. currentday=new Date(2,27,2023);
  35. nextday=Nextday.nextDay(currentday);
  36. expectday=new Date(2,28,2023);
  37. assertEquals(expectday,nextday);
  38. //因为题目中有
  39. //else if (currentPos < 0 && ((((currentPos * -1) % 4 == 1) &&
  40. //((currentPos * -1) % 100 != 1)) || ((currentPos * -1) % 400 == 1)))
  41. // return true;
  42. //这样的判断,所以年份可能会是个负数
  43. currentday=new Date(2,28,-2017);
  44. nextday=Nextday.nextDay(currentday);
  45. expectday=new Date(2,29,-2017);
  46. assertEquals(expectday,nextday);
  47. currentday=new Date(2,28,-2018);
  48. nextday=Nextday.nextDay(currentday);
  49. expectday=new Date(3,1,-2018);
  50. assertEquals(expectday,nextday);
  51. //-2017和-2018覆盖了
  52. //&& ((((currentPos * -1) % 4 == 1) && ((currentPos * -1) % 100 != 1)) ||((currentPos
  53. //-1) % 400 == 1)))
  54. // return true;
  55. //这条语句
  56. currentday=new Date(12,31,-1);
  57. nextday=Nextday.nextDay(currentday);
  58. expectday=new Date(1,1,1);
  59. assertEquals(expectday,nextday);
  60. currentday.printDate();
  61. currentday.toString();
  62. //接下来对day-month-year(12,31,-1)的功能分析
  63. assertEquals(31,currentday.getDay().getDay());
  64. assertEquals(12,currentday.getMonth().getMonth());
  65. assertEquals(-1,currentday.getYear().getYear());
  66. Year y1=new Year(1111);
  67. Month m1=new Month(11,y1);
  68. Day d1=new Day(20,m1);
  69. //这里就是搞一个新日期和刚刚的日期比较年月日是否相等
  70. //这样get.get和get.equal就覆盖了
  71. assertEquals(false,currentday.getDay().equals(d1));
  72. assertEquals(false,currentday.getMonth().equals(m1));
  73. assertEquals(false,currentday.getYear().equals(y1));
  74. assertEquals(false,currentday.getYear().equals(m1));
  75. assertEquals(false,currentday.getMonth().equals(y1));
  76. assertEquals(false,currentday.getDay().equals(m1));
  77. assertEquals(false,currentday.equals(y1));
  78. y1=new Year(-1);
  79. m1=new Month(12,y1);
  80. d1=new Day(31,m1);
  81. //false有了,那就要搞一些true的测试
  82. assertEquals(true,currentday.getDay().equals(d1));
  83. assertEquals(true,currentday.getMonth().equals(m1));
  84. assertEquals(true,currentday.getYear().equals(y1));
  85. //currentday.equal也要覆盖,然后注意false&true
  86. currentday=new Date(2,22,2023);
  87. nextday=new Date(2,22,2023);
  88. assertEquals(true,currentday.equals(nextday));
  89. nextday=new Date(2,23,2023);
  90. assertEquals(false,currentday.equals(nextday));
  91. }
  92. //下面来解决边界溢出的日期
  93. //日期大于31
  94. @Test(expected=IllegalArgumentException.class)
  95. public void testInvalidDay() {
  96. Year y=new Year(2023);
  97. Month m=new Month(11,y);
  98. Day d=new Day(32,m);
  99. }
  100. //月份为null
  101. @Test(expected=IllegalArgumentException.class)
  102. public void testInvalidMonth1() {
  103. Day d=new Day(2,null);
  104. }
  105. //月份超过12
  106. @Test(expected=IllegalArgumentException.class)
  107. public void testInvalidMonth2() {
  108. //月份超过12
  109. Year y=new Year(2023);
  110. Month m=new Month(13,y);
  111. }
  112. //年份null
  113. @Test(expected=IllegalArgumentException.class)
  114. public void testInvalidYear1() {
  115. Year y=null;
  116. Month m=new Month(1,y);
  117. }
  118. //年份为0
  119. @Test(expected=IllegalArgumentException.class)
  120. public void testInvalidYear2() {
  121. Year y=new Year(0);
  122. }
  123. }

RESULT

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/749249
推荐阅读
相关标签
  

闽ICP备14008679号