├── .circleci └── config.yml ├── .devcontainer └── devcontainer.json ├── .github └── FUNDING.yml ├── .gitignore ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── LICENSE ├── README.md ├── analysis_options.yaml ├── example └── main.dart ├── lib ├── illegal_operation_exception.dart └── stack.dart ├── pubspec.lock ├── pubspec.yaml └── test └── stack_test.dart /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | orbs: 3 | codecov: codecov/codecov@1.0.2 4 | jobs: 5 | build: 6 | docker: 7 | - image: cirrusci/flutter 8 | steps: 9 | - checkout 10 | - run: pub get && flutter test --coverage 11 | - codecov/upload: 12 | file: 'coverage/lcov.info' 13 | -------------------------------------------------------------------------------- /.devcontainer/devcontainer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Flutter", 3 | "image": "cirrusci/flutter:stable", 4 | "extensions": ["dart-code.dart-code", "dart-code.flutter"] 5 | } -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [ammaratef45] 2 | patreon: ammaratef45 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://www.dartlang.org/guides/libraries/private-files 2 | 3 | # Files and directories created by pub 4 | .dart_tool/ 5 | .packages 6 | .pub/ 7 | build/ 8 | # If you're building an application, you may want to check-in your pubspec.lock 9 | pubspec.lock 10 | 11 | # Directory created by dartdoc 12 | # If you don't generate documentation locally you can remove this line. 13 | doc/api/ 14 | 15 | 16 | test/.test_coverage.dart 17 | 18 | .vscode/ 19 | 20 | coverage/ -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # 0.2.2 2 | Added `toList` 3 | 4 | # 0.2.0 5 | Added Sized constructor to control stack's size 6 | 7 | # 0.1.5 8 | Added IllegalOperationException 9 | 10 | # 0.1.4 11 | Null Safety fix 12 | 13 | # 0.1.3 14 | Renamed debugPrint to print 15 | 16 | # 0.1.2 17 | Added contains and debugPrint 18 | 19 | # 0.1.1 20 | Added size and length 21 | 22 | # 0.1.0 23 | Fixed format 24 | Improved description 25 | Added an example 26 | 27 | # 0.0.2 28 | Enhanced documentation 29 | 30 | # 0.0.1 31 | Initial version 32 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at ammar.atef45@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Ammar Atef 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # dart_stack 2 | A dart package for stack datastructure 3 | 4 | [![pub](https://img.shields.io/pub/v/stack.svg)](https://pub.dev/packages/stack) 5 | [![codecov](https://codecov.io/gh/ammaratef45/dart_stack/branch/master/graph/badge.svg)](https://codecov.io/gh/ammaratef45/dart_stack) 6 | [![CircleCI](https://circleci.com/gh/ammaratef45/dart_stack.svg?style=svg)](https://circleci.com/gh/ammaratef45/dart_stack) 7 | 8 | # Usage 9 | Add dev dependency to your `pubspec.yaml`: 10 | 11 | ```yaml 12 | dev_dependencies: 13 | stack: ^0.0.1 14 | ``` 15 | 16 | Run `pub get` to install. 17 | 18 | # How it works 19 | Import 20 | ```Dart 21 | import 'package:stack/stack.dart'; 22 | ``` 23 | Example 24 | ```Dart 25 | // Initialize 26 | Stack stack = Stack(); 27 | // Push 28 | stack.push('abc'); 29 | // See the top 30 | stack.top(); 31 | // Pop the top 32 | stack.pop(); 33 | // Check for empty 34 | stack.isEmpty; 35 | // Check for non-enpty 36 | stack.isNotEmpty; 37 | // see if the stack has a certain object 38 | stack.contains("abc"); 39 | // print the complete stack 40 | stack.print(); 41 | // get size of current stack 42 | stack.size() || var s = stack.size() 43 | ``` 44 | 45 | # Exceptions 46 | Trying to use `top()` or `pop()` on an empty stack throws `IllegalOperationException`. 47 | use `size()`, `isEmpty`, or `isNotEmpty` to check before using these operations. 48 | 49 | 50 | 51 | Feel free to file feature requests and bug reports at the 52 | [issue tracker][]. 53 | 54 | [issue tracker]: https://github.com/ammaratef45/dart_stack/issues 55 | -------------------------------------------------------------------------------- /analysis_options.yaml: -------------------------------------------------------------------------------- 1 | analyzer: 2 | strong-mode: 3 | implicit-casts: false 4 | implicit-dynamic: false 5 | errors: 6 | unused_element: error 7 | unused_import: error 8 | unused_local_variable: error 9 | dead_code: error 10 | 11 | linter: 12 | rules: 13 | # Error Rules 14 | - avoid_relative_lib_imports 15 | - avoid_types_as_parameter_names 16 | - literal_only_boolean_expressions 17 | - no_adjacent_strings_in_list 18 | - valid_regexps -------------------------------------------------------------------------------- /example/main.dart: -------------------------------------------------------------------------------- 1 | import 'package:stack/stack.dart'; 2 | 3 | void main() { 4 | Stack stack = Stack(); 5 | stack.push('abc'); 6 | stack.push('edf'); 7 | print(stack.pop()); // prints 'edf' 8 | print(stack.top()); // prints 'abc' 9 | print(stack.size()); // prints 1 10 | print(stack.length); // prints 1 11 | print(stack.contains('abc')); // prints true 12 | print(stack.contains('edf')); // prints false 13 | for (var i = 0; i < 10; i++) { 14 | stack.push(i.toString()); 15 | } 16 | stack.print(); // prints the whole stack 17 | stack.clear(); 18 | print(stack.isEmpty); // prints true 19 | } 20 | -------------------------------------------------------------------------------- /lib/illegal_operation_exception.dart: -------------------------------------------------------------------------------- 1 | class IllegalOperationException implements Exception { 2 | final String cause; 3 | 4 | IllegalOperationException(this.cause); 5 | 6 | String errMsg() => cause; 7 | } 8 | -------------------------------------------------------------------------------- /lib/stack.dart: -------------------------------------------------------------------------------- 1 | import 'dart:collection'; 2 | import 'dart:core' as core; 3 | import 'dart:core'; 4 | 5 | import 'package:stack/illegal_operation_exception.dart'; 6 | 7 | class Stack { 8 | final ListQueue _list = ListQueue(); 9 | 10 | final int noLimit = -1; 11 | 12 | /// the maximum number of entries allowed on the stack. -1 = no limit. 13 | int _sizeMax = 0; 14 | 15 | /// Default constructor sets the maximum stack size to 'no limit.' 16 | Stack() { 17 | _sizeMax = noLimit; 18 | } 19 | 20 | /// Returns a list of T elements contained in the Stack 21 | List toList() => _list.toList(); 22 | 23 | /// Constructor in which you can specify maximum number of entries. 24 | /// This maximum is a limit that is enforced as entries are pushed on to the stack 25 | /// to prevent stack growth beyond a maximum size. There is no pre-allocation of 26 | /// slots for entries at any time in this library. 27 | Stack.sized(int sizeMax) { 28 | if(sizeMax < 2) { 29 | throw IllegalOperationException( 30 | 'Error: stack size must be 2 entries or more ' 31 | ); 32 | } 33 | else { 34 | _sizeMax = sizeMax; 35 | } 36 | } 37 | 38 | /// check if the stack is empty. 39 | bool get isEmpty => _list.isEmpty; 40 | 41 | /// check if the stack is not empty. 42 | bool get isNotEmpty => _list.isNotEmpty; 43 | 44 | /// push element in top of the stack. 45 | void push(T e) { 46 | if(_sizeMax == noLimit || _list.length < _sizeMax) { 47 | _list.addLast(e); 48 | } 49 | else { 50 | throw IllegalOperationException( 51 | 'Error: cannot add element. Stack already at maximum size of: ${_sizeMax} elements'); 52 | } 53 | } 54 | 55 | /// get the top of the stack and delete it. 56 | T pop() { 57 | if (isEmpty) { 58 | throw IllegalOperationException( 59 | 'Can\'t use pop with empty stack\n consider ' 60 | 'checking for size or isEmpty before calling pop', 61 | ); 62 | } 63 | T res = _list.last; 64 | _list.removeLast(); 65 | return res; 66 | } 67 | 68 | /// get the top of the stack without deleting it. 69 | T top() { 70 | if (isEmpty) { 71 | throw IllegalOperationException( 72 | 'Can\'t use top with empty stack\n consider ' 73 | 'checking for size or isEmpty before calling top', 74 | ); 75 | } 76 | return _list.last; 77 | } 78 | 79 | /// get the size of the stack. 80 | int size() { 81 | return _list.length; 82 | } 83 | 84 | /// get the length of the stack. 85 | int get length => size(); 86 | 87 | /// returns true if element is found in the stack 88 | bool contains(T x) { 89 | return _list.contains(x); 90 | } 91 | 92 | /// removes all elements from the stack 93 | void clear() { 94 | while (isNotEmpty) { 95 | _list.removeLast(); 96 | } 97 | } 98 | 99 | /// print stack 100 | void print() { 101 | for (var item in List.from(_list).reversed) { 102 | core.print(item); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /pubspec.lock: -------------------------------------------------------------------------------- 1 | # Generated by pub 2 | # See https://dart.dev/tools/pub/glossary#lockfile 3 | packages: 4 | _fe_analyzer_shared: 5 | dependency: transitive 6 | description: 7 | name: _fe_analyzer_shared 8 | sha256: "21bc4be2d357e2a995bb290676a1abc440b1524dfe4e822a60bc26cf11beecf9" 9 | url: "https://pub.dev" 10 | source: hosted 11 | version: "19.0.0" 12 | analyzer: 13 | dependency: transitive 14 | description: 15 | name: analyzer 16 | sha256: "6baf4ef66a70521a94d3ccde61968969b67fa2232f989590c078f8a0789883ce" 17 | url: "https://pub.dev" 18 | source: hosted 19 | version: "1.3.0" 20 | args: 21 | dependency: transitive 22 | description: 23 | name: args 24 | sha256: "37a4264b0b7fb930e94c0c47558f3b6c4f4e9cb7e655a3ea373131d79b2dc0cc" 25 | url: "https://pub.dev" 26 | source: hosted 27 | version: "2.0.0" 28 | async: 29 | dependency: transitive 30 | description: 31 | name: async 32 | sha256: "6eda8392a48ae1de7ea438c91a4ba3e77205f043e7013102a424863aa6db368f" 33 | url: "https://pub.dev" 34 | source: hosted 35 | version: "2.5.0" 36 | boolean_selector: 37 | dependency: transitive 38 | description: 39 | name: boolean_selector 40 | sha256: "5bbf32bc9e518d41ec49718e2931cd4527292c9b0c6d2dffcf7fe6b9a8a8cf72" 41 | url: "https://pub.dev" 42 | source: hosted 43 | version: "2.1.0" 44 | charcode: 45 | dependency: transitive 46 | description: 47 | name: charcode 48 | sha256: "8e36feea6de5ea69f2199f29cf42a450a855738c498b57c0b980e2d3cca9c362" 49 | url: "https://pub.dev" 50 | source: hosted 51 | version: "1.2.0" 52 | cli_util: 53 | dependency: transitive 54 | description: 55 | name: cli_util 56 | sha256: cf1c02840bbbcf8fcd13feb5933c62d643cc58ddf4f6088707cf48d1892cbc5d 57 | url: "https://pub.dev" 58 | source: hosted 59 | version: "0.3.0" 60 | collection: 61 | dependency: transitive 62 | description: 63 | name: collection 64 | sha256: "6d4193120997ecfd09acf0e313f13dc122b119e5eca87ef57a7d065ec9183762" 65 | url: "https://pub.dev" 66 | source: hosted 67 | version: "1.15.0" 68 | convert: 69 | dependency: transitive 70 | description: 71 | name: convert 72 | sha256: df567b950053d83b4dba3e8c5799c411895d146f82b2147114b666a4fd9a80dd 73 | url: "https://pub.dev" 74 | source: hosted 75 | version: "3.0.0" 76 | coverage: 77 | dependency: transitive 78 | description: 79 | name: coverage 80 | sha256: "2890d8a09829de2cc3ead1407960549e4eb3c4e48c8fb837bfa5c68398496489" 81 | url: "https://pub.dev" 82 | source: hosted 83 | version: "1.0.2" 84 | crypto: 85 | dependency: transitive 86 | description: 87 | name: crypto 88 | sha256: "8be10341257b613566fdc9fd073c46f7c032ed329b1c732bda17aca29f2366c8" 89 | url: "https://pub.dev" 90 | source: hosted 91 | version: "3.0.0" 92 | file: 93 | dependency: transitive 94 | description: 95 | name: file 96 | sha256: "9fd2163d866769f60f4df8ac1dc59f52498d810c356fe78022e383dd3c57c0e1" 97 | url: "https://pub.dev" 98 | source: hosted 99 | version: "6.1.0" 100 | glob: 101 | dependency: transitive 102 | description: 103 | name: glob 104 | sha256: dda85ce2aefce16f7e75586acbcb1e8320bf176f69fd94082e31945d6de67f3e 105 | url: "https://pub.dev" 106 | source: hosted 107 | version: "2.0.1" 108 | http_multi_server: 109 | dependency: transitive 110 | description: 111 | name: http_multi_server 112 | sha256: ac10cae1b9a06fb638a92a72b00570bac856f524f7ee0d9a13eaed4960c7fd43 113 | url: "https://pub.dev" 114 | source: hosted 115 | version: "3.0.0" 116 | http_parser: 117 | dependency: transitive 118 | description: 119 | name: http_parser 120 | sha256: e362d639ba3bc07d5a71faebb98cde68c05bfbcfbbb444b60b6f60bb67719185 121 | url: "https://pub.dev" 122 | source: hosted 123 | version: "4.0.0" 124 | io: 125 | dependency: transitive 126 | description: 127 | name: io 128 | sha256: "15a5436d2a02dc60e6dc2fb5d7dfaac08b7b137cff3d4bf3158d38ecab656b69" 129 | url: "https://pub.dev" 130 | source: hosted 131 | version: "1.0.0" 132 | js: 133 | dependency: transitive 134 | description: 135 | name: js 136 | sha256: d9bdfd70d828eeb352390f81b18d6a354ef2044aa28ef25682079797fa7cd174 137 | url: "https://pub.dev" 138 | source: hosted 139 | version: "0.6.3" 140 | logging: 141 | dependency: transitive 142 | description: 143 | name: logging 144 | sha256: "0520a4826042a8a5d09ddd4755623a50d37ee536d79a70452aff8c8ad7bb6c27" 145 | url: "https://pub.dev" 146 | source: hosted 147 | version: "1.0.1" 148 | matcher: 149 | dependency: transitive 150 | description: 151 | name: matcher 152 | sha256: "38c7be344ac5057e10161a5ecb00c9d9d67ed2f150001278601dd27d9fe64206" 153 | url: "https://pub.dev" 154 | source: hosted 155 | version: "0.12.10" 156 | meta: 157 | dependency: transitive 158 | description: 159 | name: meta 160 | sha256: "98a7492d10d7049ea129fd4e50f7cdd2d5008522b1dfa1148bbbc542b9dd21f7" 161 | url: "https://pub.dev" 162 | source: hosted 163 | version: "1.3.0" 164 | mime: 165 | dependency: transitive 166 | description: 167 | name: mime 168 | sha256: a7a98ea7f366e2cc9d2b20873815aebec5e2bc124fe0da9d3f7f59b0625ea180 169 | url: "https://pub.dev" 170 | source: hosted 171 | version: "1.0.0" 172 | node_preamble: 173 | dependency: transitive 174 | description: 175 | name: node_preamble 176 | sha256: c133f761a6a790d0b000efa4f74eae9700bb6e9e9f5e996f0e8d6fe92703ced6 177 | url: "https://pub.dev" 178 | source: hosted 179 | version: "2.0.0" 180 | package_config: 181 | dependency: transitive 182 | description: 183 | name: package_config 184 | sha256: "20e7154d701fedaeb219dad732815ecb66677667871127998a9a6581c2aba4ba" 185 | url: "https://pub.dev" 186 | source: hosted 187 | version: "2.0.0" 188 | path: 189 | dependency: transitive 190 | description: 191 | name: path 192 | sha256: "2ad4cddff7f5cc0e2d13069f2a3f7a73ca18f66abd6f5ecf215219cdb3638edb" 193 | url: "https://pub.dev" 194 | source: hosted 195 | version: "1.8.0" 196 | pedantic: 197 | dependency: transitive 198 | description: 199 | name: pedantic 200 | sha256: "8f6460c77a98ad2807cd3b98c67096db4286f56166852d0ce5951bb600a63594" 201 | url: "https://pub.dev" 202 | source: hosted 203 | version: "1.11.0" 204 | pool: 205 | dependency: transitive 206 | description: 207 | name: pool 208 | sha256: "05955e3de2683e1746222efd14b775df7131139e07695dc8e24650f6b4204504" 209 | url: "https://pub.dev" 210 | source: hosted 211 | version: "1.5.0" 212 | pub_semver: 213 | dependency: transitive 214 | description: 215 | name: pub_semver 216 | sha256: "59ed538734419e81f7fc18c98249ae72c3c7188bdd9dceff2840585227f79843" 217 | url: "https://pub.dev" 218 | source: hosted 219 | version: "2.0.0" 220 | shelf: 221 | dependency: transitive 222 | description: 223 | name: shelf 224 | sha256: c2f658d28ec86857657dec3579e2db4dc5a6c477b6aecde870e77f0682258901 225 | url: "https://pub.dev" 226 | source: hosted 227 | version: "1.1.0" 228 | shelf_packages_handler: 229 | dependency: transitive 230 | description: 231 | name: shelf_packages_handler 232 | sha256: e0b44ebddec91e70a713e13adf93c1b2100821303b86a18e1ef1d082bd8bd9b8 233 | url: "https://pub.dev" 234 | source: hosted 235 | version: "3.0.0" 236 | shelf_static: 237 | dependency: transitive 238 | description: 239 | name: shelf_static 240 | sha256: "8584c0aa0f5756a61519b1a2fc2cd22ddbc518e9396bd33ebf06b9836bb23d13" 241 | url: "https://pub.dev" 242 | source: hosted 243 | version: "1.0.0" 244 | shelf_web_socket: 245 | dependency: transitive 246 | description: 247 | name: shelf_web_socket 248 | sha256: fd84910bf7d58db109082edf7326b75322b8f186162028482f53dc892f00332d 249 | url: "https://pub.dev" 250 | source: hosted 251 | version: "1.0.1" 252 | source_map_stack_trace: 253 | dependency: transitive 254 | description: 255 | name: source_map_stack_trace 256 | sha256: "8c463326277f68a628abab20580047b419c2ff66756fd0affd451f73f9508c11" 257 | url: "https://pub.dev" 258 | source: hosted 259 | version: "2.1.0" 260 | source_maps: 261 | dependency: transitive 262 | description: 263 | name: source_maps 264 | sha256: "52de2200bb098de739794c82d09c41ac27b2e42fd7e23cce7b9c74bf653c7296" 265 | url: "https://pub.dev" 266 | source: hosted 267 | version: "0.10.10" 268 | source_span: 269 | dependency: transitive 270 | description: 271 | name: source_span 272 | sha256: d5f89a9e52b36240a80282b3dc0667dd36e53459717bb17b8fb102d30496606a 273 | url: "https://pub.dev" 274 | source: hosted 275 | version: "1.8.1" 276 | stack_trace: 277 | dependency: transitive 278 | description: 279 | name: stack_trace 280 | sha256: f8d9f247e2f9f90e32d1495ff32dac7e4ae34ffa7194c5ff8fcc0fd0e52df774 281 | url: "https://pub.dev" 282 | source: hosted 283 | version: "1.10.0" 284 | stream_channel: 285 | dependency: transitive 286 | description: 287 | name: stream_channel 288 | sha256: db47e4797198ee601990820437179bb90219f918962318d494ada2b4b11e6f6d 289 | url: "https://pub.dev" 290 | source: hosted 291 | version: "2.1.0" 292 | string_scanner: 293 | dependency: transitive 294 | description: 295 | name: string_scanner 296 | sha256: dd11571b8a03f7cadcf91ec26a77e02bfbd6bbba2a512924d3116646b4198fc4 297 | url: "https://pub.dev" 298 | source: hosted 299 | version: "1.1.0" 300 | term_glyph: 301 | dependency: transitive 302 | description: 303 | name: term_glyph 304 | sha256: a88162591b02c1f3a3db3af8ce1ea2b374bd75a7bb8d5e353bcfbdc79d719830 305 | url: "https://pub.dev" 306 | source: hosted 307 | version: "1.2.0" 308 | test: 309 | dependency: "direct dev" 310 | description: 311 | name: test 312 | sha256: "51bed717b867dd91d75b804c8abbfb30cf904d30c00bea3d5467f3c3c7d4e064" 313 | url: "https://pub.dev" 314 | source: hosted 315 | version: "1.16.8" 316 | test_api: 317 | dependency: transitive 318 | description: 319 | name: test_api 320 | sha256: "9ddd24ec429417cfffd744115508a95f0ee956ec02a2d972dd7fbfbfe047cf45" 321 | url: "https://pub.dev" 322 | source: hosted 323 | version: "0.3.0" 324 | test_core: 325 | dependency: transitive 326 | description: 327 | name: test_core 328 | sha256: "9567514cac32e25165c78bd2c98092b3f7fd5a1555114b4f08d83830d6645e33" 329 | url: "https://pub.dev" 330 | source: hosted 331 | version: "0.3.19" 332 | typed_data: 333 | dependency: transitive 334 | description: 335 | name: typed_data 336 | sha256: "53bdf7e979cfbf3e28987552fd72f637e63f3c8724c9e56d9246942dc2fa36ee" 337 | url: "https://pub.dev" 338 | source: hosted 339 | version: "1.3.0" 340 | vm_service: 341 | dependency: transitive 342 | description: 343 | name: vm_service 344 | sha256: "9a71c98ff3cb902be7d6df76d0e14c41d2f1727a1fc38b7c354fe03abb5c1753" 345 | url: "https://pub.dev" 346 | source: hosted 347 | version: "6.1.0+1" 348 | watcher: 349 | dependency: transitive 350 | description: 351 | name: watcher 352 | sha256: "68173f2fa67d241323a4123be7ed4e43424c54befa5505d71c8ad4b7baf8f71d" 353 | url: "https://pub.dev" 354 | source: hosted 355 | version: "1.0.0" 356 | web_socket_channel: 357 | dependency: transitive 358 | description: 359 | name: web_socket_channel 360 | sha256: "500e6014efebd305a30ebf1c6006d13faa82dcd85c7a2a7793679a64ed69ec48" 361 | url: "https://pub.dev" 362 | source: hosted 363 | version: "2.0.0" 364 | webkit_inspection_protocol: 365 | dependency: transitive 366 | description: 367 | name: webkit_inspection_protocol 368 | sha256: "5adb6ab8ed14e22bb907aae7338f0c206ea21e7a27004e97664b16c120306f00" 369 | url: "https://pub.dev" 370 | source: hosted 371 | version: "1.0.0" 372 | yaml: 373 | dependency: transitive 374 | description: 375 | name: yaml 376 | sha256: "3cee79b1715110341012d27756d9bae38e650588acd38d3f3c610822e1337ace" 377 | url: "https://pub.dev" 378 | source: hosted 379 | version: "3.1.0" 380 | sdks: 381 | dart: ">=2.12.0 <4.0.0" 382 | -------------------------------------------------------------------------------- /pubspec.yaml: -------------------------------------------------------------------------------- 1 | name: 'stack' 2 | description: A package for stack data structure. have the push, pop, size, length, and top functionalities. 3 | version: 0.2.2 4 | homepage: https://github.com/ammaratef45/dart_stack 5 | 6 | environment: 7 | sdk: ">=2.12.0 <4.0.0" 8 | dev_dependencies: 9 | test: ^1.16.8 10 | -------------------------------------------------------------------------------- /test/stack_test.dart: -------------------------------------------------------------------------------- 1 | import 'dart:async'; 2 | 3 | import 'package:stack/illegal_operation_exception.dart'; 4 | import 'package:stack/stack.dart'; 5 | import 'package:test/test.dart'; 6 | 7 | void main() { 8 | group('with string', () { 9 | test('illegal size stack', () { 10 | try { 11 | Stack.sized(1); 12 | fail('should throw an exception but it did not.'); 13 | } catch (ex) { 14 | expect(ex, isA()); 15 | } 16 | }); 17 | 18 | test('is empty', () { 19 | Stack stack = Stack(); 20 | expect(stack.isEmpty, true); 21 | expect(stack.isNotEmpty, false); 22 | }); 23 | test('is not empty', () { 24 | Stack stack = Stack(); 25 | stack.push('abc'); 26 | expect(stack.isNotEmpty, true); 27 | expect(stack.isEmpty, false); 28 | }); 29 | test('push', () { 30 | Stack stack = Stack(); 31 | stack.push('abc'); 32 | }); 33 | test('push beyond maximum stack size', () { 34 | Stack stack = Stack.sized(2); 35 | try { 36 | stack.push('abc'); 37 | stack.push('def'); 38 | stack.push('ghi'); 39 | fail( 40 | 'should throw an exception for exceeding stack size, but it did not.'); 41 | } catch (ex) { 42 | expect(ex, isA()); 43 | } 44 | }); 45 | test('top', () { 46 | Stack stack = Stack(); 47 | stack.push('abc'); 48 | expect(stack.top(), 'abc'); 49 | }); 50 | test('top on empty', () { 51 | try { 52 | Stack stack = Stack(); 53 | stack.top(); 54 | fail('should throw an exception but it did not.'); 55 | } catch (ex) { 56 | expect(ex, isA()); 57 | } 58 | }); 59 | test('pop', () { 60 | Stack stack = Stack(); 61 | stack.push('abc'); 62 | stack.push('def'); 63 | expect(stack.pop(), 'def'); 64 | expect(stack.top(), 'abc'); 65 | }); 66 | test('pop on empty', () { 67 | try { 68 | Stack stack = Stack(); 69 | stack.pop(); 70 | fail('should throw an exception but it did not.'); 71 | } catch (ex) { 72 | expect(ex, isA()); 73 | } 74 | }); 75 | test('size', () { 76 | Stack stack = Stack(); 77 | stack.push('abc'); 78 | stack.push('def'); 79 | expect(stack.size(), 2); 80 | stack.pop(); 81 | expect(stack.size(), 1); 82 | }); 83 | test('length', () { 84 | Stack stack = Stack(); 85 | stack.push('abc'); 86 | stack.push('def'); 87 | expect(stack.length, 2); 88 | stack.pop(); 89 | expect(stack.length, 1); 90 | }); 91 | test('contains', () { 92 | Stack stack = Stack(); 93 | stack.push('abc'); 94 | stack.push('def'); 95 | expect(stack.contains('def'), true); 96 | stack.pop(); 97 | expect(stack.contains('def'), false); 98 | }); 99 | test('clear', () { 100 | Stack stack = Stack(); 101 | stack.push('abc'); 102 | stack.push('def'); 103 | stack.push('ghi'); 104 | expect(stack.length, 3); 105 | stack.clear(); 106 | expect(stack.length, 0); 107 | }); 108 | test('print', overridePrint(() { 109 | log.clear(); 110 | Stack stack = Stack(); 111 | stack.push('abc'); 112 | stack.push('def'); 113 | stack.print(); 114 | expect(log.length, 2); 115 | expect(log[0], 'def'); 116 | expect(log[1], 'abc'); 117 | })); 118 | }); 119 | } 120 | 121 | List log = []; 122 | void Function() overridePrint(void testFn()) => () { 123 | var spec = ZoneSpecification( 124 | print: (self, parent, zone, line) { 125 | log.add(line); 126 | }, 127 | ); 128 | return Zone.current.fork(specification: spec).run(testFn); 129 | }; 130 | --------------------------------------------------------------------------------