diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml index 08f664c..4c49a86 100644 --- a/.JuliaFormatter.toml +++ b/.JuliaFormatter.toml @@ -1,2 +1,3 @@ +# See https://domluna.github.io/JuliaFormatter.jl/stable/ for a list of options style = "blue" indent = 2 diff --git a/.github/ISSUE_TEMPLATE/BUG_REPORT.md b/.github/ISSUE_TEMPLATE/BUG_REPORT.md new file mode 100644 index 0000000..79518d1 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/BUG_REPORT.md @@ -0,0 +1,62 @@ +--- +name: DataGraphs.jl bug report +about: Create a bug report to help us improve DataGraphs.jl +title: "[BUG] YOUR SHORT DESCRIPTION OF THE BUG HERE" +labels: ["bug"] +assignees: '' + +--- + +**Description of bug** + +Please give a brief description of the bug or unexpected behavior here. + +**Minimal code demonstrating the bug or unexpected behavior** + +If applicable, provide a minimal code that can be run to demonstrate the bug or unexpected behavior. + +If you are unable to construct a minimal code that demonstrates the bug or unexpected behavior, provide detailed steps for how to reproduce the behavior you are seeing. + +
Minimal runnable code

+ +```julia +[YOUR MINIMAL RUNNABLE CODE HERE] +``` + +

+ + +**Expected output or behavior** + +Describe what you expected to happen. + +If you provided a minimal code that can be run to demonstrate the bug or unexpected behavior, describe what you expected the output would be. + + +**Actual output or behavior** + +Describe what actually happened. + +If you provided a minimal code that demonstrates the bug or unexpected behavior, provide the output you get from that code. If the code leads to an error or warning, include the full error or warning below. + +
Output of minimal runnable code

+ +```julia +[OUTPUT OF YOUR MINIMAL RUNNABLE CODE HERE] +``` + +

+ + +**Version information** + + - Output from `versioninfo()`: +```julia +julia> versioninfo() +[YOUR OUTPUT HERE] +``` + - Output from `using Pkg; Pkg.status("DataGraphs")`: +```julia +julia> using Pkg; Pkg.status("DataGraphs") +[YOUR OUTPUT HERE] +``` diff --git a/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md b/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md new file mode 100644 index 0000000..09292d2 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md @@ -0,0 +1,24 @@ +--- +name: DataGraphs.jl feature request +about: Suggest an idea for DataGraphs.jl +title: "[ENHANCEMENT] YOUR SHORT DESCRIPTION OF THE FEATURE REQUEST HERE" +labels: ["enhancement"] +assignees: '' + +--- + +**Is your feature request related to a problem? Please describe.** + +A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] + +**Describe the solution you'd like** + +A clear and concise description of what you want to happen. + +**Describe alternatives you've considered** + +A clear and concise description of any alternative solutions or features you've considered. + +**Additional context** + +Add any other context or screenshots about the feature request here. diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000..58a8056 --- /dev/null +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,42 @@ +# Description + +Please include a summary of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies that are required for this change. + +Fixes #(issue) + +If practical and applicable, please include a minimal demonstration of the previous behavior and new behavior below. + +
Minimal demonstration of previous behavior

+ +```julia +[YOUR MINIMAL DEMONSTRATION OF PREVIOUS BEHAVIOR] +``` + +

+ +
Minimal demonstration of new behavior

+ +```julia +[YOUR MINIMAL DEMONSTRATION OF NEW BEHAVIOR] +``` + +

+ +# How Has This Been Tested? + +Please add tests that verify your changes to a file in the `test` directory. + +Please give a summary of the tests that you added to verify your changes. + +- [ ] Test A +- [ ] Test B + +# Checklist: + +- [ ] My code follows the style guidelines of this project. Please run `using JuliaFormatter; format(".")` in the base directory of the repository (`~/.julia/dev/DataGraphs`) to format your code according to our style guidelines. +- [ ] I have performed a self-review of my own code. +- [ ] I have commented my code, particularly in hard-to-understand areas. +- [ ] I have added tests that verify the behavior of the changes I made. +- [ ] I have made corresponding changes to the documentation. +- [ ] My changes generate no new warnings. +- [ ] Any dependent changes have been merged and published in downstream modules. diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..700707c --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,7 @@ +# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates +version: 2 +updates: + - package-ecosystem: "github-actions" + directory: "/" # Location of package manifests + schedule: + interval: "weekly" diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml deleted file mode 100644 index 4a8c86f..0000000 --- a/.github/workflows/CI.yml +++ /dev/null @@ -1,60 +0,0 @@ -name: CI -on: - push: - branches: - - main - tags: '*' - pull_request: -concurrency: - # Skip intermediate builds: always. - # Cancel intermediate builds: only if it is a pull request build. - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }} -jobs: - test: - name: Julia ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }} - runs-on: ${{ matrix.os }} - strategy: - fail-fast: false - matrix: - version: - - '1.7' - - '1' - os: - - ubuntu-latest - - macOS-latest - - windows-latest - arch: - - x64 - steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@v1 - with: - version: ${{ matrix.version }} - arch: ${{ matrix.arch }} - - uses: julia-actions/cache@v1 - - uses: julia-actions/julia-buildpkg@v1 - - uses: julia-actions/julia-runtest@v1 - - uses: julia-actions/julia-processcoverage@v1 - - uses: codecov/codecov-action@v2 - with: - files: lcov.info - docs: - name: Documentation - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@v1 - with: - version: '1' - - uses: julia-actions/julia-buildpkg@v1 - - uses: julia-actions/julia-docdeploy@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - DOCUMENTER_KEY: ${{ secrets.DOCUMENTER_KEY }} - - run: | - julia --project=docs -e ' - using Documenter: DocMeta, doctest - using DataGraphs - DocMeta.setdocmeta!(DataGraphs, :DocTestSetup, :(using DataGraphs); recursive=true) - doctest(DataGraphs)' diff --git a/.github/workflows/CompatHelper.yml b/.github/workflows/CompatHelper.yml index cba9134..456fa05 100644 --- a/.github/workflows/CompatHelper.yml +++ b/.github/workflows/CompatHelper.yml @@ -1,16 +1,16 @@ -name: CompatHelper +name: "CompatHelper" + on: schedule: - cron: 0 0 * * * workflow_dispatch: +permissions: + contents: write + pull-requests: write + jobs: - CompatHelper: - runs-on: ubuntu-latest - steps: - - name: Pkg.add("CompatHelper") - run: julia -e 'using Pkg; Pkg.add("CompatHelper")' - - name: CompatHelper.main() - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - COMPATHELPER_PRIV: ${{ secrets.DOCUMENTER_KEY }} - run: julia -e 'using CompatHelper; CompatHelper.main()' + compat-helper: + name: "CompatHelper" + uses: "ITensor/ITensorActions/.github/workflows/CompatHelper.yml@main" + with: + localregistry: "https://github.com/ITensor/ITensorRegistry.git" diff --git a/.github/workflows/Documentation.yml b/.github/workflows/Documentation.yml new file mode 100644 index 0000000..01a7f7a --- /dev/null +++ b/.github/workflows/Documentation.yml @@ -0,0 +1,23 @@ +name: "Documentation" + +on: + push: + branches: + - main + tags: '*' + pull_request: + schedule: + - cron: '1 4 * * 4' + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref_name != github.event.repository.default_branch || github.ref != 'refs/tags/v*' }} + +jobs: + build-and-deploy-docs: + name: "Documentation" + uses: "ITensor/ITensorActions/.github/workflows/Documentation.yml@main" + with: + localregistry: https://github.com/ITensor/ITensorRegistry.git + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/FormatCheck.yml b/.github/workflows/FormatCheck.yml new file mode 100644 index 0000000..3f78afc --- /dev/null +++ b/.github/workflows/FormatCheck.yml @@ -0,0 +1,13 @@ +name: "Format Check" + +on: + push: + branches: + - 'main' + tags: '*' + pull_request: + +jobs: + format-check: + name: "Format Check" + uses: "ITensor/ITensorActions/.github/workflows/FormatCheck.yml@main" diff --git a/.github/workflows/IntegrationTestRequest.yml b/.github/workflows/IntegrationTestRequest.yml new file mode 100644 index 0000000..d42fcca --- /dev/null +++ b/.github/workflows/IntegrationTestRequest.yml @@ -0,0 +1,14 @@ +name: "Integration Test Request" + +on: + issue_comment: + types: [created] + +jobs: + integrationrequest: + if: | + github.event.issue.pull_request && + contains(fromJSON('["OWNER", "COLLABORATOR", "MEMBER"]'), github.event.comment.author_association) + uses: ITensor/ITensorActions/.github/workflows/IntegrationTestRequest.yml@main + with: + localregistry: https://github.com/ITensor/ITensorRegistry.git diff --git a/.github/workflows/LiterateCheck.yml b/.github/workflows/LiterateCheck.yml new file mode 100644 index 0000000..2ca5f27 --- /dev/null +++ b/.github/workflows/LiterateCheck.yml @@ -0,0 +1,15 @@ +name: "Literate Check" + +on: + push: + branches: + - 'main' + tags: '*' + pull_request: + +jobs: + literate: + name: "Literate Check" + uses: "ITensor/ITensorActions/.github/workflows/LiterateCheck.yml@main" + with: + localregistry: https://github.com/ITensor/ITensorRegistry.git diff --git a/.github/workflows/Registrator.yml b/.github/workflows/Registrator.yml new file mode 100644 index 0000000..255e2af --- /dev/null +++ b/.github/workflows/Registrator.yml @@ -0,0 +1,24 @@ +name: Register Package +on: + workflow_dispatch: + pull_request: + types: + - closed + paths: + - 'Project.toml' + branches: + - 'master' + - 'main' + +permissions: + contents: write + pull-requests: write + +jobs: + Register: + if: github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true + uses: "ITensor/ITensorActions/.github/workflows/Registrator.yml@main" + with: + localregistry: ITensor/ITensorRegistry + secrets: + REGISTRATOR_KEY: ${{ secrets.REGISTRATOR_KEY }} diff --git a/.github/workflows/TagBot.yml b/.github/workflows/TagBot.yml index f49313b..0cd3114 100644 --- a/.github/workflows/TagBot.yml +++ b/.github/workflows/TagBot.yml @@ -4,6 +4,22 @@ on: types: - created workflow_dispatch: + inputs: + lookback: + default: "3" +permissions: + actions: read + checks: read + contents: write + deployments: read + issues: read + discussions: read + packages: read + pages: read + pull-requests: read + repository-projects: read + security-events: read + statuses: read jobs: TagBot: if: github.event_name == 'workflow_dispatch' || github.actor == 'JuliaTagBot' diff --git a/.github/workflows/Tests.yml b/.github/workflows/Tests.yml new file mode 100644 index 0000000..5a0a306 --- /dev/null +++ b/.github/workflows/Tests.yml @@ -0,0 +1,42 @@ +name: Tests +on: + push: + branches: + - 'master' + - 'main' + - 'release-' + tags: '*' + paths-ignore: + - 'docs/**' + pull_request: + workflow_dispatch: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + # Cancel intermediate builds: only if it is a pull request build. + cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }} + +jobs: + tests: + name: "Tests" + strategy: + fail-fast: false + matrix: + version: + - 'lts' # minimal supported version + - '1' # latest released Julia version + # group: + # - 'core' + # - 'optional' + os: + - ubuntu-latest + - macOS-latest + - windows-latest + uses: "ITensor/ITensorActions/.github/workflows/Tests.yml@main" + with: + group: "${{ matrix.group }}" + julia-version: "${{ matrix.version }}" + os: "${{ matrix.os }}" + localregistry: https://github.com/ITensor/ITensorRegistry.git + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/VersionCheck.yml b/.github/workflows/VersionCheck.yml new file mode 100644 index 0000000..69444f1 --- /dev/null +++ b/.github/workflows/VersionCheck.yml @@ -0,0 +1,11 @@ +name: "Version Check" + +on: + pull_request: + +jobs: + version-check: + name: "Version Check" + uses: "ITensor/ITensorActions/.github/workflows/VersionCheck.yml@main" + with: + localregistry: https://github.com/ITensor/ITensorRegistry.git diff --git a/.github/workflows/format_check.yml b/.github/workflows/format_check.yml deleted file mode 100644 index c48c91c..0000000 --- a/.github/workflows/format_check.yml +++ /dev/null @@ -1,35 +0,0 @@ -name: Format check -on: - push: - branches: [main] - tags: [v*] - pull_request: - -jobs: - format: - name: "Format Check" - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@v1 - with: - version: 1 - - name: Install JuliaFormatter and format - run: | - julia -e 'using Pkg; Pkg.add(PackageSpec(name="JuliaFormatter"))' - julia -e 'using JuliaFormatter; format(".", verbose=true)' - - name: Check format - run: | - julia -e ' - out = Cmd(`git diff --name-only`) |> read |> String - if out == "" - exit(0) - else - @error "The following files have not been formatted:" - write(stdout, out) - out_diff = Cmd(`git diff`) |> read |> String - @error "Diff:" - write(stdout, out_diff) - exit(1) - @error "" - end' diff --git a/.github/workflows/format_pr.yml b/.github/workflows/format_pr.yml deleted file mode 100644 index b7a9268..0000000 --- a/.github/workflows/format_pr.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: format-pr -on: - schedule: - - cron: '0 0 * * *' -jobs: - build: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - name: Install JuliaFormatter and format - run: | - julia -e 'import Pkg; Pkg.add("JuliaFormatter")' - julia -e 'using JuliaFormatter; format(".")' - # https://github.com/marketplace/actions/create-pull-request - # https://github.com/peter-evans/create-pull-request#reference-example - - name: Create Pull Request - id: cpr - uses: peter-evans/create-pull-request@v3 - with: - token: ${{ secrets.GITHUB_TOKEN }} - commit-message: Format .jl files - title: 'Automatic JuliaFormatter.jl run' - branch: auto-juliaformatter-pr - delete-branch: true - labels: formatting, automated pr, no changelog - - name: Check outputs - run: | - echo "Pull Request Number - ${{ steps.cpr.outputs.pull-request-number }}" - echo "Pull Request URL - ${{ steps.cpr.outputs.pull-request-url }}" diff --git a/.github/workflows/format_suggestions.yml b/.github/workflows/format_suggestions.yml deleted file mode 100644 index f6f268c..0000000 --- a/.github/workflows/format_suggestions.yml +++ /dev/null @@ -1,27 +0,0 @@ -name: Format suggestions - -on: - pull_request: - -concurrency: - # Skip intermediate builds: always. - # Cancel intermediate builds: only if it is a pull request build. - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }} - -jobs: - format: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2 - - uses: julia-actions/setup-julia@latest - with: - version: 1 - - run: | - julia -e 'using Pkg; Pkg.add("JuliaFormatter")' - julia -e 'using JuliaFormatter; format("."; verbose=true)' - - uses: reviewdog/action-suggester@v1 - with: - tool_name: JuliaFormatter - fail_on_error: true - filter_mode: added diff --git a/.github/workflows/register.yml b/.github/workflows/register.yml deleted file mode 100644 index 6e71f2f..0000000 --- a/.github/workflows/register.yml +++ /dev/null @@ -1,14 +0,0 @@ -name: Register Package -on: - workflow_dispatch: - inputs: - version: - description: Version to register or component to bump - required: true -jobs: - register: - runs-on: ubuntu-latest - steps: - - uses: julia-actions/RegisterAction@latest - with: - token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index 88e879c..10593a9 100644 --- a/.gitignore +++ b/.gitignore @@ -7,8 +7,8 @@ .benchmarkci .tmp .vscode/ -/docs/build/ +Manifest.toml benchmark/*.json +docs/Manifest.toml docs/build/ -Manifest.toml -test/Manifest.toml +docs/src/index.md diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..6599365 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,14 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: check-merge-conflict + - id: check-toml + - id: check-yaml + - id: end-of-file-fixer + exclude_types: [markdown] # incompatible with Literate.jl + +- repo: "https://github.com/domluna/JuliaFormatter.jl" + rev: v1.0.62 + hooks: + - id: "julia-formatter" diff --git a/LICENSE b/LICENSE index 58a8f7f..00e8195 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,201 @@ -MIT License - -Copyright (c) 2022 Matthew Fishman and contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2022 The Simons Foundation, Inc. - All Rights Reserved. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/Project.toml b/Project.toml index 48f87c4..c872ac9 100644 --- a/Project.toml +++ b/Project.toml @@ -1,13 +1,12 @@ name = "DataGraphs" uuid = "b5a273c3-7e6c-41f6-98bd-8d7f1525a36a" authors = ["Matthew Fishman and contributors"] -version = "0.2.5" +version = "0.2.6" [deps] Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19" -PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930" SimpleTraits = "699a6c99-e7fa-54fc-8d76-47d257e15c1d" [weakdeps] @@ -21,7 +20,6 @@ Dictionaries = "0.4" Graphs = "1" GraphsFlows = "0.1.1" NamedGraphs = "0.6.0" -PackageExtensionCompat = "1" SimpleTraits = "0.9" julia = "1.7" diff --git a/README.md b/README.md index 5c871f7..e065539 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,41 @@ -# DataGraphs +# DataGraphs.jl -[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://mtfishman.github.io/DataGraphs.jl/stable) -[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://mtfishman.github.io/DataGraphs.jl/dev) -[![Build Status](https://github.com/mtfishman/DataGraphs.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/mtfishman/DataGraphs.jl/actions/workflows/CI.yml?query=branch%3Amain) -[![Coverage](https://codecov.io/gh/mtfishman/DataGraphs.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/mtfishman/DataGraphs.jl) +[![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://itensor.github.io/DataGraphs.jl/stable/) +[![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://itensor.github.io/DataGraphs.jl/dev/) +[![Build Status](https://github.com/ITensor/DataGraphs.jl/actions/workflows/Tests.yml/badge.svg?branch=main)](https://github.com/ITensor/DataGraphs.jl/actions/workflows/Tests.yml?query=branch%3Amain) +[![Coverage](https://codecov.io/gh/ITensor/DataGraphs.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/ITensor/DataGraphs.jl) [![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle) +[![Aqua](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl) + +## Support + + + + Flatiron Center for Computational Quantum Physics logo. + + + +DataGraphs.jl is supported by the Flatiron Institute, a division of the Simons Foundation. + +## Installation instructions + +This package can be added as usual through the package manager: + +```julia +julia> using Pkg: Pkg + +julia> Pkg.add("DataGraphs") +``` + +## Examples + +````julia +using DataGraphs: DataGraphs +```` + +Examples go here. + +--- + +*This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).* + diff --git a/benchmark/benchmarks.jl b/benchmark/benchmarks.jl new file mode 100644 index 0000000..0c6c658 --- /dev/null +++ b/benchmark/benchmarks.jl @@ -0,0 +1,7 @@ +using DataGraphs +using BenchmarkTools + +SUITE = BenchmarkGroup() +SUITE["rand"] = @benchmarkable rand(10) + +# Write your benchmarks here. diff --git a/docs/Project.toml b/docs/Project.toml index fa1ead0..a1ebe66 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,3 +1,9 @@ [deps] DataGraphs = "b5a273c3-7e6c-41f6-98bd-8d7f1525a36a" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" +Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" + +[compat] +DataGraphs = "0.2.6" +Documenter = "1.10.0" +Literate = "2.20.1" diff --git a/docs/make.jl b/docs/make.jl index e34e4cf..a5da3cf 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,19 +1,20 @@ -using DataGraphs -using Documenter +using DataGraphs: DataGraphs +using Documenter: Documenter, DocMeta, deploydocs, makedocs DocMeta.setdocmeta!(DataGraphs, :DocTestSetup, :(using DataGraphs); recursive=true) +include("make_index.jl") + makedocs(; modules=[DataGraphs], - authors="Matthew Fishman and contributors", - repo="https://github.com/mtfishman/DataGraphs.jl/blob/{commit}{path}#{line}", + authors="ITensor developers and contributors", sitename="DataGraphs.jl", format=Documenter.HTML(; - prettyurls=get(ENV, "CI", "false") == "true", - canonical="https://mtfishman.github.io/DataGraphs.jl", - assets=String[], + canonical="https://itensor.github.io/DataGraphs.jl", + edit_link="main", + assets=["assets/favicon.ico", "assets/extras.css"], ), - pages=["Home" => "index.md"], + pages=["Home" => "index.md", "Reference" => "reference.md"], ) -deploydocs(; repo="github.com/mtfishman/DataGraphs.jl", devbranch="main") +deploydocs(; repo="github.com/ITensor/DataGraphs.jl", devbranch="main", push_preview=true) diff --git a/docs/make_index.jl b/docs/make_index.jl new file mode 100644 index 0000000..d48986f --- /dev/null +++ b/docs/make_index.jl @@ -0,0 +1,21 @@ +using Literate: Literate +using DataGraphs: DataGraphs + +function ccq_logo(content) + include_ccq_logo = """ + ```@raw html + Flatiron Center for Computational Quantum Physics logo. + Flatiron Center for Computational Quantum Physics logo. + ``` + """ + content = replace(content, "{CCQ_LOGO}" => include_ccq_logo) + return content +end + +Literate.markdown( + joinpath(pkgdir(DataGraphs), "examples", "README.jl"), + joinpath(pkgdir(DataGraphs), "docs", "src"); + flavor=Literate.DocumenterFlavor(), + name="index", + postprocess=ccq_logo, +) diff --git a/docs/make_readme.jl b/docs/make_readme.jl new file mode 100644 index 0000000..8fcd5b1 --- /dev/null +++ b/docs/make_readme.jl @@ -0,0 +1,21 @@ +using Literate: Literate +using DataGraphs: DataGraphs + +function ccq_logo(content) + include_ccq_logo = """ + + + Flatiron Center for Computational Quantum Physics logo. + + """ + content = replace(content, "{CCQ_LOGO}" => include_ccq_logo) + return content +end + +Literate.markdown( + joinpath(pkgdir(DataGraphs), "examples", "README.jl"), + joinpath(pkgdir(DataGraphs)); + flavor=Literate.CommonMarkFlavor(), + name="README", + postprocess=ccq_logo, +) diff --git a/docs/src/assets/CCQ-dark.png b/docs/src/assets/CCQ-dark.png new file mode 100644 index 0000000..fbaef52 Binary files /dev/null and b/docs/src/assets/CCQ-dark.png differ diff --git a/docs/src/assets/CCQ.png b/docs/src/assets/CCQ.png new file mode 100644 index 0000000..e13f908 Binary files /dev/null and b/docs/src/assets/CCQ.png differ diff --git a/docs/src/assets/extras.css b/docs/src/assets/extras.css new file mode 100644 index 0000000..aaab0f8 --- /dev/null +++ b/docs/src/assets/extras.css @@ -0,0 +1,15 @@ +.display-light-only { + display: block; +} + +.display-dark-only { + display: none; +} + +.theme--documenter-dark .display-light-only { + display: none; +} + +.theme--documenter-dark .display-dark-only { + display: block; +} diff --git a/docs/src/assets/favicon.ico b/docs/src/assets/favicon.ico new file mode 100644 index 0000000..0b06780 Binary files /dev/null and b/docs/src/assets/favicon.ico differ diff --git a/docs/src/assets/logo-dark.png b/docs/src/assets/logo-dark.png new file mode 100644 index 0000000..7450635 Binary files /dev/null and b/docs/src/assets/logo-dark.png differ diff --git a/docs/src/assets/logo.png b/docs/src/assets/logo.png new file mode 100644 index 0000000..2682e14 Binary files /dev/null and b/docs/src/assets/logo.png differ diff --git a/docs/src/reference.md b/docs/src/reference.md new file mode 100644 index 0000000..def9433 --- /dev/null +++ b/docs/src/reference.md @@ -0,0 +1,5 @@ +# Reference + +```@autodocs +Modules = [DataGraphs] +``` diff --git a/examples/Project.toml b/examples/Project.toml index 6864eaa..4ae9126 100644 --- a/examples/Project.toml +++ b/examples/Project.toml @@ -2,3 +2,8 @@ DataGraphs = "b5a273c3-7e6c-41f6-98bd-8d7f1525a36a" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19" + +[compat] +DataGraphs = "0.2.6" +Graphs = "1.12.0" +NamedGraphs = "0.6.5" diff --git a/examples/README.jl b/examples/README.jl new file mode 100644 index 0000000..5ce4674 --- /dev/null +++ b/examples/README.jl @@ -0,0 +1,31 @@ +# # DataGraphs.jl +# +# [![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://itensor.github.io/DataGraphs.jl/stable/) +# [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://itensor.github.io/DataGraphs.jl/dev/) +# [![Build Status](https://github.com/ITensor/DataGraphs.jl/actions/workflows/Tests.yml/badge.svg?branch=main)](https://github.com/ITensor/DataGraphs.jl/actions/workflows/Tests.yml?query=branch%3Amain) +# [![Coverage](https://codecov.io/gh/ITensor/DataGraphs.jl/branch/main/graph/badge.svg)](https://codecov.io/gh/ITensor/DataGraphs.jl) +# [![Code Style: Blue](https://img.shields.io/badge/code%20style-blue-4495d1.svg)](https://github.com/invenia/BlueStyle) +# [![Aqua](https://raw.githubusercontent.com/JuliaTesting/Aqua.jl/master/badge.svg)](https://github.com/JuliaTesting/Aqua.jl) + +# ## Support +# +# {CCQ_LOGO} +# +# DataGraphs.jl is supported by the Flatiron Institute, a division of the Simons Foundation. + +# ## Installation instructions + +# This package can be added as usual through the package manager: + +#= +```julia +julia> using Pkg: Pkg + +julia> Pkg.add("DataGraphs") +``` +=# + +# ## Examples + +using DataGraphs: DataGraphs +# Examples go here. diff --git a/src/DataGraphs.jl b/src/DataGraphs.jl index c603b91..d15f1bf 100644 --- a/src/DataGraphs.jl +++ b/src/DataGraphs.jl @@ -1,4 +1,5 @@ module DataGraphs + include("utils.jl") include("traits/isunderlyinggraph.jl") include("abstractdatagraph.jl") @@ -10,8 +11,4 @@ include("../ext/DataGraphsNamedGraphsExt/DataGraphsNamedGraphsExt.jl") export AbstractDataGraph, DataGraph -using PackageExtensionCompat: @require_extensions -function __init__() - @require_extensions -end end diff --git a/test/Project.toml b/test/Project.toml index 7bbf4af..515540f 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,8 +1,21 @@ [deps] +Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" DataGraphs = "b5a273c3-7e6c-41f6-98bd-8d7f1525a36a" Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6" GraphsFlows = "06909019-6f44-4949-96fc-b9d9aaa02889" NamedGraphs = "678767b0-92e7-4007-89e4-4527a8725b19" +SafeTestsets = "1bc83da4-3b8d-516f-aca4-4fe02f6d838f" Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[compat] +Aqua = "0.8.11" +DataGraphs = "0.2.6" +Dictionaries = "0.4.4" +Graphs = "1.12.0" +GraphsFlows = "0.1.1" +NamedGraphs = "0.6.6" +SafeTestsets = "0.1.0" +Suppressor = "0.2.8" +Test = "1.10.0" diff --git a/test/runtests.jl b/test/runtests.jl index d37d69c..1c52c3e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,466 +1,63 @@ -@eval module $(gensym()) -using DataGraphs: - DataGraphs, - DataGraph, - edge_data, - edge_data_eltype, - is_arranged, - vertex_data, - vertex_data_eltype -using Dictionaries: AbstractIndices, Dictionary, Indices, dictionary -using Graphs: - add_edge!, - a_star, - bfs_tree, - connected_components, - degree, - dfs_tree, - dijkstra_shortest_paths, - dst, - edges, - edgetype, - grid, - has_edge, - has_vertex, - indegree, - ne, - nv, - outdegree, - path_graph, - src, - steiner_tree, - vertices -using Graphs.SimpleGraphs: SimpleDiGraph, SimpleEdge, SimpleGraph -using GraphsFlows: GraphsFlows -using NamedGraphs: NamedDiGraph, NamedEdge, NamedGraph -using NamedGraphs.GraphsExtensions: ⊔, rename_vertices, vertextype -using NamedGraphs.NamedGraphGenerators: named_grid, named_path_graph -using NamedGraphs.OrdinalIndexing: nd, st, rd, th -using Test: @test, @test_broken, @testset - -using DataGraphs: is_arranged - -@testset "DataGraphs.jl" begin - @eval module $(gensym()) - using DataGraphs: DataGraphs - using Suppressor: @suppress - using Test: @testset - @testset "Examples" begin - examples_path = joinpath(pkgdir(DataGraphs), "examples") - @testset "Run examples: $filename" for filename in readdir(examples_path) - if endswith(filename, ".jl") - @suppress include(joinpath(examples_path, filename)) +using SafeTestsets: @safetestset +using Suppressor: Suppressor + +# check for filtered groups +# either via `--group=ALL` or through ENV["GROUP"] +const pat = r"(?:--group=)(\w+)" +arg_id = findfirst(contains(pat), ARGS) +const GROUP = uppercase( + if isnothing(arg_id) + get(ENV, "GROUP", "ALL") + else + only(match(pat, ARGS[arg_id]).captures) + end, +) + +"match files of the form `test_*.jl`, but exclude `*setup*.jl`" +istestfile(fn) = + endswith(fn, ".jl") && startswith(basename(fn), "test_") && !contains(fn, "setup") +"match files of the form `*.jl`, but exclude `*_notest.jl` and `*setup*.jl`" +isexamplefile(fn) = + endswith(fn, ".jl") && !endswith(fn, "_notest.jl") && !contains(fn, "setup") + +@time begin + # tests in groups based on folder structure + for testgroup in filter(isdir, readdir(@__DIR__)) + if GROUP == "ALL" || GROUP == uppercase(testgroup) + groupdir = joinpath(@__DIR__, testgroup) + for file in filter(istestfile, readdir(groupdir)) + filename = joinpath(groupdir, file) + @eval @safetestset $file begin + include($filename) end end end end - @testset "is_arranged" begin - for (a, b) in [ - (1, 2), - ([1], [2]), - ([1, 2], [2, 1]), - ([1, 2], [2]), - ([2], [2, 1]), - ((1,), (2,)), - ((1, 2), (2, 1)), - ((1, 2), (2,)), - ((2,), (2, 1)), - ("X", 1), - (("X",), (1, 2)), - ] - @test is_arranged(a, b) - @test !is_arranged(b, a) - end - end - - @testset "Basics" begin - g = grid((4,)) - dg = DataGraph(g; vertex_data_eltype=String, edge_data_eltype=Symbol) - @test !isassigned(dg, SimpleEdge(1, 2)) - @test !isassigned(dg, 1 => 2) - @test !isassigned(dg, SimpleEdge(1 => 2)) - @test !isassigned(dg, 1 => 3) - @test !isassigned(dg, 1) - @test !isassigned(dg, 2) - @test !isassigned(dg, 3) - @test !isassigned(dg, 4) - - @test degree(g, 1) == 1 - @test indegree(g, 1) == 1 - @test outdegree(g, 1) == 1 - @test degree(g, 2) == 2 - @test indegree(g, 2) == 2 - @test outdegree(g, 2) == 2 - - @test has_edge(dg, 1, 2) - @test has_edge(dg, 1 => 2) - @test !has_edge(dg, 1, 3) - @test !has_edge(dg, 1 => 3) - @test has_vertex(dg, 1) - @test has_vertex(dg, 4) - @test !has_vertex(dg, 0) - @test !has_vertex(dg, 5) - - dg[1] = "V1" - dg[2] = "V2" - dg[3] = "V3" - dg[4] = "V4" - @test isassigned(dg, 1) - @test dg[1] == "V1" - @test dg[2] == "V2" - @test dg[3] == "V3" - @test dg[4] == "V4" - - dg[1 => 2] = :E12 - dg[2 => 3] = :E23 - dg[SimpleEdge(3, 4)] = :E34 - #@test isassigned(dg, (1, 2)) - @test isassigned(dg, SimpleEdge(2, 3)) - @test isassigned(dg, 3 => 4) - @test dg[SimpleEdge(1, 2)] == :E12 - @test dg[2 => 3] == :E23 - @test dg[3 => 4] == :E34 - - # Regression test - # g = NamedGraph([(1, 1), (1, (1, 1))]) - dg = DataGraph(NamedGraph([(1, 1), (1, (1, 1))])) - dg[(1, 1) => (1, (1, 1))] = "X" - @test dg[(1, 1) => (1, (1, 1))] == "X" - - vdata = map(v -> "V$v", Indices(1:4)) - edata = map(e -> "E$(src(e))$(dst(e))", Indices(SimpleEdge.([1 => 2, 2 => 3, 3 => 4]))) - # TODO: Make a more compact constructor that directly accepts - # vertex and edge data? Maybe `DataGraph(g; vertex_data=vdata, edge_data=edata)` - # or `DataGraph(g; vertex_data=v -> "V$v", edge_data=e -> "E$(src(e))$(dst(e))")`. - dg = DataGraph(g; vertex_data_eltype=eltype(vdata), edge_data_eltype=eltype(edata)) - for v in vertices(dg) - dg[v] = vdata[v] + # single files in top folder + for file in filter(istestfile, readdir(@__DIR__)) + (file == basename(@__FILE__)) && continue # exclude this file to avoid infinite recursion + @eval @safetestset $file begin + include($file) end - for e in edges(dg) - dg[e] = edata[e] - end - - @test dg[1] == "V1" - @test dg[2] == "V2" - @test dg[3] == "V3" - @test dg[4] == "V4" - @test dg[1 => 2] == "E12" - @test dg[2 => 3] == "E23" - @test dg[3 => 4] == "E34" - - @test DataGraph(g) isa DataGraph{Int,Any,Any,SimpleGraph{Int},SimpleEdge{Int}} - - dg_uint16 = DataGraph{UInt16}(dg) - @test dg_uint16 isa - DataGraph{UInt16,String,String,SimpleGraph{UInt16},SimpleEdge{UInt16}} - @test vertextype(dg_uint16) === UInt16 - @test edgetype(dg_uint16) === SimpleEdge{UInt16} - @test vertex_data_eltype(dg_uint16) === String - @test edge_data_eltype(dg_uint16) === String - @test dg_uint16[1] == "V1" - @test dg_uint16[2] == "V2" - @test dg_uint16[3] == "V3" - @test dg_uint16[4] == "V4" - @test dg_uint16[1 => 2] == "E12" - @test dg_uint16[2 => 3] == "E23" - @test dg_uint16[3 => 4] == "E34" - - # Vertices with mixed types - dg = DataGraph(NamedGraph(grid((4,)), [1, "X", 2, "Y"])) - @test nv(dg) == 4 - @test ne(dg) == 3 - dg[1] = "vertex_1" - dg["X"] = "vertex_X" - dg[2] = "vertex_2" - dg["Y"] = "vertex_Y" - @test dg[1] == "vertex_1" - @test dg["X"] == "vertex_X" - @test dg[2] == "vertex_2" - @test dg["Y"] == "vertex_Y" - - dg[1 => "X"] = "edge_1X" - dg["X" => 2] = "edge_X2" - dg[2 => "Y"] = "edge_2Y" - @test dg[1 => "X"] == "edge_1X" - @test dg["X" => 1] == "edge_1X" - @test dg["X" => 2] == "edge_X2" - @test dg[2 => "X"] == "edge_X2" - @test dg[2 => "Y"] == "edge_2Y" - @test dg["Y" => 2] == "edge_2Y" - - dg["X" => 1] = "edge_X1" - dg[2 => "X"] = "edge_2X" - dg["Y" => 2] = "edge_Y2" - @test dg[1 => "X"] == "edge_X1" - @test dg["X" => 1] == "edge_X1" - @test dg["X" => 2] == "edge_2X" - @test dg[2 => "X"] == "edge_2X" - @test dg[2 => "Y"] == "edge_Y2" - @test dg["Y" => 2] == "edge_Y2" - end - - @testset "get and get! functions" begin - g = grid((4,)) - dg = DataGraph(g; vertex_data_eltype=String, edge_data_eltype=Symbol) - - # Test for vertices - @test get(dg, 1, "default") == "default" - @test !isassigned(dg, 1) - - @test get!(dg, 2, "default") == "default" - @test isassigned(dg, 2) - @test dg[2] == "default" - - # Test for edges - @test get(dg, 1 => 2, :default) == :default - @test !isassigned(dg, 1 => 2) - - @test get!(dg, 1 => 2, :default) == :default - @test isassigned(dg, 1 => 2) - @test dg[1 => 2] == :default end - @testset "Constructors specifying vertex type" begin - dg = DataGraph{Float64}( - named_path_graph(4); vertex_data_eltype=String, edge_data_eltype=Symbol - ) - @test nv(dg) == 4 - @test ne(dg) == 3 - @test edgetype(dg) === NamedEdge{Float64} - @test vertextype(dg) === Float64 - @test vertex_data_eltype(dg) === String - @test edge_data_eltype(dg) === Symbol - @test issetequal(vertices(dg), Float64.(1:4)) - @test vertices(dg) isa AbstractIndices{Float64} - @test eltype(vertices(dg)) === Float64 - @test has_edge(dg, 1.0 => 2.0) - @test has_edge(dg, 2.0 => 3.0) - @test has_edge(dg, 3.0 => 4.0) - @test vertex_data(dg) == Dictionary{Float64,String}() - @test vertex_data(dg) isa Dictionary{Float64,String} - @test keytype(vertex_data(dg)) === Float64 - @test eltype(vertex_data(dg)) === String - @test edge_data(dg) == Dictionary{NamedEdge{Float64},Symbol}() - @test edge_data(dg) isa Dictionary{NamedEdge{Float64},Symbol} - @test keytype(edge_data(dg)) === NamedEdge{Float64} - @test eltype(edge_data(dg)) === Symbol - end - - @testset "Disjoint unions" begin - g = DataGraph(named_grid((2, 2)); vertex_data_eltype=String, edge_data_eltype=String) - - for v in vertices(g) - g[v] = "V$v" - end - for e in edges(g) - g[e] = "E$e" + # test examples + examplepath = joinpath(@__DIR__, "..", "examples") + for (root, _, files) in walkdir(examplepath) + contains(chopprefix(root, @__DIR__), "setup") && continue + for file in filter(isexamplefile, files) + filename = joinpath(root, file) + @eval begin + @safetestset $file begin + $(Expr( + :macrocall, + GlobalRef(Suppressor, Symbol("@suppress")), + LineNumberNode(@__LINE__, @__FILE__), + :(include($filename)), + )) + end + end end - - gg = g ⊔ g - - @test has_vertex(gg, ((1, 1), 1)) - @test has_vertex(gg, ((1, 1), 2)) - @test has_edge(gg, ((1, 1), 1) => ((1, 2), 1)) - @test has_edge(gg, ((1, 1), 2) => ((1, 2), 2)) - @test nv(gg) == 2nv(g) - @test ne(gg) == 2ne(g) - - # TODO: Define `vcat`, `hcat`, `hvncat`? - gg = [g; g] - - @test_broken has_vertex(gg, (1, 1)) - @test_broken has_vertex(gg, (2, 1)) - @test_broken has_vertex(gg, (3, 1)) - @test_broken has_vertex(gg, (4, 1)) - @test_broken has_edge(gg, (1, 1) => (1, 2)) - @test_broken has_edge(gg, (3, 1) => (3, 2)) - @test_broken nv(gg) == 2nv(g) - @test_broken ne(gg) == 2ne(g) - - gg = [g;; g] - - @test_broken has_vertex(gg, (1, 1)) - @test_broken has_vertex(gg, (1, 2)) - @test_broken has_vertex(gg, (1, 3)) - @test_broken has_vertex(gg, (1, 4)) - @test_broken has_edge(gg, (1, 1) => (1, 2)) - @test_broken has_edge(gg, (1, 3) => (1, 4)) - @test_broken nv(gg) == 2nv(g) - @test_broken ne(gg) == 2ne(g) - end - - @testset "union" begin - g1 = DataGraph(grid((4,))) - g1[1] = ["A", "B", "C"] - g1[1 => 2] = ["E", "F"] - - g2 = DataGraph(SimpleGraph(5)) - add_edge!(g2, 1 => 5) - g2[1] = ["C", "D", "E"] - - # Same as: - # union(g1, g2; merge_data=(x, y) -> y) - g = union(g1, g2) - @test nv(g) == 5 - @test ne(g) == 4 - @test has_edge(g, 1 => 2) - @test has_edge(g, 2 => 3) - @test has_edge(g, 3 => 4) - @test has_edge(g, 1 => 5) - @test g[1] == ["C", "D", "E"] - @test g[1 => 2] == ["E", "F"] - - g = union(g1, g2; merge_data=union) - @test nv(g) == 5 - @test ne(g) == 4 - @test has_edge(g, 1 => 2) - @test has_edge(g, 2 => 3) - @test has_edge(g, 3 => 4) - @test has_edge(g, 1 => 5) - @test g[1] == ["A", "B", "C", "D", "E"] - @test g[1 => 2] == ["E", "F"] - end - @testset "connected_components" begin - g1 = named_path_graph(3) - g2 = rename_vertices(v -> nv(g1) + v, g1) - dg1 = DataGraph(g1) - dg1[1] = "A1" - dg1[2] = "B1" - dg1[3] = "C1" - dg1[1 => 2] = "A1" => "B1" - dg1[2 => 3] = "B1" => "C1" - dg2 = DataGraph(g2) - dg2[1] = "A2" - dg2[2] = "B2" - dg2[3] = "C2" - dg2[1 => 2] = "A2" => "B2" - dg2[2 => 3] = "B2" => "C2" - dg = union(dg1, dg2) - comps = connected_components(dg) - @test length(comps) == 2 - @test issetequal(comps[1], [1, 2, 3]) - @test issetequal(comps[2], [4, 5, 6]) end - @testset "reverse" begin - g = DataGraph(SimpleDiGraph(4)) - add_edge!(g, 1 => 2) - add_edge!(g, 3 => 4) - g[1 => 2] = :A - g[3 => 4] = "X" - rg = reverse(g) - @test has_edge(rg, 2 => 1) - @test has_edge(rg, 4 => 3) - @test rg[2 => 1] == :A - @test isassigned(rg, 2 => 1) - @test !isassigned(rg, 1 => 2) - @test rg[4 => 3] == "X" - @test !isassigned(rg, 3 => 4) - @test isassigned(rg, 4 => 3) - end - @testset "Tree traversal" begin - g = DataGraph(named_grid(4)) - - t = bfs_tree(g, 2) - es = [2 => 1, 2 => 3, 3 => 4] - @test t isa NamedDiGraph{Int} - @test nv(t) == nv(g) - @test ne(t) == nv(g) - 1 - @test all(e -> has_edge(t, e), es) - - t = dfs_tree(g, 2) - @test t isa NamedDiGraph{Int} - @test nv(t) == nv(g) - @test ne(t) == nv(g) - 1 - @test all(e -> has_edge(t, e), es) - - g = DataGraph(named_grid((4, 2))) - - t = bfs_tree(g, (1, 1)) - es = [ - (1, 1) => (1, 2), - (1, 1) => (2, 1), - (2, 1) => (2, 2), - (2, 1) => (3, 1), - (3, 1) => (3, 2), - (3, 1) => (4, 1), - (4, 1) => (4, 2), - ] - @test t isa NamedDiGraph{Tuple{Int,Int}} - @test nv(t) == nv(g) - @test ne(t) == nv(g) - 1 - @test all(e -> has_edge(t, e), es) - - t = dfs_tree(g, (1, 1)) - es = [ - (1, 1) => (2, 1), - (2, 1) => (3, 1), - (3, 1) => (4, 1), - (4, 1) => (4, 2), - (4, 2) => (3, 2), - (3, 2) => (2, 2), - (2, 2) => (1, 2), - ] - @test t isa NamedDiGraph{Tuple{Int,Int}} - @test nv(t) == nv(g) - @test ne(t) == nv(g) - 1 - @test all(e -> has_edge(t, e), es) - end - @testset "dijkstra_shortest_paths" begin - g = DataGraph(named_grid(4)) - ps = dijkstra_shortest_paths(g, [1]) - @test ps.dists == dictionary([1 => 0, 2 => 1, 3 => 2, 4 => 3]) - @test ps.parents == dictionary([1 => 1, 2 => 1, 3 => 2, 4 => 3]) - @test ps.pathcounts == dictionary([1 => 1.0, 2 => 1.0, 3 => 1.0, 4 => 1.0]) - end - @testset "a_star" begin - g = DataGraph(named_grid(4)) - path = a_star(g, 1, 3) - @test path == NamedEdge.([1 => 2, 2 => 3]) - end - @testset "steiner_tree" begin - g = DataGraph(named_grid(5)) - t = steiner_tree(g, [2, 4]) - @test nv(t) == 3 - @test ne(t) == 2 - end - @testset "GraphsFlows.mincut (vertextype=$(eltype(verts))" for verts in ( - [1, 2, 3, 4], ["A", "B", "C", "D"] - ) - g = DataGraph(NamedGraph(path_graph(4), verts)) - part1, part2, flow = GraphsFlows.mincut(g, verts[1], verts[4]) - @test verts[1] ∈ part1 - @test verts[4] ∈ part2 - @test flow == 1 - end - @testset "OrdinalIndexing" begin - g = DataGraph( - NamedGraph(path_graph(3), ["a", "b", "c"]); - vertex_data_eltype=String, - edge_data_eltype=Symbol, - ) - g[1st] = "v_a" - g[2nd] = "v_b" - g[3rd] = "v_c" - g[1st => 2nd] = :e_ab - g[2nd => 3rd] = :e_bc - @test g["a"] == "v_a" - @test g["b"] == "v_b" - @test g["c"] == "v_c" - @test g["a" => "b"] === :e_ab - @test g["b" => "a"] === :e_ab - @test g["b" => "c"] === :e_bc - @test g["c" => "b"] === :e_bc - @test g[1st] == "v_a" - @test g[1th] == "v_a" - @test g[2nd] == "v_b" - @test g[2th] == "v_b" - @test g[3rd] == "v_c" - @test g[3th] == "v_c" - @test g[1st => 2nd] === :e_ab - @test g[2nd => 1st] === :e_ab - @test g[2nd => 3rd] === :e_bc - @test g[3rd => 2nd] === :e_bc - end -end end diff --git a/test/test_aqua.jl b/test/test_aqua.jl new file mode 100644 index 0000000..8dad0e7 --- /dev/null +++ b/test/test_aqua.jl @@ -0,0 +1,7 @@ +using Aqua: Aqua +using DataGraphs: DataGraphs +using Test: @testset + +@testset "Code quality (Aqua.jl)" begin + # Aqua.test_all(DataGraphs) +end diff --git a/test/test_basics.jl b/test/test_basics.jl new file mode 100644 index 0000000..1a5eefd --- /dev/null +++ b/test/test_basics.jl @@ -0,0 +1,464 @@ +using DataGraphs: + DataGraphs, + DataGraph, + edge_data, + edge_data_eltype, + is_arranged, + vertex_data, + vertex_data_eltype +using Dictionaries: AbstractIndices, Dictionary, Indices, dictionary +using Graphs: + add_edge!, + a_star, + bfs_tree, + connected_components, + degree, + dfs_tree, + dijkstra_shortest_paths, + dst, + edges, + edgetype, + grid, + has_edge, + has_vertex, + indegree, + ne, + nv, + outdegree, + path_graph, + src, + steiner_tree, + vertices +using Graphs.SimpleGraphs: SimpleDiGraph, SimpleEdge, SimpleGraph +using GraphsFlows: GraphsFlows +using NamedGraphs: NamedDiGraph, NamedEdge, NamedGraph +using NamedGraphs.GraphsExtensions: ⊔, rename_vertices, vertextype +using NamedGraphs.NamedGraphGenerators: named_grid, named_path_graph +using NamedGraphs.OrdinalIndexing: nd, st, rd, th +using Test: @test, @test_broken, @testset + +using DataGraphs: is_arranged + +@testset "DataGraphs.jl" begin + @eval module $(gensym()) + using DataGraphs: DataGraphs + using Suppressor: @suppress + using Test: @testset + @testset "Examples" begin + examples_path = joinpath(pkgdir(DataGraphs), "examples") + @testset "Run examples: $filename" for filename in readdir(examples_path) + if endswith(filename, ".jl") + @suppress include(joinpath(examples_path, filename)) + end + end + end + end + + @testset "is_arranged" begin + for (a, b) in [ + (1, 2), + ([1], [2]), + ([1, 2], [2, 1]), + ([1, 2], [2]), + ([2], [2, 1]), + ((1,), (2,)), + ((1, 2), (2, 1)), + ((1, 2), (2,)), + ((2,), (2, 1)), + ("X", 1), + (("X",), (1, 2)), + ] + @test is_arranged(a, b) + @test !is_arranged(b, a) + end + end + + @testset "Basics" begin + g = grid((4,)) + dg = DataGraph(g; vertex_data_eltype=String, edge_data_eltype=Symbol) + @test !isassigned(dg, SimpleEdge(1, 2)) + @test !isassigned(dg, 1 => 2) + @test !isassigned(dg, SimpleEdge(1 => 2)) + @test !isassigned(dg, 1 => 3) + @test !isassigned(dg, 1) + @test !isassigned(dg, 2) + @test !isassigned(dg, 3) + @test !isassigned(dg, 4) + + @test degree(g, 1) == 1 + @test indegree(g, 1) == 1 + @test outdegree(g, 1) == 1 + @test degree(g, 2) == 2 + @test indegree(g, 2) == 2 + @test outdegree(g, 2) == 2 + + @test has_edge(dg, 1, 2) + @test has_edge(dg, 1 => 2) + @test !has_edge(dg, 1, 3) + @test !has_edge(dg, 1 => 3) + @test has_vertex(dg, 1) + @test has_vertex(dg, 4) + @test !has_vertex(dg, 0) + @test !has_vertex(dg, 5) + + dg[1] = "V1" + dg[2] = "V2" + dg[3] = "V3" + dg[4] = "V4" + @test isassigned(dg, 1) + @test dg[1] == "V1" + @test dg[2] == "V2" + @test dg[3] == "V3" + @test dg[4] == "V4" + + dg[1 => 2] = :E12 + dg[2 => 3] = :E23 + dg[SimpleEdge(3, 4)] = :E34 + #@test isassigned(dg, (1, 2)) + @test isassigned(dg, SimpleEdge(2, 3)) + @test isassigned(dg, 3 => 4) + @test dg[SimpleEdge(1, 2)] == :E12 + @test dg[2 => 3] == :E23 + @test dg[3 => 4] == :E34 + + # Regression test + # g = NamedGraph([(1, 1), (1, (1, 1))]) + dg = DataGraph(NamedGraph([(1, 1), (1, (1, 1))])) + dg[(1, 1) => (1, (1, 1))] = "X" + @test dg[(1, 1) => (1, (1, 1))] == "X" + + vdata = map(v -> "V$v", Indices(1:4)) + edata = map(e -> "E$(src(e))$(dst(e))", Indices(SimpleEdge.([1 => 2, 2 => 3, 3 => 4]))) + # TODO: Make a more compact constructor that directly accepts + # vertex and edge data? Maybe `DataGraph(g; vertex_data=vdata, edge_data=edata)` + # or `DataGraph(g; vertex_data=v -> "V$v", edge_data=e -> "E$(src(e))$(dst(e))")`. + dg = DataGraph(g; vertex_data_eltype=eltype(vdata), edge_data_eltype=eltype(edata)) + for v in vertices(dg) + dg[v] = vdata[v] + end + for e in edges(dg) + dg[e] = edata[e] + end + + @test dg[1] == "V1" + @test dg[2] == "V2" + @test dg[3] == "V3" + @test dg[4] == "V4" + @test dg[1 => 2] == "E12" + @test dg[2 => 3] == "E23" + @test dg[3 => 4] == "E34" + + @test DataGraph(g) isa DataGraph{Int,Any,Any,SimpleGraph{Int},SimpleEdge{Int}} + + dg_uint16 = DataGraph{UInt16}(dg) + @test dg_uint16 isa + DataGraph{UInt16,String,String,SimpleGraph{UInt16},SimpleEdge{UInt16}} + @test vertextype(dg_uint16) === UInt16 + @test edgetype(dg_uint16) === SimpleEdge{UInt16} + @test vertex_data_eltype(dg_uint16) === String + @test edge_data_eltype(dg_uint16) === String + @test dg_uint16[1] == "V1" + @test dg_uint16[2] == "V2" + @test dg_uint16[3] == "V3" + @test dg_uint16[4] == "V4" + @test dg_uint16[1 => 2] == "E12" + @test dg_uint16[2 => 3] == "E23" + @test dg_uint16[3 => 4] == "E34" + + # Vertices with mixed types + dg = DataGraph(NamedGraph(grid((4,)), [1, "X", 2, "Y"])) + @test nv(dg) == 4 + @test ne(dg) == 3 + dg[1] = "vertex_1" + dg["X"] = "vertex_X" + dg[2] = "vertex_2" + dg["Y"] = "vertex_Y" + @test dg[1] == "vertex_1" + @test dg["X"] == "vertex_X" + @test dg[2] == "vertex_2" + @test dg["Y"] == "vertex_Y" + + dg[1 => "X"] = "edge_1X" + dg["X" => 2] = "edge_X2" + dg[2 => "Y"] = "edge_2Y" + @test dg[1 => "X"] == "edge_1X" + @test dg["X" => 1] == "edge_1X" + @test dg["X" => 2] == "edge_X2" + @test dg[2 => "X"] == "edge_X2" + @test dg[2 => "Y"] == "edge_2Y" + @test dg["Y" => 2] == "edge_2Y" + + dg["X" => 1] = "edge_X1" + dg[2 => "X"] = "edge_2X" + dg["Y" => 2] = "edge_Y2" + @test dg[1 => "X"] == "edge_X1" + @test dg["X" => 1] == "edge_X1" + @test dg["X" => 2] == "edge_2X" + @test dg[2 => "X"] == "edge_2X" + @test dg[2 => "Y"] == "edge_Y2" + @test dg["Y" => 2] == "edge_Y2" + end + + @testset "get and get! functions" begin + g = grid((4,)) + dg = DataGraph(g; vertex_data_eltype=String, edge_data_eltype=Symbol) + + # Test for vertices + @test get(dg, 1, "default") == "default" + @test !isassigned(dg, 1) + + @test get!(dg, 2, "default") == "default" + @test isassigned(dg, 2) + @test dg[2] == "default" + + # Test for edges + @test get(dg, 1 => 2, :default) == :default + @test !isassigned(dg, 1 => 2) + + @test get!(dg, 1 => 2, :default) == :default + @test isassigned(dg, 1 => 2) + @test dg[1 => 2] == :default + end + + @testset "Constructors specifying vertex type" begin + dg = DataGraph{Float64}( + named_path_graph(4); vertex_data_eltype=String, edge_data_eltype=Symbol + ) + @test nv(dg) == 4 + @test ne(dg) == 3 + @test edgetype(dg) === NamedEdge{Float64} + @test vertextype(dg) === Float64 + @test vertex_data_eltype(dg) === String + @test edge_data_eltype(dg) === Symbol + @test issetequal(vertices(dg), Float64.(1:4)) + @test vertices(dg) isa AbstractIndices{Float64} + @test eltype(vertices(dg)) === Float64 + @test has_edge(dg, 1.0 => 2.0) + @test has_edge(dg, 2.0 => 3.0) + @test has_edge(dg, 3.0 => 4.0) + @test vertex_data(dg) == Dictionary{Float64,String}() + @test vertex_data(dg) isa Dictionary{Float64,String} + @test keytype(vertex_data(dg)) === Float64 + @test eltype(vertex_data(dg)) === String + @test edge_data(dg) == Dictionary{NamedEdge{Float64},Symbol}() + @test edge_data(dg) isa Dictionary{NamedEdge{Float64},Symbol} + @test keytype(edge_data(dg)) === NamedEdge{Float64} + @test eltype(edge_data(dg)) === Symbol + end + + @testset "Disjoint unions" begin + g = DataGraph(named_grid((2, 2)); vertex_data_eltype=String, edge_data_eltype=String) + + for v in vertices(g) + g[v] = "V$v" + end + for e in edges(g) + g[e] = "E$e" + end + + gg = g ⊔ g + + @test has_vertex(gg, ((1, 1), 1)) + @test has_vertex(gg, ((1, 1), 2)) + @test has_edge(gg, ((1, 1), 1) => ((1, 2), 1)) + @test has_edge(gg, ((1, 1), 2) => ((1, 2), 2)) + @test nv(gg) == 2nv(g) + @test ne(gg) == 2ne(g) + + # TODO: Define `vcat`, `hcat`, `hvncat`? + gg = [g; g] + + @test_broken has_vertex(gg, (1, 1)) + @test_broken has_vertex(gg, (2, 1)) + @test_broken has_vertex(gg, (3, 1)) + @test_broken has_vertex(gg, (4, 1)) + @test_broken has_edge(gg, (1, 1) => (1, 2)) + @test_broken has_edge(gg, (3, 1) => (3, 2)) + @test_broken nv(gg) == 2nv(g) + @test_broken ne(gg) == 2ne(g) + + gg = [g;; g] + + @test_broken has_vertex(gg, (1, 1)) + @test_broken has_vertex(gg, (1, 2)) + @test_broken has_vertex(gg, (1, 3)) + @test_broken has_vertex(gg, (1, 4)) + @test_broken has_edge(gg, (1, 1) => (1, 2)) + @test_broken has_edge(gg, (1, 3) => (1, 4)) + @test_broken nv(gg) == 2nv(g) + @test_broken ne(gg) == 2ne(g) + end + + @testset "union" begin + g1 = DataGraph(grid((4,))) + g1[1] = ["A", "B", "C"] + g1[1 => 2] = ["E", "F"] + + g2 = DataGraph(SimpleGraph(5)) + add_edge!(g2, 1 => 5) + g2[1] = ["C", "D", "E"] + + # Same as: + # union(g1, g2; merge_data=(x, y) -> y) + g = union(g1, g2) + @test nv(g) == 5 + @test ne(g) == 4 + @test has_edge(g, 1 => 2) + @test has_edge(g, 2 => 3) + @test has_edge(g, 3 => 4) + @test has_edge(g, 1 => 5) + @test g[1] == ["C", "D", "E"] + @test g[1 => 2] == ["E", "F"] + + g = union(g1, g2; merge_data=union) + @test nv(g) == 5 + @test ne(g) == 4 + @test has_edge(g, 1 => 2) + @test has_edge(g, 2 => 3) + @test has_edge(g, 3 => 4) + @test has_edge(g, 1 => 5) + @test g[1] == ["A", "B", "C", "D", "E"] + @test g[1 => 2] == ["E", "F"] + end + @testset "connected_components" begin + g1 = named_path_graph(3) + g2 = rename_vertices(v -> nv(g1) + v, g1) + dg1 = DataGraph(g1) + dg1[1] = "A1" + dg1[2] = "B1" + dg1[3] = "C1" + dg1[1 => 2] = "A1" => "B1" + dg1[2 => 3] = "B1" => "C1" + dg2 = DataGraph(g2) + dg2[1] = "A2" + dg2[2] = "B2" + dg2[3] = "C2" + dg2[1 => 2] = "A2" => "B2" + dg2[2 => 3] = "B2" => "C2" + dg = union(dg1, dg2) + comps = connected_components(dg) + @test length(comps) == 2 + @test issetequal(comps[1], [1, 2, 3]) + @test issetequal(comps[2], [4, 5, 6]) + end + @testset "reverse" begin + g = DataGraph(SimpleDiGraph(4)) + add_edge!(g, 1 => 2) + add_edge!(g, 3 => 4) + g[1 => 2] = :A + g[3 => 4] = "X" + rg = reverse(g) + @test has_edge(rg, 2 => 1) + @test has_edge(rg, 4 => 3) + @test rg[2 => 1] == :A + @test isassigned(rg, 2 => 1) + @test !isassigned(rg, 1 => 2) + @test rg[4 => 3] == "X" + @test !isassigned(rg, 3 => 4) + @test isassigned(rg, 4 => 3) + end + @testset "Tree traversal" begin + g = DataGraph(named_grid(4)) + + t = bfs_tree(g, 2) + es = [2 => 1, 2 => 3, 3 => 4] + @test t isa NamedDiGraph{Int} + @test nv(t) == nv(g) + @test ne(t) == nv(g) - 1 + @test all(e -> has_edge(t, e), es) + + t = dfs_tree(g, 2) + @test t isa NamedDiGraph{Int} + @test nv(t) == nv(g) + @test ne(t) == nv(g) - 1 + @test all(e -> has_edge(t, e), es) + + g = DataGraph(named_grid((4, 2))) + + t = bfs_tree(g, (1, 1)) + es = [ + (1, 1) => (1, 2), + (1, 1) => (2, 1), + (2, 1) => (2, 2), + (2, 1) => (3, 1), + (3, 1) => (3, 2), + (3, 1) => (4, 1), + (4, 1) => (4, 2), + ] + @test t isa NamedDiGraph{Tuple{Int,Int}} + @test nv(t) == nv(g) + @test ne(t) == nv(g) - 1 + @test all(e -> has_edge(t, e), es) + + t = dfs_tree(g, (1, 1)) + es = [ + (1, 1) => (2, 1), + (2, 1) => (3, 1), + (3, 1) => (4, 1), + (4, 1) => (4, 2), + (4, 2) => (3, 2), + (3, 2) => (2, 2), + (2, 2) => (1, 2), + ] + @test t isa NamedDiGraph{Tuple{Int,Int}} + @test nv(t) == nv(g) + @test ne(t) == nv(g) - 1 + @test all(e -> has_edge(t, e), es) + end + @testset "dijkstra_shortest_paths" begin + g = DataGraph(named_grid(4)) + ps = dijkstra_shortest_paths(g, [1]) + @test ps.dists == dictionary([1 => 0, 2 => 1, 3 => 2, 4 => 3]) + @test ps.parents == dictionary([1 => 1, 2 => 1, 3 => 2, 4 => 3]) + @test ps.pathcounts == dictionary([1 => 1.0, 2 => 1.0, 3 => 1.0, 4 => 1.0]) + end + @testset "a_star" begin + g = DataGraph(named_grid(4)) + path = a_star(g, 1, 3) + @test path == NamedEdge.([1 => 2, 2 => 3]) + end + @testset "steiner_tree" begin + g = DataGraph(named_grid(5)) + t = steiner_tree(g, [2, 4]) + @test nv(t) == 3 + @test ne(t) == 2 + end + @testset "GraphsFlows.mincut (vertextype=$(eltype(verts))" for verts in ( + [1, 2, 3, 4], ["A", "B", "C", "D"] + ) + g = DataGraph(NamedGraph(path_graph(4), verts)) + part1, part2, flow = GraphsFlows.mincut(g, verts[1], verts[4]) + @test verts[1] ∈ part1 + @test verts[4] ∈ part2 + @test flow == 1 + end + @testset "OrdinalIndexing" begin + g = DataGraph( + NamedGraph(path_graph(3), ["a", "b", "c"]); + vertex_data_eltype=String, + edge_data_eltype=Symbol, + ) + g[1st] = "v_a" + g[2nd] = "v_b" + g[3rd] = "v_c" + g[1st => 2nd] = :e_ab + g[2nd => 3rd] = :e_bc + @test g["a"] == "v_a" + @test g["b"] == "v_b" + @test g["c"] == "v_c" + @test g["a" => "b"] === :e_ab + @test g["b" => "a"] === :e_ab + @test g["b" => "c"] === :e_bc + @test g["c" => "b"] === :e_bc + @test g[1st] == "v_a" + @test g[1th] == "v_a" + @test g[2nd] == "v_b" + @test g[2th] == "v_b" + @test g[3rd] == "v_c" + @test g[3th] == "v_c" + @test g[1st => 2nd] === :e_ab + @test g[2nd => 1st] === :e_ab + @test g[2nd => 3rd] === :e_bc + @test g[3rd => 2nd] === :e_bc + end +end