From b512b0e30a7ba1c145f0139d3d7d2c3ef17afb70 Mon Sep 17 00:00:00 2001 From: minhqdao Date: Wed, 6 Mar 2024 16:31:35 +0300 Subject: [PATCH] Remove examples, remove more code, fix tests --- example/ex1.f90 | 51 -------------- example/ex2.f90 | 66 ------------------ src/version_f.f90 | 148 +--------------------------------------- test/version_f_test.f90 | 16 +++-- 4 files changed, 14 insertions(+), 267 deletions(-) delete mode 100644 example/ex1.f90 delete mode 100644 example/ex2.f90 diff --git a/example/ex1.f90 b/example/ex1.f90 deleted file mode 100644 index bef62c6..0000000 --- a/example/ex1.f90 +++ /dev/null @@ -1,51 +0,0 @@ -!> A program that loads boxes and finds the box with the highest version number. -program ex1 - use version_f, only: version_t - - implicit none - - type box - character(len=:), allocatable :: name - character(len=:), allocatable :: version - end type - - type(box), allocatable :: loaded_boxes(:) - type(version_t), allocatable :: versions(:) - type(box) :: latest_box - type(version_t) :: highest_version - integer :: i - - call load_boxes(loaded_boxes) - - ! Create and register versions - allocate (versions(size(loaded_boxes))) - do i = 1, size(loaded_boxes) - versions(i) = version_t(loaded_boxes(i)%version) - end do - - ! Find the highest version number - highest_version = versions(1) - latest_box = loaded_boxes(1) - do i = 1, size(versions) - if (versions(i) > highest_version) then - highest_version = versions(i) - latest_box = loaded_boxes(i) - end if - end do - - print *, "The latest box is '", latest_box%name, "'." - print *, "It has the version '", highest_version%to_string(), "'." - -contains - - subroutine load_boxes(boxes) - type(box), allocatable, intent(out) :: boxes(:) - - boxes = [ & - & box('nice box', '1.0'), & - & box('prototype box', '0.0.1'), & - & box('nicest box', '2.1'), & - & box('nearly nicest box', '2.1.0-alpha+1') & - & ] - end -end diff --git a/example/ex2.f90 b/example/ex2.f90 deleted file mode 100644 index 3c0c39d..0000000 --- a/example/ex2.f90 +++ /dev/null @@ -1,66 +0,0 @@ -!> This program loads a list of versions and checks whether they satisfy given version ranges. -program ex2 - use version_f - - implicit none - - type(version_t), allocatable :: all_versions(:) - type(version_t), allocatable :: smaller_versions(:) - type(version_t), allocatable :: versions_between(:) - type(version_t), allocatable :: greater_versions(:) - integer :: i - - call load_versions(all_versions) - - allocate (smaller_versions(0)) - allocate (versions_between(0)) - allocate (greater_versions(0)) - - do i = 1, size(all_versions) - if (all_versions(i)%satisfies('<1.0.0')) then - smaller_versions = [smaller_versions, all_versions(i)] - else if (all_versions(i)%satisfies('>=1.0.0 <2.0.0')) then - versions_between = [versions_between, all_versions(i)] - else if (all_versions(i)%satisfies('>=2.0.0')) then - greater_versions = [greater_versions, all_versions(i)] - end if - end do - - print *, 'Versions less than 1.0.0:' - do i = 1, size(smaller_versions) - print *, smaller_versions(i)%to_string() - end do - - print *, '' - print *, 'Versions greater or equal 1.0.0 and less than 2.0.0:' - do i = 1, size(versions_between) - print *, versions_between(i)%to_string() - end do - - print *, '' - print *, 'Versions greater or equal 2.0.0:' - do i = 1, size(greater_versions) - print *, greater_versions(i)%to_string() - end do - -contains - - subroutine load_versions(versions) - type(version_t), allocatable, intent(out) :: versions(:) - - versions = [ & - & version_t(0, 2, 5), & - & version_t(1, 2, 4, 'alpha'), & - & version_t(2, 5), & - & version_t(1, 2), & - & version_t(2, build='one'), & - & version_t(1, 2, 4), & - & version_t(0, 2, 5, 'pre'), & - & version_t(2, prerelease='pre'), & - & version_t(1, 2, 4, 'alpha', '1'), & - & version_t(2, 3, 5), & - & version_t(0, 99, 999) & - & ] - end - -end diff --git a/src/version_f.f90 b/src/version_f.f90 index 0b404b9..00c5250 100644 --- a/src/version_f.f90 +++ b/src/version_f.f90 @@ -5,7 +5,7 @@ module version_f implicit none private - public :: version_t, string_t, error_t, is_version, version_range_t, & + public :: version_t, string_t, error_t, version_range_t, & comparator_set_t, comparator_t, operator_index type :: string_t @@ -37,9 +37,7 @@ module version_f contains - procedure :: to_string, increment_major, increment_minor, increment_patch, & - & increment_prerelease, increment_build, is_exactly, satisfies, try_satisfy, & - & satisfies_comp_set, satisfies_comp, is_stable + procedure :: to_string, try_satisfy, satisfies_comp_set, satisfies_comp generic :: create => try_create procedure, private :: try_create @@ -253,73 +251,6 @@ pure function to_string(this) result(str) end if end - !> Increments the major version number and resets the minor and patch number - !> as well as the prerelease and build data. - elemental subroutine increment_major(this) - class(version_t), intent(inout) :: this - - this%major = this%major + 1 - this%minor = 0 - this%patch = 0 - - if (allocated(this%prerelease)) deallocate (this%prerelease) - if (allocated(this%build)) deallocate (this%build) - end - - !> Increments the minor version number and resets patch, prerelease and build. - elemental subroutine increment_minor(this) - class(version_t), intent(inout) :: this - - this%minor = this%minor + 1 - this%patch = 0 - - if (allocated(this%prerelease)) deallocate (this%prerelease) - if (allocated(this%build)) deallocate (this%build) - end - - !> Increments the patch version number and resets prerelease and build. - elemental subroutine increment_patch(this) - class(version_t), intent(inout) :: this - - this%patch = this%patch + 1 - - if (allocated(this%prerelease)) deallocate (this%prerelease) - if (allocated(this%build)) deallocate (this%build) - end - - !> Increment prerelease and reset build data. - elemental subroutine increment_prerelease(this) - class(version_t), intent(inout) :: this - - call increment_identifier(this%prerelease) - if (allocated(this%build)) deallocate (this%build) - end - - !> Increment build metadata. - elemental subroutine increment_build(this) - class(version_t), intent(inout) :: this - - call increment_identifier(this%build) - end - - !> Increment prerelease or build identifiers. If the last identifier is - !> numeric, increment it by 1. Otherwise add a new identifier with the value - !> 1. - pure subroutine increment_identifier(ids) - type(string_t), allocatable, intent(inout) :: ids(:) - - if (allocated(ids)) then - if (ids(size(ids))%is_numeric()) then - ids = [ids(1:size(ids) - 1), string_t(trim(int2s(ids(size(ids))%num() + 1)))] - else - ids = [ids, string_t('1')] - end if - else - allocate (ids(1)) - ids(1)%str = '1' - end if - end - !> Parse a string into a version including prerelease and build data. !> !> Wrapper function for `try_parse`. @@ -696,53 +627,6 @@ pure logical function is_greater(lhs, rhs) is_greater = size(lhs) > size(rhs) end - !> True if both versions are exactly the same including the build metadata. - !> This procedure has been added for conveniece. It is not part of the - !> Semantic Versioning 2.0.0 specification. - elemental logical function is_exactly(self, other) - class(version_t), intent(in) :: self - type(version_t), intent(in) :: other - - integer :: i - - is_exactly = self == other; - if (.not. is_exactly) return - - if (allocated(self%build) .and. allocated(other%build)) then - if (size(self%build) /= size(other%build)) then - is_exactly = .false.; return - end if - - do i = 1, size(self%build) - if (self%build(i)%str /= other%build(i)%str) then - is_exactly = .false.; return - end if - end do - else if (allocated(self%build) .or. allocated(other%build)) then - is_exactly = .false.; return - end if - end - - !> True if the string can be parsed as a valid `version_t`. Use `parse` if you - !> wish to receive detailed error messages. In strict mode, all major, minor - !> and patch versions must be provided. Implicit zeros are forbidden in strict - !> mode. - logical function is_version(str, strict_mode) - - !> Input string. - character(*), intent(in) :: str - - !> If true, all major, minor and patch versions must be provided. Implicit - !> zeros are forbidden in strict mode. - logical, optional, intent(in) :: strict_mode - - type(version_t) :: version - type(error_t), allocatable :: error - - call version%parse(str, error, strict_mode) - is_version = .not. allocated(error) - end - !> Helper function to generate a new `string_t` instance. elemental function create_string_t(inp_str) result(string) @@ -825,24 +709,6 @@ subroutine try_satisfy(this, string, is_satisfied, error) end do end - !> Convenience function to determine whether the version meets the comparison. - !> - !> Wrapper function for `try_satisfy`, which returns `.false.` if the - !> comparison fails. - logical function satisfies(this, str) - - !> Instance of `version_t` to be evaluated. - class(version_t), intent(in) :: this - - !> Input string to be evaluated. - character(*), intent(in) :: str - - type(error_t), allocatable :: error - - call this%try_satisfy(str, satisfies, error) - if (allocated(error)) satisfies = .false. - end - !> Create sets of comparators that are separated by `||`. An example of a !> version range is `4.2.3 || 5.0.0 - 7.2.3`. subroutine parse_version_range(this, string, error) @@ -1084,14 +950,4 @@ pure function create_comp_set(comps) result(comp_set) comp_set%comps = comps end - - !> Returns true if the version is stable. A version is stable if its major - !> version is greater than zero and the version is not a prerelease. - elemental logical function is_stable(version) - - !> Instance of `version_t` to be evaluated. - class(version_t), intent(in) :: version - - is_stable = version%major > 0 .and. .not. allocated(version%prerelease) - end end diff --git a/test/version_f_test.f90 b/test/version_f_test.f90 index ee56d3f..2c18087 100644 --- a/test/version_f_test.f90 +++ b/test/version_f_test.f90 @@ -3,11 +3,8 @@ program test implicit none - type(version_t) :: v1, v2 + type(version_t) :: v1 logical :: is_satisfied - type(comparator_t), allocatable :: comps(:) - type(comparator_set_t) :: comp_set - type(version_range_t) :: range type(error_t), allocatable :: e !##################################try_satisfy#################################! @@ -242,3 +239,14 @@ program test call v1%try_satisfy('0.1.0+abc', is_satisfied, e) if (.not. is_satisfied) call fail('satisfy-58 should satisfy.') if (allocated(e)) call fail('satisfy-58 should not fail.') + + print *, achar(10)//achar(27)//'[92m All tests passed.'//achar(27) + +contains + + subroutine fail(msg) + character(*), intent(in) :: msg + print *, achar(27)//'[31m'//'Test failed: '//msg//achar(27)//'[0m' + stop 1 + end +end program