-
-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02c5722
commit 2156377
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/org/cloud/sonic/agent/bridge/ios/CompareVersionUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.cloud.sonic.agent.bridge.ios; | ||
|
||
/** | ||
* Created by fengbincao on 2024/08/19 | ||
*/ | ||
public class CompareVersionUtil { | ||
|
||
/** | ||
* 比较版本号的大小,前者大则返回一个正数,后者大返回一个负数,相等则返回0 | ||
* | ||
* @param version1 第一个版本号 | ||
* @param version2 第二个版本号 | ||
* @return 前者大则返回一个正数, 后者大返回一个负数, 相等则返回0 | ||
*/ | ||
public static int compareVersion(String version1, String version2) { | ||
String[] v1 = version1.split("\\."), v2 = version2.split("\\."); | ||
int index = 0; | ||
for (; index < v1.length && index < v2.length; index++) { | ||
int v1n = Integer.parseInt(v1[index]), v2n = Integer.parseInt(v2[index]); | ||
if (v1n > v2n) return 1; | ||
if (v1n < v2n) return -1; | ||
} | ||
if (index < v1.length) { | ||
for (; index < v1.length; index++) { | ||
int v1n = Integer.parseInt(v1[index]); | ||
if (v1n > 0) return 1; | ||
} | ||
} | ||
if (index < v2.length) { | ||
for (; index < v2.length; index++) { | ||
int v2n = Integer.parseInt(v2[index]); | ||
if (v2n > 0) return -1; | ||
} | ||
} | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/test/java/org/cloud/sonic/agent/bridge/ios/CompareVersionUtilTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.cloud.sonic.agent.bridge.ios; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* Created by fengbincao on 2024/08/19 | ||
*/ | ||
public class CompareVersionUtilTest { | ||
@Test | ||
public void compareVersion1() { | ||
String version1 = "17.1.0"; | ||
String version2 = "17.0.0"; | ||
Assert.assertTrue(CompareVersionUtil.compareVersion(version1, version2) > 0); | ||
} | ||
|
||
@Test | ||
public void compareVersion2() { | ||
String version1 = "17.1.0"; | ||
String version2 = "17"; | ||
Assert.assertTrue(CompareVersionUtil.compareVersion(version1, version2) > 0); | ||
} | ||
|
||
@Test | ||
public void compareVersion3() { | ||
String version1 = "17.1"; | ||
String version2 = "17.0"; | ||
Assert.assertTrue(CompareVersionUtil.compareVersion(version1, version2) > 0); | ||
} | ||
|
||
@Test | ||
public void compareVersion4() { | ||
String version1 = "17.0.0"; | ||
String version2 = "17"; | ||
assertEquals(0, CompareVersionUtil.compareVersion(version1, version2)); | ||
} | ||
|
||
@Test | ||
public void compareVersion5() { | ||
String version1 = "16.8"; | ||
String version2 = "17.0"; | ||
Assert.assertTrue(CompareVersionUtil.compareVersion(version1, version2) < 0); | ||
} | ||
|
||
@Test | ||
public void compareVersion6() { | ||
String version1 = "16"; | ||
String version2 = "17"; | ||
Assert.assertTrue(CompareVersionUtil.compareVersion(version1, version2) < 0); | ||
} | ||
|
||
@Test | ||
public void compareVersion7() { | ||
String version1 = "18.0.1"; | ||
String version2 = "17.0"; | ||
Assert.assertTrue(CompareVersionUtil.compareVersion(version1, version2) > 0); | ||
} | ||
} |