text:
package adapter

import (
	"context"
	"io/ioutil"
	"net/http"
	"net/http/httptest"
	"testing"

	cloudevents "github.com/cloudevents/sdk-go/v2"

	"go.uber.org/zap"

	"github.com/aws/aws-sdk-go/service/sqs"
)

func TestPostMessage_ServeHTTP(t *testing.T) {
	//Millisecond unix timestamp
	timestamp := "1542107977907"
	testCases := map[string]struct {
		sink    func(http.ResponseWriter, *http.Request)
		reqBody string
		error   bool
	}{
		"happy": {
			sink: sinkAccepted,
			//Millisecond unix timestamp
			reqBody: `{"Attributes":{"SentTimestamp":"1542107977907"},"Body":"The body","MD5OfBody":null,"MD5OfMessageAttributes":null,"MessageAttributes":null,"MessageId":"ABC01","ReceiptHandle":null}`,
		},
		"rejected": {
			sink: sinkRejected,
			//Millisecond unix timestamp
			reqBody: `{"Attributes":{"SentTimestamp":"1542107977907"},"Body":"The body","MD5OfBody":null,"MD5OfMessageAttributes":null,"MessageAttributes":null,"MessageId":"ABC01","ReceiptHandle":null}`,
			error:   true,
		},
	}
	for n, tc := range testCases {
		t.Run(n, func(t *testing.T) {
			h := &fakeHandler{
				handler: tc.sink,
			}
			sinkServer := httptest.NewServer(h)
			defer sinkServer.Close()

			a := &Adapter{
				QueueURL:             "https://sqs.us-east-2.amazonaws.com/123123/MyQueue",
				SinkURI:              sinkServer.URL,
				OnFailedPollWaitSecs: 1,
			}

			ctx := cloudevents.ContextWithTarget(context.Background(), sinkServer.URL)

			if err := a.initClient(); err != nil {
				t.Errorf("failed to create cloudevent client, %v", err)
			}

			body := "The body"
			messageID := "ABC01"
			attrs := map[string]*string{
				"SentTimestamp": &timestamp,
			}
			m := &sqs.Message{
				MessageId:  &messageID,
				Body:       &body,
				Attributes: attrs,
			}
			err := a.postMessage(ctx, zap.S(), m)

			if tc.error && err == nil {
				t.Errorf("expected error, but got %v", err)
			} else if !tc.error && err != nil {
				t.Errorf("expected no error, but got %v", err)
			}

			if tc.reqBody != string(h.body) {
				t.Errorf("expected request body %q, but got %q", tc.reqBody, h.body)
			}
		})
	}
}

func TestGetRegionInvalidUrl(t *testing.T) {
	actual, err := getRegion("sdfadf")
	if err == nil {
		t.Error("Expecting an error but got a result", actual)
	}
}

func TestGetRegionValidUrl(t *testing.T) {
	expected := "eu-west-1"
	actual, _ := getRegion("https://sqs.eu-west-1.amazonaws.com/23234/test")
	if expected != actual {
		t.Error("Expecting", expected, "but got", actual)
	}
}

func TestReceiveMessage_ServeHTTP(t *testing.T) {

	id := "ABC01"
	body := "the body"
	//Millisecond unix timestamp
	timestamp := "1542107977907"
	m := &sqs.Message{
		MessageId:  &id,
		Body:       &body,
		Attributes: map[string]*string{"SentTimestamp": &timestamp},
	}

	testCases := map[string]struct {
		sink  func(http.ResponseWriter, *http.Request)
		acked bool
	}{
		"happy": {
			sink:  sinkAccepted,
			acked: true,
		},
		"rejected": {
			sink:  sinkRejected,
			acked: false,
		},
	}
	for n, tc := range testCases {
		t.Run(n, func(t *testing.T) {
			h := &fakeHandler{
				handler: tc.sink,
			}
			sinkServer := httptest.NewServer(h)
			defer sinkServer.Close()

			a := &Adapter{
				QueueURL: "https://sqs.us-east-2.amazonaws.com/123123/MyQueue",
				SinkURI:  sinkServer.URL,
			}

			if err := a.initClient(); err != nil {
				t.Errorf("failed to create cloudevent client, %v", err)
			}

			ack := new(bool)

			a.receiveMessage(context.TODO(), m, func() { *ack = true })

			if tc.acked != *ack {
				t.Error("expected message ack ", tc.acked, " but real is ", *ack)
			}

		})
	}
}

func TestPollLoopCancels(t *testing.T) {

	a := &Adapter{
		QueueURL: "https://test.sqs.aws/123123",
		SinkURI:  "https://sink.server/",
	}

	ctx := context.Background()
	stopCh := make(chan struct{}, 1)

	msg := struct{}{}
	stopCh <- msg
	err := a.pollLoop(ctx, nil, stopCh)
	if err != nil {
		t.Error("expected pollLoop to return cleanly, but got", err)
	}
}

func TestStart(t *testing.T) {

	a := &Adapter{
		QueueURL: "https://test.sqs.aws/123123",
		SinkURI:  "https://sink.server/",
	}

	ctx := context.Background()
	stopCh := make(chan struct{}, 1)

	msg := struct{}{}
	stopCh <- msg
	err := a.Start(ctx, stopCh)
	if err != nil {
		t.Error("expected Start to return cleanly, but got", err)
	}
}

type fakeHandler struct {
	body []byte

	handler func(http.ResponseWriter, *http.Request)
}

func (h *fakeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "can not read body", http.StatusBadRequest)
		return
	}
	h.body = body

	defer r.Body.Close()
	h.handler(w, r)
}

func sinkAccepted(writer http.ResponseWriter, req *http.Request) {
	writer.WriteHeader(http.StatusOK)
}

func sinkRejected(writer http.ResponseWriter, _ *http.Request) {
	writer.WriteHeader(http.StatusRequestTimeout)
}


text:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package pl.panryba.mc.auth;

/**
 *
 * @author PanRyba.pl
 */
public enum ChangePasswordResult {
    CHANGED,
    INVALID,
    FAIL
}

redpajama_set_name: RedPajamaGithub


text:
A MATLAB framework for hyperspectral cube analysis.

## Ingredients
This package contains the following components:

  - [The main Cube class](@Cube/Cube.m)
  - [Wrapper class](@ENVI/ENVI.m) for reading and writing cube data to ENVI files. Requires [ENVI file reader / writer](http://se.mathworks.com/matlabcentral/fileexchange/27172-envi-file-reader-writer)
  - [An InputParser subclass](@CubeArgs/CubeArgs.m) that handles some of the argument parsing
  - [Utility functions](@Utils/Utils.m) used by the class, collected as a class
  - [Unit tests](tests/) for the above components
  - [An example gallery](examples/)

## Usage
1. Clone the repository:

   `git clone git@github.com:silmae/hsicube.git`

2. Add this directory to your MATLAB path to bring the main classes into scope:

   `addpath('hsicube')`

Additional functionality:
- To handle ENVI files using the included `ENVI` wrapper class, include the files from [ENVI file reader / writer][envi] in your path beforehand.
- For nice color maps in visualizations, add [Brewermap][colorbrewer] to your path and it will be used automatically.
- To use the `slice()` method, download [im_cube_slicer][slicer] and add it to path.

[envi]: http://se.mathworks.com/matlabcentral/fileexchange/27172-envi-file-reader-writer
[colorbrewer]: https://github.com/DrosteEffect/BrewerMap
[slicer]: https://se.mathworks.com/matlabcentral/fileexchange/40076-hyperspectral-image-cube-slicer

Use `doc Cube` to view the basic feature list and documentation of the main class. The [Examples](examples/) directory contains some examples (which you can view easily as HTML by running `publish('examplefile.m')` in MATLAB.

If you wish to delve deeper, most of the code should be well commented. You can also see the [Cube](tests/CubeTest/) or [ENVI test files](tests/ENVITest/) for expected behaviors of the different methods.

## Citing
If you use this package for scientific work, you can cite the following conference paper:

> Software Framework For Hyperspectral Data Exploration and Processing in MATLAB

> M. A. Eskelinen

> Int. Arch. Photogramm. Remote Sens. Spatial Inf. Sci., XLII-3-W3, 47-50, https://doi.org/10.5194/isprs-archives-XLII-3-W3-47-2017, 2017

Or in BibTeX format:

> @Article{isprs-archives-XLII-3-W3-47-2017,

> AUTHOR = {Eskelinen, M. A.},

> TITLE = {SOFTWARE FRAMEWORK FOR HYPERSPECTRAL DATA EXPLORATION AND PROCESSING IN MATLAB},

> JOURNAL = {ISPRS - International Archives of the Photogrammetry, Remote Sensing and Spatial Information Sciences},

> VOLUME = {XLII-3/W3},

> YEAR = {2017},

> PAGES = {47--50},

> URL = {https://www.int-arch-photogramm-remote-sens-spatial-inf-sci.net/XLII-3-W3/47/2017/},

> DOI = {10.5194/isprs-archives-XLII-3-W3-47-2017}

> }

For referring to specific versions, you can look for the corresponding version on [Zenodo](https://zenodo.org/search?q=hsicube).

## Author
2017 Matti A. Eskelinen, University of Jyväskylä, Finland


text:
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.documentation.render;

import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.documentation.DocumentationActionProvider;
import com.intellij.codeInsight.documentation.DocumentationComponent;
import com.intellij.codeInsight.documentation.DocumentationManager;
import com.intellij.codeInsight.documentation.QuickDocUtil;
import com.intellij.icons.AllIcons;
import com.intellij.ide.BrowserUtil;
import com.intellij.ide.IdeEventQueue;
import com.intellij.ide.ui.UISettings;
import com.intellij.lang.documentation.InlineDocumentation;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.*;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.event.CaretEvent;
import com.intellij.openapi.editor.event.CaretListener;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.impl.EditorCssFontResolver;
import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.editor.markup.TextAttributes;
import com.intellij.openapi.ide.CopyPasteManager;
import com.intellij.openapi.keymap.KeymapManager;
import com.intellij.openapi.keymap.KeymapUtil;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.pom.Navigatable;
import com.intellij.psi.PsiDocCommentBase;
import com.intellij.psi.PsiElement;
import com.intellij.ui.AppUIUtil;
import com.intellij.ui.ColorUtil;
import com.intellij.ui.Graphics2DDelegate;
import com.intellij.ui.popup.PopupFactoryImpl;
import com.intellij.ui.scale.JBUIScale;
import com.intellij.util.text.CharArrayUtil;
import com.intellij.util.ui.HTMLEditorKitBuilder;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.StyleSheetUtil;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.text.*;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.ImageView;
import javax.swing.text.html.StyleSheet;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.MouseEvent;
import java.awt.font.TextAttribute;
import java.awt.geom.Rectangle2D;
import java.awt.image.ImageObserver;
import java.util.List;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;

import static com.intellij.codeInsight.documentation.QuickDocUtil.isDocumentationV2Enabled;
import static com.intellij.lang.documentation.ide.impl.DocumentationManager.instance;

final class DocRenderer implements CustomFoldRegionRenderer {
  private static final Logger LOG = Logger.getInstance(DocRenderer.class);
  private static final Key<EditorPane> CACHED_LOADING_PANE = Key.create("cached.loading.pane");
  private static final DocRendererMemoryManager MEMORY_MANAGER = new DocRendererMemoryManager();
  private static final DocRenderImageManager IMAGE_MANAGER = new DocRenderImageManager();

  private static final int MIN_WIDTH = 350;
  private static final int MAX_WIDTH = 680;
  private static final int LEFT_INSET = 14;
  private static final int RIGHT_INSET = 12;
  private static final int TOP_BOTTOM_INSETS = 2;
  private static final int TOP_BOTTOM_MARGINS = 4;
  private static final int LINE_WIDTH = 2;
  private static final int ARC_RADIUS = 5;

  private static StyleSheet ourCachedStyleSheet;
  private static String ourCachedStyleSheetLinkColor = "non-existing";

  final DocRenderItem myItem;
  private boolean myContentUpdateNeeded;
  private EditorPane myPane;
  private int myCachedWidth = -1;
  private int myCachedHeight = -1;

  DocRenderer(@NotNull DocRenderItem item) {
    myItem = item;
  }
  void update(boolean updateSize, boolean updateContent, List<Runnable> foldingTasks) {
    CustomFoldRegion foldRegion = myItem.foldRegion;
    if (foldRegion != null) {
      if (updateSize) {
        myCachedWidth = -1;
        myCachedHeight = -1;
      }
      myContentUpdateNeeded = updateContent;
      Runnable task = () -> foldRegion.update();
      if (foldingTasks == null) {
        task.run();
      }
      else {
        foldingTasks.add(task);
      }
    }
  }

  @Override
  public int calcWidthInPixels(@NotNull CustomFoldRegion region) {
    if (myCachedWidth < 0) {
      return myCachedWidth = calcWidth(region.getEditor());
    }
    else {
      return myCachedWidth;
    }
  }

  @Override
  public int calcHeightInPixels(@NotNull CustomFoldRegion region) {
    if (myCachedHeight < 0) {
      Editor editor = region.getEditor();
      int indent = 0;
      // optimize editor opening: skip 'proper' width calculation for 'Loading...' inlays
      if (myItem.textToRender != null) {
        indent = calcInlayStartX() - editor.getInsets().left;
      }
      int width = Math.max(0, calcWidth(editor) - indent - scale(LEFT_INSET) - scale(RIGHT_INSET));
      JComponent component = getRendererComponent(editor, width);
      return myCachedHeight = Math.max(editor.getLineHeight(),
                                       component.getPreferredSize().height + scale(TOP_BOTTOM_INSETS) * 2 + scale(TOP_BOTTOM_MARGINS) * 2);
    }
    else {
      return myCachedHeight;
    }
  }

  @Override
  public void paint(@NotNull CustomFoldRegion region,
                    @NotNull Graphics2D g,
                    @NotNull Rectangle2D r,
                    @NotNull TextAttributes textAttributes) {
    int startX = calcInlayStartX();
    int endX = (int)r.getX() + (int)r.getWidth();
    if (startX >= endX) return;
    int margin = scale(TOP_BOTTOM_MARGINS);
    int filledHeight = (int)r.getHeight() - margin * 2;
    if (filledHeight <= 0) return;
    int filledStartY = (int)r.getY() + margin;

    Editor editor = region.getEditor();
    Color defaultBgColor = ((EditorEx)editor).getBackgroundColor();
    Color currentBgColor = textAttributes.getBackgroundColor();
    Color bgColor = currentBgColor == null ? defaultBgColor
                                           : ColorUtil.mix(defaultBgColor, textAttributes.getBackgroundColor(), .5);
    if (currentBgColor != null) {
      g.setColor(bgColor);
      int arcDiameter = ARC_RADIUS * 2;
      if (endX - startX >= arcDiameter) {
        g.fillRect(startX, filledStartY, endX - startX - ARC_RADIUS, filledHeight);
        Object savedHint = g.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.fillRoundRect(endX - arcDiameter, filledStartY, arcDiameter, filledHeight, arcDiameter, arcDiameter);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, savedHint);
      }
      else {
        g.fillRect(startX, filledStartY, endX - startX, filledHeight);
      }
    }
    g.setColor(editor.getColorsScheme().getColor(DefaultLanguageHighlighterColors.DOC_COMMENT_GUIDE));
    g.fillRect(startX, filledStartY, scale(LINE_WIDTH), filledHeight);

    int topBottomInset = scale(TOP_BOTTOM_INSETS);
    int componentWidth = endX - startX - scale(LEFT_INSET) - scale(RIGHT_INSET);
    int componentHeight = filledHeight - topBottomInset * 2;
    if (componentWidth > 0 && componentHeight > 0) {
      JComponent component = getRendererComponent(editor, componentWidth);
      component.setBackground(bgColor);
      Graphics dg = g.create(startX + scale(LEFT_INSET), filledStartY + topBottomInset, componentWidth, componentHeight);
      UISettings.setupAntialiasing(dg);
      component.paint(dg);
      dg.dispose();
    }
  }

  @Override
  public @Nullable GutterIconRenderer calcGutterIconRenderer(@NotNull CustomFoldRegion region) {
    DocRenderItem.MyGutterIconRenderer highlighterIconRenderer =
      (DocRenderItem.MyGutterIconRenderer)myItem.highlighter.getGutterIconRenderer();
    return highlighterIconRenderer == null ? null : myItem.new MyGutterIconRenderer(AllIcons.Gutter.JavadocEdit,
                                                                                highlighterIconRenderer.isIconVisible());
  }

  @Override
  public ActionGroup getContextMenuGroup(@NotNull CustomFoldRegion region) {
    DefaultActionGroup group = new DefaultActionGroup();
    group.add(new CopySelection());
    group.addSeparator();
    group.add(myItem.createToggleAction());
    AnAction toggleRenderAllAction = ActionManager.getInstance().getAction(IdeActions.ACTION_TOGGLE_RENDERED_DOC_FOR_ALL);
    if (toggleRenderAllAction != null) {
      group.add(toggleRenderAllAction);
    }
    group.add(new DocRenderItem.ChangeFontSize());

    PsiDocCommentBase comment = getComment();
    for (DocumentationActionProvider provider : DocumentationActionProvider.EP_NAME.getExtensions()) {
      provider.additionalActions(myItem.editor, comment, myItem.textToRender).forEach(group::add);
    }

    return group;
  }

  private @Nullable PsiDocCommentBase getComment() {
    InlineDocumentation documentation = myItem.getInlineDocumentation();
    return documentation instanceof PsiCommentInlineDocumentation
           ? ((PsiCommentInlineDocumentation)documentation).getComment()
           : null;
  }

  private static int scale(int value) {
    return (int)(value * UISettings.getDefFontScale());
  }

  static int calcWidth(@NotNull Editor editor) {
    int availableWidth = editor.getScrollingModel().getVisibleArea().width;
    if (availableWidth <= 0) {
      // if editor is not shown yet, we create the inlay with maximum possible width,
      // assuming that there's a higher probability that editor will be shown with larger width than with smaller width
      return MAX_WIDTH;
    }
    return Math.max(scale(MIN_WIDTH), Math.min(scale(MAX_WIDTH), availableWidth));
  }

  private int calcInlayStartX() {
    Editor editor = myItem.editor;
    RangeHighlighter highlighter = myItem.highlighter;
    if (highlighter.isValid()) {
      Document document = editor.getDocument();
      int nextLineNumber = document.getLineNumber(highlighter.getEndOffset()) + 1;
      if (nextLineNumber < document.getLineCount()) {
        int lineStartOffset = document.getLineStartOffset(nextLineNumber);
        int contentStartOffset = CharArrayUtil.shiftForward(document.getImmutableCharSequence(), lineStartOffset, " \t\n");
        return editor.offsetToXY(contentStartOffset, false, true).x;
      }
    }
    return editor.getInsets().left;
  }

  Rectangle getEditorPaneBoundsWithinRenderer(int width, int height) {
    int relativeX = calcInlayStartX() - myItem.editor.getInsets().left + scale(LEFT_INSET);
    int relativeY = scale(TOP_BOTTOM_MARGINS) + scale(TOP_BOTTOM_INSETS);
    return new Rectangle(relativeX, relativeY, width - relativeX - scale(RIGHT_INSET), height - relativeY * 2);
  }

  EditorPane getRendererComponent(Editor editor, int width) {
    boolean newInstance = false;
    EditorPane pane = myPane;
    if (pane == null || myContentUpdateNeeded) {
      myContentUpdateNeeded = false;
      clearCachedComponent();
      if (myItem.textToRender == null) {
        pane = getLoadingPane(editor);
      }
      else {
        myPane = pane = createEditorPane(editor, myItem.textToRender, false);
        newInstance = true;
      }
    }
    AppUIUtil.targetToDevice(pane, editor.getContentComponent());
    pane.setSize(width, 10_000_000 /* Arbitrary large value, that doesn't lead to overflows and precision loss */);
    if (newInstance) {
      // trigger internal layout, so that image elements are created
      // this is done after 'targetToDevice' call to take correct graphics context into account
      pane.getPreferredSize();
      pane.startImageTracking();
    }
    return pane;
  }

  private EditorPane getLoadingPane(@NotNull Editor editor) {
    EditorPane pane = editor.getUserData(CACHED_LOADING_PANE);
    if (pane == null) {
      editor.putUserData(CACHED_LOADING_PANE, pane = createEditorPane(editor, CodeInsightBundle.message("doc.render.loading.text"), true));
    }
    return pane;
  }

  static void clearCachedLoadingPane(@NotNull Editor editor) {
    editor.putUserData(CACHED_LOADING_PANE, null);
  }

  private EditorPane createEditorPane(@NotNull Editor editor, @Nls @NotNull String text, boolean reusable) {
    EditorPane pane = new EditorPane(!reusable);
    pane.setEditable(false);
    pane.getCaret().setSelectionVisible(!reusable);
    pane.putClientProperty("caretWidth", 0); // do not reserve space for caret (making content one pixel narrower than component)
    pane.setEditorKit(createEditorKit(editor));
    pane.setBorder(JBUI.Borders.empty());
    Map<TextAttribute, Object> fontAttributes = new HashMap<>();
    fontAttributes.put(TextAttribute.SIZE, JBUIScale.scale(DocumentationComponent.getQuickDocFontSize().getSize()));
    // disable kerning for now - laying out all fragments in a file with it takes too much time
    fontAttributes.put(TextAttribute.KERNING, 0);
    pane.setFont(pane.getFont().deriveFont(fontAttributes));
    Color textColor = getTextColor(editor.getColorsScheme());
    pane.setForeground(textColor);
    pane.setSelectedTextColor(textColor);
    pane.setSelectionColor(editor.getSelectionModel().getTextAttributes().getBackgroundColor());
    UIUtil.enableEagerSoftWrapping(pane);
    pane.setText(text);
    pane.addHyperlinkListener(e -> {
      if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
        activateLink(e);
      }
    });
    pane.getDocument().putProperty("imageCache", IMAGE_MANAGER.getImageProvider());
    return pane;
  }

  void clearCachedComponent() {
    if (myPane != null) {
      myPane.dispose();
      myPane = null;
    }
  }

  void dispose() {
    clearCachedComponent();
  }

  private static @NotNull Color getTextColor(@NotNull EditorColorsScheme scheme) {
    TextAttributes attributes = scheme.getAttributes(DefaultLanguageHighlighterColors.DOC_COMMENT);
    Color color = attributes == null ? null : attributes.getForegroundColor();
    return color == null ? scheme.getDefaultForeground() : color;
  }

  private void activateLink(HyperlinkEvent event) {
    Element element = event.getSourceElement();
    if (element == null) return;

    Rectangle2D location = null;
    try {
      location = ((JEditorPane)event.getSource()).modelToView2D(element.getStartOffset());
    }
    catch (BadLocationException ignored) {
    }
    if (location == null) return;

    String url = event.getDescription();
    if (isDocumentationV2Enabled()) {
      activateLinkV2(url, location);
      return;
    }

    InlineDocumentation documentation = myItem.getInlineDocumentation();
    if (documentation == null) return;

    PsiElement context = ((PsiCommentInlineDocumentation)documentation).getContext();
    if (isGotoDeclarationEvent()) {
      navigateToDeclaration(context, url);
    }
    else {
      showDocumentation(myItem.editor, context, url, location);
    }
  }

  private void activateLinkV2(@NotNull String url, @NotNull Rectangle2D location) {
    Editor editor = myItem.editor;
    Project project = editor.getProject();
    if (project == null) {
      return;
    }
    if (isGotoDeclarationEvent()) {
      instance(project).navigateInlineLink(
        url, myItem::getInlineDocumentationTarget
      );
    }
    else {
      instance(project).activateInlineLink(
        url, myItem::getInlineDocumentationTarget,
        editor, popupPosition(location)
      );
    }
  }

  private static boolean isGotoDeclarationEvent() {
    KeymapManager keymapManager = KeymapManager.getInstance();
    if (keymapManager == null) return false;
    AWTEvent event = IdeEventQueue.getInstance().getTrueCurrentEvent();
    if (!(event instanceof MouseEvent)) return false;
    MouseShortcut mouseShortcut = KeymapUtil.createMouseShortcut((MouseEvent)event);
    return keymapManager.getActiveKeymap().getActionIds(mouseShortcut).contains(IdeActions.ACTION_GOTO_DECLARATION);
  }

  private static void navigateToDeclaration(@NotNull PsiElement context, @NotNull String linkUrl) {
    PsiElement targetElement = DocumentationManager.getInstance(context.getProject()).getTargetElement(context, linkUrl);
    if (targetElement instanceof Navigatable) {
      ((Navigatable)targetElement).navigate(true);
    }
  }

  /**
   * @deprecated Unused in v2 implementation.
   */
  @Deprecated
  private void showDocumentation(@NotNull Editor editor,
                                 @NotNull PsiElement context,
                                 @NotNull String linkUrl,
                                 @NotNull Rectangle2D linkLocationWithinInlay) {
    if (isExternalLink(linkUrl)) {
      BrowserUtil.open(linkUrl);
      return;
    }
    Project project = context.getProject();
    DocumentationManager documentationManager = DocumentationManager.getInstance(project);
    if (QuickDocUtil.getActiveDocComponent(project) == null) {
      editor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POINT, popupPosition(linkLocationWithinInlay));
      documentationManager.showJavaDocInfo(editor, context, context, () -> {
        editor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POINT, null);
      }, "", false, true);
    }
    DocumentationComponent component = QuickDocUtil.getActiveDocComponent(project);
    if (component != null) {
      if (!documentationManager.hasActiveDockedDocWindow()) {
        component.startWait();
      }
      documentationManager.navigateByLink(component, context, linkUrl);
    }
    if (documentationManager.getDocInfoHint() == null) {
      editor.putUserData(PopupFactoryImpl.ANCHOR_POPUP_POINT, null);
    }
    if (documentationManager.hasActiveDockedDocWindow()) {
      Disposable disposable = Disposer.newDisposable();
      editor.getCaretModel().addCaretListener(new CaretListener() {
        @Override
        public void caretPositionChanged(@NotNull CaretEvent e) {
          Disposer.dispose(disposable);
        }
      }, disposable);
      documentationManager.muteAutoUpdateTill(disposable);
    }
  }

  private @NotNull Point popupPosition(@NotNull Rectangle2D linkLocationWithinInlay) {
    CustomFoldRegion foldRegion = myItem.foldRegion;
    Point rendererPosition = Objects.requireNonNull(foldRegion.getLocation());
    Rectangle relativeBounds = getEditorPaneBoundsWithinRenderer(foldRegion.getWidthInPixels(), foldRegion.getHeightInPixels());
    return new Point(
      rendererPosition.x + relativeBounds.x + (int)linkLocationWithinInlay.getX(),
      rendererPosition.y + relativeBounds.y + (int)Math.ceil(linkLocationWithinInlay.getMaxY())
    );
  }

  private static boolean isExternalLink(@NotNull String linkUrl) {
    String l = linkUrl.toLowerCase(Locale.ROOT);
    return l.startsWith("http://") || l.startsWith("https://");
  }

  private static EditorKit createEditorKit(@NotNull Editor editor) {
    HTMLEditorKit editorKit =
      new HTMLEditorKitBuilder()
        .withViewFactoryExtensions((element, view) -> view instanceof ImageView ? new MyScalingImageView(element) : null)
        .withFontResolver(EditorCssFontResolver.getInstance(editor))
        .build();
    editorKit.getStyleSheet().addStyleSheet(getStyleSheet(editor));
    return editorKit;
  }

  private static StyleSheet getStyleSheet(@NotNull Editor editor) {
    EditorColorsScheme colorsScheme = editor.getColorsScheme();
    Color linkColor = colorsScheme.getColor(DefaultLanguageHighlighterColors.DOC_COMMENT_LINK);
    if (linkColor == null) linkColor = getTextColor(colorsScheme);
    String linkColorHex = ColorUtil.toHex(linkColor);
    if (!Objects.equals(linkColorHex, ourCachedStyleSheetLinkColor)) {
      String editorFontNamePlaceHolder = EditorCssFontResolver.EDITOR_FONT_NAME_NO_LIGATURES_PLACEHOLDER;
      ourCachedStyleSheet = StyleSheetUtil.loadStyleSheet(
        "body {overflow-wrap: anywhere}" + // supported by JetBrains Runtime
        "code {font-family: \"" + editorFontNamePlaceHolder + "\"}" +
        "pre {font-family: \"" + editorFontNamePlaceHolder + "\";" +
        "white-space: pre-wrap}" + // supported by JetBrains Runtime
        "h1, h2, h3, h4, h5, h6 {margin-top: 0; padding-top: 1}" +
        "a {color: #" + linkColorHex + "; text-decoration: none}" +
        "p {padding: 7 0 2 0}" +
        "ol {padding: 0 20 0 0}" +
        "ul {padding: 0 20 0 0}" +
        "li {padding: 1 0 2 0}" +
        "li p {padding-top: 0}" +
        "table p {padding-bottom: 0}" +
        "th {text-align: left}" +
        "td {padding: 2 0 2 0}" +
        "td p {padding-top: 0}" +
        ".sections {border-spacing: 0}" +
        ".section {padding-right: 5; white-space: nowrap}" +
        ".content {padding: 2 0 2 0}"
      );
      ourCachedStyleSheetLinkColor = linkColorHex;
    }
    return ourCachedStyleSheet;
  }

  class EditorPane extends JEditorPane {
    private final List<Image> myImages = new ArrayList<>();
    private final AtomicBoolean myUpdateScheduled = new AtomicBoolean();
    private final AtomicBoolean myRepaintScheduled = new AtomicBoolean();
    private final ImageObserver myImageObserver = new ImageObserver() {
      @Override
      public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
        if ((infoflags & (ImageObserver.WIDTH | ImageObserver.HEIGHT)) != 0) {
          scheduleUpdate();
          return false;
        }
        return true;
      }
    };
    private boolean myRepaintRequested;

    EditorPane(boolean trackMemory) {
      if (trackMemory) {
        MEMORY_MANAGER.register(DocRenderer.this, 50 /* rough size estimation */);
      }
    }

    @Override
    public void repaint(long tm, int x, int y, int width, int height) {
      myRepaintRequested = true;
    }

    void doWithRepaintTracking(Runnable task) {
      myRepaintRequested = false;
      task.run();
      if (myRepaintRequested) repaintRenderer();
    }

    private void repaintRenderer() {
      CustomFoldRegion foldRegion = myItem.foldRegion;
      if (foldRegion != null) {
        foldRegion.repaint();
      }
    }

    @Override
    public void paint(Graphics g) {
      MEMORY_MANAGER.notifyPainted(DocRenderer.this);
      for (Image image : myImages) {
        IMAGE_MANAGER.notifyPainted(image);
      }
      super.paint(g);
    }

    Editor getEditor() {
      return myItem.editor;
    }

    void removeSelection() {
      doWithRepaintTracking(() -> select(0, 0));
    }

    boolean hasSelection() {
      return getSelectionStart() != getSelectionEnd();
    }

    @Nullable Point getSelectionPositionInEditor() {
      if (myPane != this) {
        return null;
      }
      CustomFoldRegion foldRegion = myItem.foldRegion;
      if (foldRegion == null || foldRegion.getRenderer() != DocRenderer.this) {
        return null;
      }
      Point rendererLocation = foldRegion.getLocation();
      if (rendererLocation == null) {
        return null;
      }
      Rectangle boundsWithinRenderer = getEditorPaneBoundsWithinRenderer(foldRegion.getWidthInPixels(), foldRegion.getHeightInPixels());
      Rectangle2D locationInPane;
      try {
        locationInPane = modelToView2D(getSelectionStart());
      }
      catch (BadLocationException e) {
        LOG.error(e);
        locationInPane = new Rectangle();
      }
      return new Point(rendererLocation.x + boundsWithinRenderer.x + (int)locationInPane.getX(),
                       rendererLocation.y + boundsWithinRenderer.y + (int)locationInPane.getY());
    }

    private void scheduleUpdate() {
      if (myUpdateScheduled.compareAndSet(false, true)) {
        SwingUtilities.invokeLater(() -> {
          myRepaintScheduled.set(false);
          myUpdateScheduled.set(false);
          if (this == myPane) {
            CustomFoldRegion foldRegion = myItem.foldRegion;
            if (foldRegion != null) {
              DocRenderItemUpdater.getInstance().updateFoldRegions(Collections.singleton(foldRegion), false);
            }
          }
        });
      }
    }

    private void scheduleRepaint() {
      if (!myUpdateScheduled.get() && myRepaintScheduled.compareAndSet(false, true)) {
        SwingUtilities.invokeLater(() -> {
          myRepaintScheduled.set(false);
          if (this == myPane) {
            repaintRenderer();
          }
        });
      }
    }

    void startImageTracking() {
      collectImages(getUI().getRootView(this));
      boolean update = false;
      for (Image image : myImages) {
        IMAGE_MANAGER.setCompletionListener(image, this::scheduleRepaint);
        update |= image.getWidth(myImageObserver) >= 0 || image.getHeight(myImageObserver) >= 0;
      }
      if (update) {
        myImageObserver.imageUpdate(null, ImageObserver.WIDTH | ImageObserver.HEIGHT, 0, 0, 0, 0);
      }
    }

    private void collectImages(View view) {
      if (view instanceof ImageView) {
        Image image = ((ImageView)view).getImage();
        if (image != null) {
          myImages.add(image);
        }
      }
      int childCount = view.getViewCount();
      for (int i = 0; i < childCount; i++) {
        collectImages(view.getView(i));
      }
    }

    void dispose() {
      MEMORY_MANAGER.unregister(DocRenderer.this);
      myImages.forEach(image -> IMAGE_MANAGER.dispose(image));
    }
  }

  private static final class MyScalingImageView extends ImageView {
    private int myAvailableWidth;

    private MyScalingImageView(Element element) {
      super(element);
    }

    @Override
    public int getResizeWeight(int axis) {
      return 1;
    }

    @Override
    public float getMaximumSpan(int axis) {
      return getPreferredSpan(axis);
    }

    @Override
    public float getPreferredSpan(int axis) {
      float baseSpan = super.getPreferredSpan(axis);
      if (axis == View.X_AXIS) {
        return baseSpan;
      }
      else {
        int availableWidth = getAvailableWidth();
        if (availableWidth <= 0) return baseSpan;
        float baseXSpan = super.getPreferredSpan(View.X_AXIS);
        if (baseXSpan <= 0) return baseSpan;
        if (availableWidth > baseXSpan) {
          availableWidth = (int)baseXSpan;
        }
        if (myAvailableWidth > 0 && availableWidth != myAvailableWidth) {
          preferenceChanged(null, false, true);
        }
        myAvailableWidth = availableWidth;
        return baseSpan * availableWidth / baseXSpan;
      }
    }

    private int getAvailableWidth() {
      for (View v = this; v != null; ) {
        View parent = v.getParent();
        if (parent instanceof FlowView) {
          int childCount = parent.getViewCount();
          for (int i = 0; i < childCount; i++) {
            if (parent.getView(i) == v) {
              return ((FlowView)parent).getFlowSpan(i);
            }
          }
        }
        v = parent;
      }
      return 0;
    }

    @Override
    public void paint(Graphics g, Shape a) {
      Rectangle targetRect = (a instanceof Rectangle) ? (Rectangle)a : a.getBounds();
      Graphics scalingGraphics = new Graphics2DDelegate((Graphics2D)g) {
        @Override
        public boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer) {
          int maxWidth = Math.max(0, targetRect.width - 2 * (x - targetRect.x)); // assuming left and right insets are the same
          int maxHeight = Math.max(0, targetRect.height - 2 * (y - targetRect.y)); // assuming top and bottom insets are the same
          if (width > maxWidth) {
            height = height * maxWidth / width;
            width = maxWidth;
          }
          if (height > maxHeight) {
            width = width * maxHeight / height;
            height = maxHeight;
          }
          return super.drawImage(img, x, y, width, height, observer);
        }
      };
      super.paint(scalingGraphics, a);
    }
  }

  private class CopySelection extends DumbAwareAction {
    CopySelection() {
      super(CodeInsightBundle.messagePointer("doc.render.copy.action.text"), AllIcons.Actions.Copy);
      AnAction copyAction = ActionManager.getInstance().getAction(IdeActions.ACTION_COPY);
      if (copyAction != null) {
        copyShortcutFrom(copyAction);
      }
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
      e.getPresentation().setVisible(myPane != null && myPane.hasSelection());
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
      String text = myPane == null ? null : myPane.getSelectedText();
      if (!StringUtil.isEmpty(text)) {
        CopyPasteManager.getInstance().setContents(new StringSelection(text));
      }
    }
  }
}


text:
'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.reducer = exports.actions = undefined;

var _lodash;

function _load_lodash() {
  return _lodash = _interopRequireDefault(require('lodash'));
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const INITIAL_PROJECT_STATE = {
  count: 0,
  color: '#595C68',
  info: [],
  warn: [],
  error: []
};

const GLOBAL = 'global';

const actions = exports.actions = {
  // logLevel = 'warning', 'error', or 'info'
  add: (projectRoot, id, message, tag, logLevel) => {
    return {
      type: 'ADD_NOTIFICATION',
      projectRoot,
      id,
      message,
      tag,
      logLevel
    };
  },

  clear: (projectRoot, id) => {
    return {
      type: 'CLEAR_NOTIFICATION',
      projectRoot,
      id
    };
  },

  addGlobal: (id, message, tag, logLevel) => {
    return {
      type: 'ADD_NOTIFICATION',
      projectRoot: GLOBAL,
      id,
      message,
      tag,
      logLevel
    };
  },

  clearGlobal: id => {
    return {
      type: 'CLEAR_NOTIFICATION',
      projectRoot: GLOBAL,
      id
    };
  }
};

const reducer = exports.reducer = (state = {}, action) => {
  switch (action.type) {
    case 'ADD_NOTIFICATION':
      return _addNotification(state, action);
    case 'CLEAR_NOTIFICATION':
      return _clearNotification(state, action);
    default:
      return state;
  }
};

function _addNotification(state, action) {
  let { projectRoot, id, message, tag, logLevel } = action;

  if (logLevel !== 'info' && logLevel !== 'warn' && logLevel !== 'error') {
    logLevel = 'info';
  }

  let projectObject = state[projectRoot] || INITIAL_PROJECT_STATE;
  projectObject = JSON.parse(JSON.stringify(projectObject));
  let arrayOfIssues = projectObject[logLevel];
  let index = (_lodash || _load_lodash()).default.findIndex(arrayOfIssues, { id });
  if (index === -1) {
    arrayOfIssues.push({
      id,
      message,
      tag,
      count: 0
    });
  } else {
    arrayOfIssues[index] = {
      id,
      message,
      tag,
      count: arrayOfIssues[index].count + 1
    };
  }

  // TODO: switch to immutable.js
  let newState = JSON.parse(JSON.stringify(state));
  projectObject[logLevel] = arrayOfIssues;
  _setCount(projectObject);
  newState[projectRoot] = projectObject;
  return newState;
}

function _clearNotification(state, action) {
  let { projectRoot, id } = action;

  if (!state[projectRoot]) {
    return state;
  }

  let projectObject = state[projectRoot];
  let newProjectObject = {};
  (_lodash || _load_lodash()).default.forEach(projectObject, function (array, key) {
    if (Array.isArray(array)) {
      (_lodash || _load_lodash()).default.remove(array, notification => {
        if (notification.id === id) {
          console.log('REMOVED' + id);
        }
        return notification.id === id;
      });

      newProjectObject[key] = array;
    }
  });

  _setCount(newProjectObject);
  // TODO: switch to immutable.js
  let newState = JSON.parse(JSON.stringify(state));
  newState[projectRoot] = newProjectObject;
  return newState;
}

function _setCount(projectObject) {
  projectObject.count = projectObject.warn.length + projectObject.error.length;
  if (projectObject.count === 0) {
    projectObject.color = '#595C68';
  } else {
    projectObject.color = projectObject.error.length > 0 ? '#F6345D' : '#FF8C00';
  }

  return projectObject;
}
//# sourceMappingURL=../../__sourcemaps__/state/reducers/notifications.js.map


text:
```bash
npm install
npm test
```

## Create code coverage "di-cov.html"

```bash
npm install
npm run test-cover
```


text:
/*
 * tms380tr.h: TI TMS380 Token Ring driver for Linux
 *
 * Authors:
 * - Christoph Goos <cgoos@syskonnect.de>
 * - Adam Fritzler
 */

#ifndef __LINUX_TMS380TR_H
#define __LINUX_TMS380TR_H

#ifdef __KERNEL__

#include <linux/interrupt.h>

/* module prototypes */
extern const struct net_device_ops tms380tr_netdev_ops;
int tms380tr_open(struct net_device *dev);
int tms380tr_close(struct net_device *dev);
irqreturn_t tms380tr_interrupt(int irq, void *dev_id);
int tmsdev_init(struct net_device *dev, struct device *pdev);
void tmsdev_term(struct net_device *dev);
void tms380tr_wait(unsigned long time);

#define TMS380TR_MAX_ADAPTERS 7

#define SEND_TIMEOUT 10*HZ

#define TR_RCF_LONGEST_FRAME_MASK 0x0070
#define TR_RCF_FRAME4K 0x0030

/*------------------------------------------------------------------*/
/*  Bit order for adapter communication with DMA		    */
/*  --------------------------------------------------------------  */
/*  Bit  8 | 9| 10| 11|| 12| 13| 14| 15|| 0| 1| 2| 3|| 4| 5| 6| 7|  */
/*  --------------------------------------------------------------  */
/*  The bytes in a word must be byte swapped. Also, if a double	    */
/*  word is used for storage, then the words, as well as the bytes, */
/*  must be swapped. 						    */
/*  Bit order for adapter communication with DIO 		    */
/*  --------------------------------------------------------------  */
/*  Bit  0 | 1| 2| 3|| 4| 5| 6| 7|| 8| 9| 10| 11|| 12| 13| 14| 15|  */
/*  --------------------------------------------------------------  */
/*------------------------------------------------------------------*/

/* Swap words of a long.                        */
#define SWAPW(x) (((x) << 16) | ((x) >> 16))

/* Get the low byte of a word.                      */
#define LOBYTE(w)       ((unsigned char)(w))

/* Get the high byte of a word.                     */
#define HIBYTE(w)       ((unsigned char)((unsigned short)(w) >> 8))

/* Get the low word of a long.                      */
#define LOWORD(l)       ((unsigned short)(l))

/* Get the high word of a long.                     */
#define HIWORD(l)       ((unsigned short)((unsigned long)(l) >> 16))

/* Token ring adapter I/O addresses for normal mode. */

/*
 * The SIF registers.  Common to all adapters.
 */
/* Basic SIF (SRSX = 0) */
#define SIFDAT      		0x00	/* SIF/DMA data. */
#define SIFINC      		0x02  	/* IO Word data with auto increment. */
#define SIFINH      		0x03  	/* IO Byte data with auto increment. */
#define SIFADR      		0x04  	/* SIF/DMA Address. */
#define SIFCMD      		0x06  	/* SIF Command. */
#define SIFSTS      		0x06  	/* SIF Status. */

/* "Extended" SIF (SRSX = 1) */
#define SIFACL      		0x08  	/* SIF Adapter Control Register. */
#define SIFADD      		0x0a 	/* SIF/DMA Address. -- 0x0a */
#define SIFADX      		0x0c     /* 0x0c */
#define DMALEN      		0x0e 	/* SIF DMA length. -- 0x0e */

/*
 * POS Registers.  Only for ISA Adapters.
 */
#define POSREG      		0x10 	/* Adapter Program Option Select (POS)
			 		 * Register: base IO address + 16 byte.
			 		 */
#define POSREG_2    		24L 	/* only for TR4/16+ adapter
			 		 * base IO address + 24 byte. -- 0x18
			 		 */

/* SIFCMD command codes (high-low) */
#define CMD_INTERRUPT_ADAPTER   0x8000  /* Cause internal adapter interrupt */
#define CMD_ADAPTER_RESET   	0x4000  /* Hardware reset of adapter */
#define CMD_SSB_CLEAR		0x2000  /* Acknowledge to adapter to
					 * system interrupts.
					 */
#define CMD_EXECUTE		0x1000	/* Execute SCB command */
#define CMD_SCB_REQUEST		0x0800  /* Request adapter to interrupt
					 * system when SCB is available for
					 * another command.
					 */
#define CMD_RX_CONTINUE		0x0400  /* Continue receive after odd pointer
					 * stop. (odd pointer receive method)
					 */
#define CMD_RX_VALID		0x0200  /* Now actual RPL is valid. */
#define CMD_TX_VALID		0x0100  /* Now actual TPL is valid. (valid
					 * bit receive/transmit method)
					 */
#define CMD_SYSTEM_IRQ		0x0080  /* Adapter-to-attached-system
					 * interrupt is reset.
					 */
#define CMD_CLEAR_SYSTEM_IRQ	0x0080	/* Clear SYSTEM_INTERRUPT bit.
					 * (write: 1=ignore, 0=reset)
					 */
#define EXEC_SOFT_RESET		0xFF00  /* adapter soft reset. (restart
					 * adapter after hardware reset)
					 */

/* ACL commands (high-low) */
#define ACL_SWHLDA		0x0800  /* Software hold acknowledge. */
#define ACL_SWDDIR		0x0400  /* Data transfer direction. */
#define ACL_SWHRQ		0x0200  /* Pseudo DMA operation. */
#define ACL_PSDMAEN		0x0100  /* Enable pseudo system DMA. */
#define ACL_ARESET		0x0080  /* Adapter hardware reset command.
					 * (held in reset condition as
					 * long as bit is set)
					 */
#define ACL_CPHALT		0x0040  /* Communication processor halt.
					 * (can only be set while ACL_ARESET
					 * bit is set; prevents adapter
					 * processor from executing code while
					 * downloading firmware)
					 */
#define ACL_BOOT		0x0020
#define ACL_SINTEN		0x0008  /* System interrupt enable/disable
					 * (1/0): can be written if ACL_ARESET
					 * is zero.
					 */
#define ACL_PEN                 0x0004

#define ACL_NSELOUT0            0x0002
#define ACL_NSELOUT1            0x0001	/* NSELOUTx have a card-specific
					 * meaning for setting ring speed.
					 */

#define PS_DMA_MASK		(ACL_SWHRQ | ACL_PSDMAEN)

/* SIFSTS register return codes (high-low) */
#define STS_SYSTEM_IRQ		0x0080	/* Adapter-to-attached-system
					 * interrupt is valid.
					 */
#define STS_INITIALIZE		0x0040  /* INITIALIZE status. (ready to
					 * initialize)
					 */
#define STS_TEST		0x0020  /* TEST status. (BUD not completed) */
#define STS_ERROR		0x0010  /* ERROR status. (unrecoverable
					 * HW error occurred)
					 */
#define STS_MASK		0x00F0  /* Mask interesting status bits. */
#define STS_ERROR_MASK		0x000F  /* Get Error Code by masking the
					 * interrupt code bits.
					 */
#define ADAPTER_INT_PTRS	0x0A00  /* Address offset of adapter internal
					 * pointers 01:0a00 (high-low) have to
					 * be read after init and before open.
					 */

/* Interrupt Codes (only MAC IRQs) */
#define STS_IRQ_ADAPTER_CHECK	0x0000	/* unrecoverable hardware or
					 * software error.
					 */
#define STS_IRQ_RING_STATUS	0x0004  /* SSB is updated with ring status. */
#define STS_IRQ_LLC_STATUS	0x0005	/* Not used in MAC-only microcode */
#define STS_IRQ_SCB_CLEAR	0x0006	/* SCB clear, following an
					 * SCB_REQUEST IRQ.
					 */
#define STS_IRQ_TIMER		0x0007	/* Not normally used in MAC ucode */
#define STS_IRQ_COMMAND_STATUS	0x0008	/* SSB is updated with command
					 * status.
					 */
#define STS_IRQ_RECEIVE_STATUS	0x000A	/* SSB is updated with receive
					 * status.
					 */
#define STS_IRQ_TRANSMIT_STATUS	0x000C	/* SSB is updated with transmit
                                         * status
					 */
#define STS_IRQ_RECEIVE_PENDING	0x000E	/* Not used in MAC-only microcode */
#define STS_IRQ_MASK		0x000F	/* = STS_ERROR_MASK. */

/* TRANSMIT_STATUS completion code: (SSB.Parm[0]) */
#define COMMAND_COMPLETE	0x0080	/* TRANSMIT command completed
                                         * (avoid this!) issue another transmit
					 * to send additional frames.
					 */
#define FRAME_COMPLETE		0x0040	/* Frame has been transmitted;
					 * INTERRUPT_FRAME bit was set in the
					 * CSTAT request; indication of possibly
					 * more than one frame transmissions!
					 * SSB.Parm[0-1]: 32 bit pointer to
					 * TPL of last frame.
					 */
#define LIST_ERROR		0x0020	/* Error in one of the TPLs that
					 * compose the frame; TRANSMIT
					 * terminated; Parm[1-2]: 32bit pointer
					 * to TPL which starts the error
					 * frame; error details in bits 8-13.
					 * (14?)
					 */
#define FRAME_SIZE_ERROR	0x8000	/* FRAME_SIZE does not equal the sum of
					 * the valid DATA_COUNT fields;
					 * FRAME_SIZE less than header plus
					 * information field. (15 bytes +
					 * routing field) Or if FRAME_SIZE
					 * was specified as zero in one list.
					 */
#define TX_THRESHOLD		0x4000	/* FRAME_SIZE greater than (BUFFER_SIZE
					 * - 9) * TX_BUF_MAX.
					 */
#define ODD_ADDRESS		0x2000	/* Odd forward pointer value is
					 * read on a list without END_FRAME
					 * indication.
					 */
#define FRAME_ERROR		0x1000	/* START_FRAME bit (not) anticipated,
					 * but (not) set.
					 */
#define ACCESS_PRIORITY_ERROR	0x0800	/* Access priority requested has not
					 * been allowed.
					 */
#define UNENABLED_MAC_FRAME	0x0400	/* MAC frame has source class of zero
					 * or MAC frame PCF ATTN field is
					 * greater than one.
					 */
#define ILLEGAL_FRAME_FORMAT	0x0200	/* Bit 0 or FC field was set to one. */

/*
 * Since we need to support some functions even if the adapter is in a
 * CLOSED state, we have a (pseudo-) command queue which holds commands
 * that are outstandig to be executed.
 *
 * Each time a command completes, an interrupt occurs and the next
 * command is executed. The command queue is actually a simple word with
 * a bit for each outstandig command. Therefore the commands will not be
 * executed in the order they have been queued.
 *
 * The following defines the command code bits and the command queue:
 */
#define OC_OPEN			0x0001	/* OPEN command */
#define OC_TRANSMIT		0x0002	/* TRANSMIT command */
#define OC_TRANSMIT_HALT	0x0004	/* TRANSMIT_HALT command */
#define OC_RECEIVE		0x0008	/* RECEIVE command */
#define OC_CLOSE		0x0010	/* CLOSE command */
#define OC_SET_GROUP_ADDR	0x0020	/* SET_GROUP_ADDR command */
#define OC_SET_FUNCT_ADDR	0x0040	/* SET_FUNCT_ADDR command */
#define OC_READ_ERROR_LOG	0x0080	/* READ_ERROR_LOG command */
#define OC_READ_ADAPTER		0x0100	/* READ_ADAPTER command */
#define OC_MODIFY_OPEN_PARMS	0x0400	/* MODIFY_OPEN_PARMS command */
#define OC_RESTORE_OPEN_PARMS	0x0800	/* RESTORE_OPEN_PARMS command */
#define OC_SET_FIRST_16_GROUP	0x1000	/* SET_FIRST_16_GROUP command */
#define OC_SET_BRIDGE_PARMS	0x2000	/* SET_BRIDGE_PARMS command */
#define OC_CONFIG_BRIDGE_PARMS	0x4000	/* CONFIG_BRIDGE_PARMS command */

#define OPEN			0x0300	/* C: open command. S: completion. */
#define TRANSMIT		0x0400	/* C: transmit command. S: completion
					 * status. (reject: COMMAND_REJECT if
					 * adapter not opened, TRANSMIT already
					 * issued or address passed in the SCB
					 * not word aligned)
					 */
#define TRANSMIT_HALT		0x0500	/* C: interrupt TX TPL chain; if no
					 * TRANSMIT command issued, the command
					 * is ignored (completion with TRANSMIT
					 * status (0x0400)!)
					 */
#define RECEIVE			0x0600	/* C: receive command. S: completion
					 * status. (reject: COMMAND_REJECT if
					 * adapter not opened, RECEIVE already
					 * issued or address passed in the SCB
					 * not word aligned)
					 */
#define CLOSE			0x0700	/* C: close adapter. S: completion.
					 * (COMMAND_REJECT if adapter not open)
					 */
#define SET_GROUP_ADDR		0x0800	/* C: alter adapter group address after
					 * OPEN. S: completion. (COMMAND_REJECT
					 * if adapter not open)
					 */
#define SET_FUNCT_ADDR		0x0900	/* C: alter adapter functional address
					 * after OPEN. S: completion.
					 * (COMMAND_REJECT if adapter not open)
					 */
#define READ_ERROR_LOG		0x0A00	/* C: read adapter error counters.
					 * S: completion. (command ignored
					 * if adapter not open!)
					 */
#define READ_ADAPTER		0x0B00	/* C: read data from adapter memory.
					 * (important: after init and before
					 * open!) S: completion. (ADAPTER_CHECK
					 * interrupt if undefined storage area
					 * read)
					 */
#define MODIFY_OPEN_PARMS	0x0D00	/* C: modify some adapter operational
					 * parameters. (bit correspondend to
					 * WRAP_INTERFACE is ignored)
					 * S: completion. (reject:
					 * COMMAND_REJECT)
					 */
#define RESTORE_OPEN_PARMS	0x0E00	/* C: modify some adapter operational
					 * parameters. (bit correspondend
					 * to WRAP_INTERFACE is ignored)
					 * S: completion. (reject:
					 * COMMAND_REJECT)
					 */
#define SET_FIRST_16_GROUP	0x0F00	/* C: alter the first two bytes in
					 * adapter group address.
					 * S: completion. (reject:
					 * COMMAND_REJECT)
					 */
#define SET_BRIDGE_PARMS	0x1000	/* C: values and conditions for the
					 * adapter hardware to use when frames
					 * are copied for forwarding.
					 * S: completion. (reject:
					 * COMMAND_REJECT)
					 */
#define CONFIG_BRIDGE_PARMS	0x1100	/* C: ..
					 * S: completion. (reject:
					 * COMMAND_REJECT)
					 */

#define SPEED_4			4
#define SPEED_16		16	/* Default transmission speed  */

/* Initialization Parameter Block (IPB); word alignment necessary! */
#define BURST_SIZE	0x0018	/* Default burst size */
#define BURST_MODE	0x9F00	/* Burst mode enable */
#define DMA_RETRIES	0x0505	/* Magic DMA retry number... */

#define CYCLE_TIME	3	/* Default AT-bus cycle time: 500 ns
				 * (later adapter version: fix  cycle time!)
				 */
#define LINE_SPEED_BIT	0x80

/* Macro definition for the wait function. */
#define ONE_SECOND_TICKS	1000000
#define HALF_SECOND		(ONE_SECOND_TICKS / 2)
#define ONE_SECOND		(ONE_SECOND_TICKS)
#define TWO_SECONDS		(ONE_SECOND_TICKS * 2)
#define THREE_SECONDS		(ONE_SECOND_TICKS * 3)
#define FOUR_SECONDS		(ONE_SECOND_TICKS * 4)
#define FIVE_SECONDS		(ONE_SECOND_TICKS * 5)

#define BUFFER_SIZE 		2048	/* Buffers on Adapter */

#pragma pack(1)
typedef struct {
	unsigned short Init_Options;	/* Initialize with burst mode;
					 * LLC disabled. (MAC only)
					 */

	/* Interrupt vectors the adapter places on attached system bus. */
	u_int8_t  CMD_Status_IV;    /* Interrupt vector: command status. */
	u_int8_t  TX_IV;	    /* Interrupt vector: transmit. */
	u_int8_t  RX_IV;	    /* Interrupt vector: receive. */
	u_int8_t  Ring_Status_IV;   /* Interrupt vector: ring status. */
	u_int8_t  SCB_Clear_IV;	    /* Interrupt vector: SCB clear. */
	u_int8_t  Adapter_CHK_IV;   /* Interrupt vector: adapter check. */

	u_int16_t RX_Burst_Size;    /* Max. number of transfer cycles. */
	u_int16_t TX_Burst_Size;    /* During DMA burst; even value! */
	u_int16_t DMA_Abort_Thrhld; /* Number of DMA retries. */

	u_int32_t SCB_Addr;   /* SCB address: even, word aligned, high-low */
	u_int32_t SSB_Addr;   /* SSB address: even, word aligned, high-low */
} IPB, *IPB_Ptr;
#pragma pack()

/*
 * OPEN Command Parameter List (OCPL) (can be reused, if the adapter has to
 * be reopened)
 */
#define BUFFER_SIZE	2048		/* Buffers on Adapter. */
#define TPL_SIZE	8+6*TX_FRAG_NUM /* Depending on fragments per TPL. */
#define RPL_SIZE	14		/* (with TI firmware v2.26 handling
					 * up to nine fragments possible)
					 */
#define TX_BUF_MIN	20		/* ??? (Stephan: calculation with */
#define TX_BUF_MAX	40		/* BUFFER_SIZE and MAX_FRAME_SIZE) ???
					 */
#define DISABLE_EARLY_TOKEN_RELEASE 	0x1000

/* OPEN Options (high-low) */
#define WRAP_INTERFACE		0x0080	/* Inserting omitted for test
					 * purposes; transmit data appears
					 * as receive data. (useful for
					 * testing; change: CLOSE necessary)
					 */
#define DISABLE_HARD_ERROR	0x0040	/* On HARD_ERROR & TRANSMIT_BEACON
					 * no RING.STATUS interrupt.
					 */
#define DISABLE_SOFT_ERROR	0x0020	/* On SOFT_ERROR, no RING.STATUS
					 * interrupt.
					 */
#define PASS_ADAPTER_MAC_FRAMES	0x0010	/* Passing unsupported MAC frames
					 * to system.
					 */
#define PASS_ATTENTION_FRAMES	0x0008	/* All changed attention MAC frames are
					 * passed to the system.
					 */
#define PAD_ROUTING_FIELD	0x0004	/* Routing field is padded to 18
					 * bytes.
					 */
#define FRAME_HOLD		0x0002	/*Adapter waits for entire frame before
					 * initiating DMA transfer; otherwise:
					 * DMA transfer initiation if internal
					 * buffer filled.
					 */
#define CONTENDER		0x0001	/* Adapter participates in the monitor
					 * contention process.
					 */
#define PASS_BEACON_MAC_FRAMES	0x8000	/* Adapter passes beacon MAC frames
					 * to the system.
					 */
#define EARLY_TOKEN_RELEASE 	0x1000	/* Only valid in 16 Mbps operation;
					 * 0 = ETR. (no effect in 4 Mbps
					 * operation)
					 */
#define COPY_ALL_MAC_FRAMES	0x0400	/* All MAC frames are copied to
					 * the system. (after OPEN: duplicate
					 * address test (DAT) MAC frame is
					 * first received frame copied to the
					 * system)
					 */
#define COPY_ALL_NON_MAC_FRAMES	0x0200	/* All non MAC frames are copied to
					 * the system.
					 */
#define PASS_FIRST_BUF_ONLY	0x0100	/* Passes only first internal buffer
					 * of each received frame; FrameSize
					 * of RPLs must contain internal
					 * BUFFER_SIZE bits for promiscous mode.
					 */
#define ENABLE_FULL_DUPLEX_SELECTION	0x2000
 					/* Enable the use of full-duplex
					 * settings with bits in byte 22 in
					 * ocpl. (new feature in firmware
					 * version 3.09)
					 */

/* Full-duplex settings */
#define OPEN_FULL_DUPLEX_OFF	0x0000
#define OPEN_FULL_DUPLEX_ON	0x00c0
#define OPEN_FULL_DUPLEX_AUTO	0x0080

#define PROD_ID_SIZE	18	/* Length of product ID. */

#define TX_FRAG_NUM	3	 /* Number of fragments used in one TPL. */
#define TX_MORE_FRAGMENTS 0x8000 /* Bit set in DataCount to indicate more
				  * fragments following.
				  */

/* XXX is there some better way to do this? */
#define ISA_MAX_ADDRESS 	0x00ffffff
#define PCI_MAX_ADDRESS		0xffffffff

#pragma pack(1)
typedef struct {
	u_int16_t OPENOptions;
	u_int8_t  NodeAddr[6];	/* Adapter node address; use ROM
				 * address
				 */
	u_int32_t GroupAddr;	/* Multicast: high order
				 * bytes = 0xC000
				 */
	u_int32_t FunctAddr;	/* High order bytes = 0xC000 */
	__be16 RxListSize;	/* RPL size: 0 (=26), 14, 20 or
				 * 26 bytes read by the adapter.
				 * (Depending on the number of
				 * fragments/list)
				 */
	__be16 TxListSize;	/* TPL size */
	__be16 BufSize;		/* Is automatically rounded up to the
				 * nearest nK boundary.
				 */
	u_int16_t FullDuplex;
	u_int16_t Reserved;
	u_int8_t  TXBufMin;	/* Number of adapter buffers reserved
				 * for transmission a minimum of 2
				 * buffers must be allocated.
				 */
	u_int8_t  TXBufMax;	/* Maximum number of adapter buffers
				 * for transmit; a minimum of 2 buffers
				 * must be available for receive.
				 * Default: 6
				 */
	u_int16_t ProdIDAddr[2];/* Pointer to product ID. */
} OPB, *OPB_Ptr;
#pragma pack()

/*
 * SCB: adapter commands enabled by the host system started by writing
 * CMD_INTERRUPT_ADAPTER | CMD_EXECUTE (|SCB_REQUEST) to the SIFCMD IO
 * register. (special case: | CMD_SYSTEM_IRQ for initialization)
 */
#pragma pack(1)
typedef struct {
	u_int16_t CMD;		/* Command code */
	u_int16_t Parm[2];	/* Pointer to Command Parameter Block */
} SCB;	/* System Command Block (32 bit physical address; big endian)*/
#pragma pack()

/*
 * SSB: adapter command return status can be evaluated after COMMAND_STATUS
 * adapter to system interrupt after reading SSB, the availability of the SSB
 * has to be told the adapter by writing CMD_INTERRUPT_ADAPTER | CMD_SSB_CLEAR
 * in the SIFCMD IO register.
 */
#pragma pack(1)
typedef struct {
	u_int16_t STS;		/* Status code */
	u_int16_t Parm[3];	/* Parameter or pointer to Status Parameter
				 * Block.
				 */
} SSB;	/* System Status Block (big endian - physical address)  */
#pragma pack()

typedef struct {
	unsigned short BurnedInAddrPtr;	/* Pointer to adapter burned in
					 * address. (BIA)
					 */
	unsigned short SoftwareLevelPtr;/* Pointer to software level data. */
	unsigned short AdapterAddrPtr;	/* Pointer to adapter addresses. */
	unsigned short AdapterParmsPtr;	/* Pointer to adapter parameters. */
	unsigned short MACBufferPtr;	/* Pointer to MAC buffer. (internal) */
	unsigned short LLCCountersPtr;	/* Pointer to LLC counters.  */
	unsigned short SpeedFlagPtr;	/* Pointer to data rate flag.
					 * (4/16 Mbps)
					 */
	unsigned short AdapterRAMPtr;	/* Pointer to adapter RAM found. (KB) */
} INTPTRS;	/* Adapter internal pointers */

#pragma pack(1)
typedef struct {
	u_int8_t  Line_Error;		/* Line error: code violation in
					 * frame or in a token, or FCS error.
					 */
	u_int8_t  Internal_Error;	/* IBM specific. (Reserved_1) */
	u_int8_t  Burst_Error;
	u_int8_t  ARI_FCI_Error;	/* ARI/FCI bit zero in AMP or
					 * SMP MAC frame.
					 */
	u_int8_t  AbortDelimeters;	/* IBM specific. (Reserved_2) */
	u_int8_t  Reserved_3;
	u_int8_t  Lost_Frame_Error;	/* Receive of end of transmitted
					 * frame failed.
					 */
	u_int8_t  Rx_Congest_Error;	/* Adapter in repeat mode has not
					 * enough buffer space to copy incoming
					 * frame.
					 */
	u_int8_t  Frame_Copied_Error;	/* ARI bit not zero in frame
					 * addressed to adapter.
					 */
	u_int8_t  Frequency_Error;	/* IBM specific. (Reserved_4) */
	u_int8_t  Token_Error;		/* (active only in monitor station) */
	u_int8_t  Reserved_5;
	u_int8_t  DMA_Bus_Error;	/* DMA bus errors not exceeding the
					 * abort thresholds.
					 */
	u_int8_t  DMA_Parity_Error;	/* DMA parity errors not exceeding
					 * the abort thresholds.
					 */
} ERRORTAB;	/* Adapter error counters */
#pragma pack()

/*--------------------- Send and Receive definitions -------------------*/
#pragma pack(1)
typedef struct {
	__be16 DataCount;	/* Value 0, even and odd values are
				 * permitted; value is unaltered most
				 * significant bit set: following
				 * fragments last fragment: most
				 * significant bit is not evaluated.
				 * (???)
				 */
	__be32 DataAddr;	/* Pointer to frame data fragment;
				 * even or odd.
				 */
} Fragment;
#pragma pack()

#define MAX_FRAG_NUMBERS    9	/* Maximal number of fragments possible to use
				 * in one RPL/TPL. (depending on TI firmware
				 * version)
				 */

/*
 * AC (1), FC (1), Dst (6), Src (6), RIF (18), Data (4472) = 4504
 * The packet size can be one of the follows: 548, 1502, 2084, 4504, 8176,
 * 11439, 17832. Refer to TMS380 Second Generation Token Ring User's Guide
 * Page 2-27.
 */
#define HEADER_SIZE		(1 + 1 + 6 + 6)
#define SRC_SIZE		18
#define MIN_DATA_SIZE		516
#define DEFAULT_DATA_SIZE	4472
#define MAX_DATA_SIZE		17800

#define DEFAULT_PACKET_SIZE (HEADER_SIZE + SRC_SIZE + DEFAULT_DATA_SIZE)
#define MIN_PACKET_SIZE     (HEADER_SIZE + SRC_SIZE + MIN_DATA_SIZE)
#define MAX_PACKET_SIZE     (HEADER_SIZE + SRC_SIZE + MAX_DATA_SIZE)

/*
 * Macros to deal with the frame status field.
 */
#define AC_NOT_RECOGNIZED	0x00
#define GROUP_BIT		0x80
#define GET_TRANSMIT_STATUS_HIGH_BYTE(Ts) ((unsigned char)((Ts) >> 8))
#define GET_FRAME_STATUS_HIGH_AC(Fs)	  ((unsigned char)(((Fs) & 0xC0) >> 6))
#define GET_FRAME_STATUS_LOW_AC(Fs)       ((unsigned char)(((Fs) & 0x0C) >> 2))
#define DIRECTED_FRAME(Context)           (!((Context)->MData[2] & GROUP_BIT))

/*--------------------- Send Functions ---------------------------------*/
/* define TX_CSTAT _REQUEST (R) and _COMPLETE (C) values (high-low) */

#define TX_VALID		0x0080	/* R: set via TRANSMIT.VALID interrupt.
					 * C: always reset to zero!
					 */
#define TX_FRAME_COMPLETE	0x0040	/* R: must be reset to zero.
					 * C: set to one.
					 */
#define TX_START_FRAME		0x0020  /* R: start of a frame: 1
					 * C: unchanged.
					 */
#define TX_END_FRAME		0x0010  /* R: end of a frame: 1
					 * C: unchanged.
					 */
#define TX_FRAME_IRQ		0x0008  /* R: request interrupt generation
					 * after transmission.
					 * C: unchanged.
					 */
#define TX_ERROR		0x0004  /* R: reserved.
					 * C: set to one if Error occurred.
					 */
#define TX_INTERFRAME_WAIT	0x0004
#define TX_PASS_CRC		0x0002  /* R: set if CRC value is already
					 * calculated. (valid only in
					 * FRAME_START TPL)
					 * C: unchanged.
					 */
#define TX_PASS_SRC_ADDR	0x0001  /* R: adapter uses explicit frame
					 * source address and does not overwrite
					 * with the adapter node address.
					 * (valid only in FRAME_START TPL)
					 *
					 * C: unchanged.
					 */
#define TX_STRIP_FS		0xFF00  /* R: reserved.
					 * C: if no Transmission Error,
					 * field contains copy of FS byte after
					 * stripping of frame.
					 */

/*
 * Structure of Transmit Parameter Lists (TPLs) (only one frame every TPL,
 * but possibly multiple TPLs for one frame) the length of the TPLs has to be
 * initialized in the OPL. (OPEN parameter list)
 */
#define TPL_NUM		3	/* Number of Transmit Parameter Lists.
				 * !! MUST BE >= 3 !!
				 */

#pragma pack(1)
typedef struct s_TPL TPL;

struct s_TPL {	/* Transmit Parameter List (align on even word boundaries) */
	__be32 NextTPLAddr;		/* Pointer to next TPL in chain; if
					 * pointer is odd: this is the last
					 * TPL. Pointing to itself can cause
					 * problems!
					 */
	volatile u_int16_t Status;	/* Initialized by the adapter:
					 * CSTAT_REQUEST important: update least
					 * significant bit first! Set by the
					 * adapter: CSTAT_COMPLETE status.
					 */
	__be16 FrameSize;		/* Number of bytes to be transmitted
					 * as a frame including AC/FC,
					 * Destination, Source, Routing field
					 * not including CRC, FS, End Delimiter
					 * (valid only if START_FRAME bit in
					 * CSTAT nonzero) must not be zero in
					 * any list; maximum value: (BUFFER_SIZE
					 * - 8) * TX_BUF_MAX sum of DataCount
					 * values in FragmentList must equal
					 * Frame_Size value in START_FRAME TPL!
					 * frame data fragment list.
					 */

	/* TPL/RPL size in OPEN parameter list depending on maximal
	 * numbers of fragments used in one parameter list.
	 */
	Fragment FragList[TX_FRAG_NUM];	/* Maximum: nine frame fragments in one
					 * TPL actual version of firmware: 9
					 * fragments possible.
					 */
#pragma pack()

	/* Special proprietary data and precalculations */

	TPL *NextTPLPtr;		/* Pointer to next TPL in chain. */
	unsigned char *MData;
	struct sk_buff *Skb;
	unsigned char TPLIndex;
	volatile unsigned char BusyFlag;/* Flag: TPL busy? */
	dma_addr_t DMABuff;		/* DMA IO bus address from dma_map */
};

/* ---------------------Receive Functions-------------------------------*
 * define RECEIVE_CSTAT_REQUEST (R) and RECEIVE_CSTAT_COMPLETE (C) values.
 * (high-low)
 */
#define RX_VALID		0x0080	/* R: set; tell adapter with
					 * RECEIVE.VALID interrupt.
					 * C: reset to zero.
					 */
#define RX_FRAME_COMPLETE	0x0040  /* R: must be reset to zero,
					 * C: set to one.
					 */
#define RX_START_FRAME		0x0020  /* R: must be reset to zero.
					 * C: set to one on the list.
					 */
#define RX_END_FRAME		0x0010  /* R: must be reset to zero.
					 * C: set to one on the list
					 * that ends the frame.
					 */
#define RX_FRAME_IRQ		0x0008  /* R: request interrupt generation
					 * after receive.
					 * C: unchanged.
					 */
#define RX_INTERFRAME_WAIT	0x0004  /* R: after receiving a frame:
					 * interrupt and wait for a
					 * RECEIVE.CONTINUE.
					 * C: unchanged.
					 */
#define RX_PASS_CRC		0x0002  /* R: if set, the adapter includes
					 * the CRC in data passed. (last four
					 * bytes; valid only if FRAME_START is
					 * set)
					 * C: set, if CRC is included in
					 * received data.
					 */
#define RX_PASS_SRC_ADDR	0x0001  /* R: adapter uses explicit frame
					 * source address and does not
					 * overwrite with the adapter node
					 * address. (valid only if FRAME_START
					 * is set)
					 * C: unchanged.
					 */
#define RX_RECEIVE_FS		0xFC00  /* R: reserved; must be reset to zero.
					 * C: on lists with START_FRAME, field
					 * contains frame status field from
					 * received frame; otherwise cleared.
					 */
#define RX_ADDR_MATCH		0x0300  /* R: reserved; must be reset to zero.
					 * C: address match code mask.
					 */
#define RX_STATUS_MASK		0x00FF  /* Mask for receive status bits. */

#define RX_INTERN_ADDR_MATCH    0x0100  /* C: internally address match. */
#define RX_EXTERN_ADDR_MATCH    0x0200  /* C: externally matched via
					 * XMATCH/XFAIL interface.
					 */
#define RX_INTEXT_ADDR_MATCH    0x0300  /* C: internally and externally
					 * matched.
					 */
#define RX_READY (RX_VALID | RX_FRAME_IRQ) /* Ready for receive. */

/* Constants for Command Status Interrupt.
 * COMMAND_REJECT status field bit functions (SSB.Parm[0])
 */
#define ILLEGAL_COMMAND		0x0080	/* Set if an unknown command
					 * is issued to the adapter
					 */
#define ADDRESS_ERROR		0x0040  /* Set if any address field in
					 * the SCB is odd. (not word aligned)
					 */
#define ADAPTER_OPEN		0x0020  /* Command issued illegal with
					 * open adapter.
					 */
#define ADAPTER_CLOSE		0x0010  /* Command issued illegal with
					 * closed adapter.
					 */
#define SAME_COMMAND		0x0008  /* Command issued with same command
					 * already executing.
					 */

/* OPEN_COMPLETION values (SSB.Parm[0], MSB) */
#define NODE_ADDR_ERROR		0x0040  /* Wrong address or BIA read
					 * zero address.
					 */
#define LIST_SIZE_ERROR		0x0020  /* If List_Size value not in 0,
					 * 14, 20, 26.
					 */
#define BUF_SIZE_ERROR		0x0010  /* Not enough available memory for
					 * two buffers.
					 */
#define TX_BUF_COUNT_ERROR	0x0004  /* Remaining receive buffers less than
					 * two.
					 */
#define OPEN_ERROR		0x0002	/* Error during ring insertion; more
					 * information in bits 8-15.
					 */

/* Standard return codes */
#define GOOD_COMPLETION		0x0080  /* =OPEN_SUCCESSFULL */
#define INVALID_OPEN_OPTION	0x0001  /* OPEN options are not supported by
					 * the adapter.
					 */

/* OPEN phases; details of OPEN_ERROR (SSB.Parm[0], LSB)            */
#define OPEN_PHASES_MASK            0xF000  /* Check only the bits 8-11. */
#define LOBE_MEDIA_TEST             0x1000
#define PHYSICAL_INSERTION          0x2000
#define ADDRESS_VERIFICATION        0x3000
#define PARTICIPATION_IN_RING_POLL  0x4000
#define REQUEST_INITIALISATION      0x5000
#define FULLDUPLEX_CHECK            0x6000

/* OPEN error codes; details of OPEN_ERROR (SSB.Parm[0], LSB) */
#define OPEN_ERROR_CODES_MASK	0x0F00  /* Check only the bits 12-15. */
#define OPEN_FUNCTION_FAILURE   0x0100  /* Unable to transmit to itself or
					 * frames received before insertion.
					 */
#define OPEN_SIGNAL_LOSS	0x0200	/* Signal loss condition detected at
					 * receiver.
					 */
#define OPEN_TIMEOUT		0x0500	/* Insertion timer expired before
					 * logical insertion.
					 */
#define OPEN_RING_FAILURE	0x0600	/* Unable to receive own ring purge
					 * MAC frames.
					 */
#define OPEN_RING_BEACONING	0x0700	/* Beacon MAC frame received after
					 * ring insertion.
					 */
#define OPEN_DUPLICATE_NODEADDR	0x0800  /* Other station in ring found
					 * with the same address.
					 */
#define OPEN_REQUEST_INIT	0x0900	/* RPS present but does not respond. */
#define OPEN_REMOVE_RECEIVED    0x0A00  /* Adapter received a remove adapter
					 * MAC frame.
					 */
#define OPEN_FULLDUPLEX_SET	0x0D00	/* Got this with full duplex on when
					 * trying to connect to a normal ring.
					 */

/* SET_BRIDGE_PARMS return codes: */
#define BRIDGE_INVALID_MAX_LEN  0x4000  /* MAX_ROUTING_FIELD_LENGTH odd,
					 * less than 6 or > 30.
					 */
#define BRIDGE_INVALID_SRC_RING 0x2000  /* SOURCE_RING number zero, too large
					 * or = TARGET_RING.
					 */
#define BRIDGE_INVALID_TRG_RING 0x1000  /* TARGET_RING number zero, too large
					 * or = SOURCE_RING.
					 */
#define BRIDGE_INVALID_BRDGE_NO 0x0800  /* BRIDGE_NUMBER too large. */
#define BRIDGE_INVALID_OPTIONS  0x0400  /* Invalid bridge options. */
#define BRIDGE_DIAGS_FAILED     0x0200  /* Diagnostics of TMS380SRA failed. */
#define BRIDGE_NO_SRA           0x0100  /* The TMS380SRA does not exist in HW
					 * configuration.
					 */

/*
 * Bring Up Diagnostics error codes.
 */
#define BUD_INITIAL_ERROR       0x0
#define BUD_CHECKSUM_ERROR      0x1
#define BUD_ADAPTER_RAM_ERROR   0x2
#define BUD_INSTRUCTION_ERROR   0x3
#define BUD_CONTEXT_ERROR       0x4
#define BUD_PROTOCOL_ERROR      0x5
#define BUD_INTERFACE_ERROR	0x6

/* BUD constants */
#define BUD_MAX_RETRIES         3
#define BUD_MAX_LOOPCNT         6
#define BUD_TIMEOUT             3000

/* Initialization constants */
#define INIT_MAX_RETRIES        3	/* Maximum three retries. */
#define INIT_MAX_LOOPCNT        22      /* Maximum loop counts. */

/* RING STATUS field values (high/low) */
#define SIGNAL_LOSS             0x0080  /* Loss of signal on the ring
					 * detected.
					 */
#define HARD_ERROR              0x0040  /* Transmitting or receiving beacon
					 * frames.
					 */
#define SOFT_ERROR              0x0020  /* Report error MAC frame
					 * transmitted.
					 */
#define TRANSMIT_BEACON         0x0010  /* Transmitting beacon frames on the
					 * ring.
					 */
#define LOBE_WIRE_FAULT         0x0008  /* Open or short circuit in the
					 * cable to concentrator; adapter
					 * closed.
					 */
#define AUTO_REMOVAL_ERROR      0x0004  /* Lobe wrap test failed, deinserted;
					 * adapter closed.
					 */
#define REMOVE_RECEIVED         0x0001  /* Received a remove ring station MAC
					 * MAC frame request; adapter closed.
					 */
#define COUNTER_OVERFLOW        0x8000  /* Overflow of one of the adapters
					 * error counters; READ.ERROR.LOG.
					 */
#define SINGLE_STATION          0x4000  /* Adapter is the only station on the
					 * ring.
					 */
#define RING_RECOVERY           0x2000  /* Claim token MAC frames on the ring;
					 * reset after ring purge frame.
					 */

#define ADAPTER_CLOSED (LOBE_WIRE_FAULT | AUTO_REMOVAL_ERROR |\
                        REMOVE_RECEIVED)

/* Adapter_check_block.Status field bit assignments: */
#define DIO_PARITY              0x8000  /* Adapter detects bad parity
					 * through direct I/O access.
					 */
#define DMA_READ_ABORT          0x4000  /* Aborting DMA read operation
					 * from system Parm[0]: 0=timeout,
					 * 1=parity error, 2=bus error;
					 * Parm[1]: 32 bit pointer to host
					 * system address at failure.
					 */
#define DMA_WRITE_ABORT         0x2000  /* Aborting DMA write operation
					 * to system. (parameters analogous to
					 * DMA_READ_ABORT)
					 */
#define ILLEGAL_OP_CODE         0x1000  /* Illegal operation code in the
					 * the adapters firmware Parm[0]-2:
					 * communications processor registers
					 * R13-R15.
					 */
#define PARITY_ERRORS           0x0800  /* Adapter detects internal bus
					 * parity error.
					 */
#define RAM_DATA_ERROR          0x0080  /* Valid only during RAM testing;
					 * RAM data error Parm[0-1]: 32 bit
					 * pointer to RAM location.
					 */
#define RAM_PARITY_ERROR        0x0040  /* Valid only during RAM testing;
					 * RAM parity error Parm[0-1]: 32 bit
					 * pointer to RAM location.
					 */
#define RING_UNDERRUN           0x0020  /* Internal DMA underrun when
					 * transmitting onto ring.
					 */
#define INVALID_IRQ             0x0008  /* Unrecognized interrupt generated
					 * internal to adapter Parm[0-2]:
					 * adapter register R13-R15.
					 */
#define INVALID_ERROR_IRQ       0x0004  /* Unrecognized error interrupt
					 * generated Parm[0-2]: adapter register
					 * R13-R15.
					 */
#define INVALID_XOP             0x0002  /* Unrecognized XOP request in
					 * communication processor Parm[0-2]:
					 * adapter register R13-R15.
					 */
#define CHECKADDR               0x05E0  /* Adapter check status information
					 * address offset.
					 */
#define ROM_PAGE_0              0x0000  /* Adapter ROM page 0. */

/*
 * RECEIVE.STATUS interrupt result SSB values: (high-low)
 * (RECEIVE_COMPLETE field bit definitions in SSB.Parm[0])
 */
#define RX_COMPLETE             0x0080  /* SSB.Parm[0]; SSB.Parm[1]: 32
					 * bit pointer to last RPL.
					 */
#define RX_SUSPENDED            0x0040  /* SSB.Parm[0]; SSB.Parm[1]: 32
					 * bit pointer to RPL with odd
					 * forward pointer.
					 */

/* Valid receive CSTAT: */
#define RX_FRAME_CONTROL_BITS (RX_VALID | RX_START_FRAME | RX_END_FRAME | \
			       RX_FRAME_COMPLETE)
#define VALID_SINGLE_BUFFER_FRAME (RX_START_FRAME | RX_END_FRAME | \
				   RX_FRAME_COMPLETE)

typedef enum SKB_STAT SKB_STAT;
enum SKB_STAT {
	SKB_UNAVAILABLE,
	SKB_DMA_DIRECT,
	SKB_DATA_COPY
};

/* Receive Parameter List (RPL) The length of the RPLs has to be initialized
 * in the OPL. (OPEN parameter list)
 */
#define RPL_NUM		3

#define RX_FRAG_NUM     1	/* Maximal number of used fragments in one RPL.
				 * (up to firmware v2.24: 3, now: up to 9)
				 */

#pragma pack(1)
typedef struct s_RPL RPL;
struct s_RPL {	/* Receive Parameter List */
	__be32 NextRPLAddr;		/* Pointer to next RPL in chain
					 * (normalized = physical 32 bit
					 * address) if pointer is odd: this
					 * is last RPL. Pointing to itself can
					 * cause problems!
					 */
	volatile u_int16_t Status;	/* Set by creation of Receive Parameter
					 * List RECEIVE_CSTAT_COMPLETE set by
					 * adapter in lists that start or end
					 * a frame.
					 */
	volatile __be16 FrameSize;	 /* Number of bytes received as a
					 * frame including AC/FC, Destination,
					 * Source, Routing field not including
					 * CRC, FS (Frame Status), End Delimiter
					 * (valid only if START_FRAME bit in
					 * CSTAT nonzero) must not be zero in
					 * any list; maximum value: (BUFFER_SIZE
					 * - 8) * TX_BUF_MAX sum of DataCount
					 * values in FragmentList must equal
					 * Frame_Size value in START_FRAME TPL!
					 * frame data fragment list
					 */

	/* TPL/RPL size in OPEN parameter list depending on maximal numbers
	 * of fragments used in one parameter list.
	 */
	Fragment FragList[RX_FRAG_NUM];	/* Maximum: nine frame fragments in
					 * one TPL. Actual version of firmware:
					 * 9 fragments possible.
					 */
#pragma pack()

	/* Special proprietary data and precalculations. */
	RPL *NextRPLPtr;	/* Logical pointer to next RPL in chain. */
	unsigned char *MData;
	struct sk_buff *Skb;
	SKB_STAT SkbStat;
	int RPLIndex;
	dma_addr_t DMABuff;		/* DMA IO bus address from dma_map */
};

/* Information that need to be kept for each board. */
typedef struct net_local {
#pragma pack(1)
	IPB ipb;	/* Initialization Parameter Block. */
	SCB scb;	/* System Command Block: system to adapter
			 * communication.
			 */
	SSB ssb;	/* System Status Block: adapter to system
			 * communication.
			 */
	OPB ocpl;	/* Open Options Parameter Block. */

	ERRORTAB errorlogtable;	/* Adapter statistic error counters.
				 * (read from adapter memory)
				 */
	unsigned char ProductID[PROD_ID_SIZE + 1]; /* Product ID */
#pragma pack()

	TPL Tpl[TPL_NUM];
	TPL *TplFree;
	TPL *TplBusy;
	unsigned char LocalTxBuffers[TPL_NUM][DEFAULT_PACKET_SIZE];

	RPL Rpl[RPL_NUM];
	RPL *RplHead;
	RPL *RplTail;
	unsigned char LocalRxBuffers[RPL_NUM][DEFAULT_PACKET_SIZE];

	struct device *pdev;
	int DataRate;
	unsigned char ScbInUse;
	unsigned short CMDqueue;

	unsigned long AdapterOpenFlag:1;
	unsigned long AdapterVirtOpenFlag:1;
	unsigned long OpenCommandIssued:1;
	unsigned long TransmitCommandActive:1;
	unsigned long TransmitHaltScheduled:1;
	unsigned long HaltInProgress:1;
	unsigned long LobeWireFaultLogged:1;
	unsigned long ReOpenInProgress:1;
	unsigned long Sleeping:1;

	unsigned long LastOpenStatus;
	unsigned short CurrentRingStatus;
	unsigned long MaxPacketSize;

	unsigned long StartTime;
	unsigned long LastSendTime;

	struct tr_statistics MacStat;	/* MAC statistics structure */

	unsigned long dmalimit; /* the max DMA address (ie, ISA) */
	dma_addr_t    dmabuffer; /* the DMA bus address corresponding to
				    priv. Might be different from virt_to_bus()
				    for architectures with IO MMU (Alpha) */

	struct timer_list timer;

	wait_queue_head_t  wait_for_tok_int;

	INTPTRS intptrs;	/* Internal adapter pointer. Must be read
				 * before OPEN command.
				 */
	unsigned short (*setnselout)(struct net_device *);
	unsigned short (*sifreadb)(struct net_device *, unsigned short);
	void (*sifwriteb)(struct net_device *, unsigned short, unsigned short);
	unsigned short (*sifreadw)(struct net_device *, unsigned short);
	void (*sifwritew)(struct net_device *, unsigned short, unsigned short);

	spinlock_t lock;                /* SMP protection */
	void *tmspriv;
} NET_LOCAL;

#endif	/* __KERNEL__ */
#endif	/* __LINUX_TMS380TR_H */


text:
/**
  * @author ynupc
  *         Created on 2016/01/26
  */
object Day3Main {
  def main(args: Array[String]): Unit = {
    val executionTime: ExecutionTime = new ExecutionTime(1000000)
    val str: String = "𠮷野家すもももももももものうち祇園精舎の鐘の声、諸行無常の響きあり。沙羅双樹の花の色、盛者必衰の理をあらはす。おごれる人も久しからず。ただ春の夜の夢のごとし。たけき者も遂にはほろびぬ、ひとへに風の前の塵に同じ。 \n\n遠くの異朝をとぶらへば、秦の趙高、漢の王莽、梁の朱忌、唐の禄山、これらは皆、旧主先皇の政にも従はず、楽しみを極め、諫めをも思ひ入れず、天下の乱れんことを悟らずして、民間の愁ふるところを知らざつしかば、久しからずして、亡じにし者どもなり。 \n\n近く本朝をうかがふに、承平の将門、天慶の純友、康和の義親、平治の信頼、これらはおごれる心もたけきことも、皆とりどりにこそありしかども、間近くは六波羅の入道前太政大臣平朝臣清盛公と申しし人のありさま、伝え承るこそ、心も詞も及ばれね。𠮷"
    val length: Int = str.length
    println("codePointAt: Character vs String (1st round)")
    val charArray: Array[Char] = str.toCharArray
    println("Character1")
    executionTime.printlnAverageExecutionTime(Character.codePointAt(charArray, 0))
    executionTime.printlnAverageExecutionTime(Character.codePointAt(charArray, 0))
    executionTime.printlnAverageExecutionTime(Character.codePointAt(charArray, 0))
    println("Character2")
    executionTime.printlnAverageExecutionTime(Character.codePointAt(str, 0))
    executionTime.printlnAverageExecutionTime(Character.codePointAt(str, 0))
    executionTime.printlnAverageExecutionTime(Character.codePointAt(str, 0))
    println("String")
    executionTime.printlnAverageExecutionTime(str.codePointAt(0))
    executionTime.printlnAverageExecutionTime(str.codePointAt(0))
    executionTime.printlnAverageExecutionTime(str.codePointAt(0))
    println()
    println("codePointBefore: Character vs String (1st round)")
    println("Character1")
    executionTime.printlnAverageExecutionTime(Character.codePointBefore(charArray, length))
    executionTime.printlnAverageExecutionTime(Character.codePointBefore(charArray, length))
    executionTime.printlnAverageExecutionTime(Character.codePointBefore(charArray, length))
    println("Character2")
    executionTime.printlnAverageExecutionTime(Character.codePointBefore(str, length))
    executionTime.printlnAverageExecutionTime(Character.codePointBefore(str, length))
    executionTime.printlnAverageExecutionTime(Character.codePointBefore(str, length))
    println("String")
    executionTime.printlnAverageExecutionTime(str.codePointBefore(length))
    executionTime.printlnAverageExecutionTime(str.codePointBefore(length))
    executionTime.printlnAverageExecutionTime(str.codePointBefore(length))
    println()
    println("codePointAt: Character vs String (2nd round)")
    println("Character1")
    executionTime.printlnAverageExecutionTime(Character.codePointAt(str.toCharArray, 0))
    executionTime.printlnAverageExecutionTime(Character.codePointAt(str.toCharArray, 0))
    executionTime.printlnAverageExecutionTime(Character.codePointAt(str.toCharArray, 0))
    println("Character2")
    executionTime.printlnAverageExecutionTime(Character.codePointAt(str, 0))
    executionTime.printlnAverageExecutionTime(Character.codePointAt(str, 0))
    executionTime.printlnAverageExecutionTime(Character.codePointAt(str, 0))
    println("String")
    executionTime.printlnAverageExecutionTime(str.codePointAt(0))
    executionTime.printlnAverageExecutionTime(str.codePointAt(0))
    executionTime.printlnAverageExecutionTime(str.codePointAt(0))
    println()
    println("codePointBefore: Character vs String (2nd round)")
    println("Character1")
    executionTime.printlnAverageExecutionTime(Character.codePointBefore(str.toCharArray, length))
    executionTime.printlnAverageExecutionTime(Character.codePointBefore(str.toCharArray, length))
    executionTime.printlnAverageExecutionTime(Character.codePointBefore(str.toCharArray, length))
    println("Character2")
    executionTime.printlnAverageExecutionTime(Character.codePointBefore(str, length))
    executionTime.printlnAverageExecutionTime(Character.codePointBefore(str, length))
    executionTime.printlnAverageExecutionTime(Character.codePointBefore(str, length))
    println("String")
    executionTime.printlnAverageExecutionTime(str.codePointBefore(length))
    executionTime.printlnAverageExecutionTime(str.codePointBefore(length))
    executionTime.printlnAverageExecutionTime(str.codePointBefore(length))
    println()
    println("codePointAt: Character vs String (3rd round)")
    val n: Int = 20
    println("Character1")
    executionTime.printlnAverageExecutionTime(testCharacter1CodePointAt(str, n))
    executionTime.printlnAverageExecutionTime(testCharacter1CodePointAt(str, n))
    executionTime.printlnAverageExecutionTime(testCharacter1CodePointAt(str, n))
    println("Character2")
    executionTime.printlnAverageExecutionTime(testCharacter2CodePointAt(str, n))
    executionTime.printlnAverageExecutionTime(testCharacter2CodePointAt(str, n))
    executionTime.printlnAverageExecutionTime(testCharacter2CodePointAt(str, n))
    println("String")
    executionTime.printlnAverageExecutionTime(testStringCodePointAt(str, n))
    executionTime.printlnAverageExecutionTime(testStringCodePointAt(str, n))
    executionTime.printlnAverageExecutionTime(testStringCodePointAt(str, n))
    println()
    println("codePointBefore: Character vs String (3rd round)")
    println("Character1")
    executionTime.printlnAverageExecutionTime(testCharacter1CodePointBefore(str, n, length))
    executionTime.printlnAverageExecutionTime(testCharacter1CodePointBefore(str, n, length))
    executionTime.printlnAverageExecutionTime(testCharacter1CodePointBefore(str, n, length))
    println("Character2")
    executionTime.printlnAverageExecutionTime(testCharacter2CodePointBefore(str, n, length))
    executionTime.printlnAverageExecutionTime(testCharacter2CodePointBefore(str, n, length))
    executionTime.printlnAverageExecutionTime(testCharacter2CodePointBefore(str, n, length))
    println("String")
    executionTime.printlnAverageExecutionTime(testStringCodePointBefore(str, n, length))
    executionTime.printlnAverageExecutionTime(testStringCodePointBefore(str, n, length))
    executionTime.printlnAverageExecutionTime(testStringCodePointBefore(str, n, length))
    println()
    println("length: String.length vs String.chars.count")
    println("String.length")
    executionTime.printlnAverageExecutionTime(str.length)
    executionTime.printlnAverageExecutionTime(str.length)
    executionTime.printlnAverageExecutionTime(str.length)
    println("String.chars.count")
    executionTime.printlnAverageExecutionTime(str.chars.count)
    executionTime.printlnAverageExecutionTime(str.chars.count)
    executionTime.printlnAverageExecutionTime(str.chars.count)
    println()
    println("codePointCount: String.codePointCount vs String.codePoints.count")
    println("String.codePointCount")
    executionTime.printlnAverageExecutionTime(str.codePointCount(0, str.length))
    executionTime.printlnAverageExecutionTime(str.codePointCount(0, str.length))
    executionTime.printlnAverageExecutionTime(str.codePointCount(0, str.length))
    println("String.codePoints.count")
    executionTime.printlnAverageExecutionTime(str.codePoints.count)
    executionTime.printlnAverageExecutionTime(str.codePoints.count)
    executionTime.printlnAverageExecutionTime(str.codePoints.count)
    println()
    println("offsetByCodePoints: Character.offsetByCodePoints vs String.offsetByCodePoints")
    println("Character.offsetByCodePoints")
    executionTime.printlnAverageExecutionTime(Character.offsetByCodePoints(str, 0, 1))
    executionTime.printlnAverageExecutionTime(Character.offsetByCodePoints(str, 0, 1))
    executionTime.printlnAverageExecutionTime(Character.offsetByCodePoints(str, 0, 1))
    println("String.offsetByCodePoints")
    executionTime.printlnAverageExecutionTime(str.offsetByCodePoints(0, 1))
    executionTime.printlnAverageExecutionTime(str.offsetByCodePoints(0, 1))
    executionTime.printlnAverageExecutionTime(str.offsetByCodePoints(0, 1))
    println()
    println("codePoints: String.codePoints vs toCodePoints")
    println("String.codePoints")
    executionTime.printlnAverageExecutionTime(str.codePoints.toArray)
    executionTime.printlnAverageExecutionTime(str.codePoints.toArray)
    executionTime.printlnAverageExecutionTime(str.codePoints.toArray)
    println("toCodePoints")
    executionTime.printlnAverageExecutionTime(toCodePoints(str))
    executionTime.printlnAverageExecutionTime(toCodePoints(str))
    executionTime.printlnAverageExecutionTime(toCodePoints(str))
  }

  private def testCharacter1CodePointAt(str: String, n: Int): Unit = {
    val characterArray: Array[Char] = str.toCharArray
    Iterator.range(0, n).foreach {
      i =>
        Character.codePointAt(characterArray, 0)
    }
  }

  private def testCharacter2CodePointAt(str: String, n: Int): Unit = {
    Iterator.range(0, n).foreach {
      i =>
        Character.codePointAt(str, 0)
    }
  }

  private def testStringCodePointAt(str: String, n: Int): Unit = {
    Iterator.range(0, n).foreach {
      i =>
        str.codePointAt(0)
    }
  }

  private def testCharacter1CodePointBefore(str: String, n : Int, length: Int): Unit = {
    val characterArray: Array[Char] = str.toCharArray
    Iterator.range(0, n).foreach {
      i =>
        Character.codePointBefore(characterArray, length)
    }
  }

  private def testCharacter2CodePointBefore(str: String, n : Int, length: Int): Unit = {
    Iterator.range(0, n).foreach {
      i =>
        Character.codePointBefore(str, length)
    }
  }

  private def testStringCodePointBefore(str: String, n : Int, length: Int): Unit = {
    Iterator.range(0, n).foreach {
      i =>
        str.codePointBefore(length)
    }
  }

  private def toCodePoints(charSequence: CharSequence): Array[Int] = {
    if (charSequence == null) {
      throw new NullPointerException
    }

    val charArray: Array[Char] =
      {
        charSequence match {
          case str: String =>
            str
          case otherwise =>
            otherwise.toString
        }
      }.toCharArray
    val length: Int = charArray.length
    var surrogatePairCount: Int = 0
    var isSkipped: Boolean = false
    for (i <- 0 until length) {
      if (isSkipped) {
        isSkipped = false
      } else {
        if (0 < i && Character.isSurrogatePair(charArray(i - 1), charArray(i))) {
          surrogatePairCount += 1
          isSkipped = true
        }
      }
    }
    isSkipped = false
    val codePoints: Array[Int] = new Array[Int](length - surrogatePairCount)
    var j: Int = 0
    for (i <- 0 until length) {
      if (isSkipped) {
        isSkipped = false
      } else {
        val currentChar = charArray(i)
        if (Character.isHighSurrogate(currentChar) && i + 1 < length) {
          val nextChar = charArray(i + 1)
          if (Character.isLowSurrogate(nextChar)) {
            codePoints(j) = Character.toCodePoint(currentChar, nextChar)
            j += 1
            isSkipped = true
          }
        }
        if (!isSkipped) {
          codePoints(j) = currentChar
          j += 1
        }
      }
    }
    codePoints
  }
}


text:
package com.litmus.worldscope.utility;

import android.content.Context;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.litmus.worldscope.model.WorldScopeUser;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashSet;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.GsonConverterFactory;
import retrofit2.Retrofit;

/**
 * WorldScopeRestAPI creates instances of API calls through RetroFit and other libraries
 * Cookies are also intercepted and stored
 * Think of this class as the gun and WorldScopeAPIService as the bullets
 */
public class WorldScopeRestAPI {

    private static final String TAG = "WorldScopeRestAPI";

    private static final String cookiesSetTag = "PREF_COOKIES";

    private static final String cookiesHeaderTag = "cookie";

    private static final String setCookiesHeaderTag = "set-cookie";

    private Context context;

    private OkHttpClient okHttpClient;
    static Retrofit.Builder retrofitBuilder;

    public WorldScopeRestAPI(Context context) {
        this.context = context;

        // Set up interceptors for cookies with Context given
        okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new AddCookiesInterceptor())
            .addInterceptor(new SaveCookiesInterceptor())
            .build();
    }

    public WorldScopeAPIService.WorldScopeAPIInterface buildWorldScopeAPIService() {

        // Create an instance of the GsonBuilder to pass JSON results
        GsonBuilder  gsonBuilder = new GsonBuilder();
        gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);

        // TODO: Consider putting a switch here
        gsonBuilder.registerTypeAdapter(WorldScopeUser.class, new WorldScopeJsonDeserializer<WorldScopeUser>());

        // Create an instance of RetrofitBuilder to build the API for use
        Retrofit retrofit = getRetrofitBuilderInstance()
                .baseUrl(WorldScopeAPIService.WorldScopeURL)
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();

        Log.d(TAG, "Returning created service");
        return retrofit.create(WorldScopeAPIService.WorldScopeAPIInterface.class);
    }

    /**
     * Returns the instance of Retrofit.Builder required to build requests
     * @return Retrofit.Builder
     */
    private static Retrofit.Builder getRetrofitBuilderInstance() {
        if(retrofitBuilder == null) {
            return new Retrofit.Builder();
        } else {
          return retrofitBuilder;
        }
    }

    private static class WorldScopeJsonDeserializer<T> implements JsonDeserializer<T> {
        @Override
        public T deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException {
            JsonElement content = je.getAsJsonObject();

            return new Gson().fromJson(content, type);
        }
    }

    // AddCookiesInterceptor appends cookies to request
    public class AddCookiesInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {

            Log.d(TAG, "Request: " + chain.request().toString());
            Log.d(TAG, "Request URL: " + chain.request().url());
            Log.d(TAG, "Request Method: " + chain.request().method());

            HashSet<String> preferences = (HashSet<String>) PreferenceManager.getDefaultSharedPreferences(context)
                    .getStringSet(cookiesSetTag, new HashSet<String>());

            // If the request is login, clear previous cookies (cookies not required for login)
            if(chain.request().method().equals("POST") && chain.request().url().toString().equals(WorldScopeAPIService.WorldScopeURL + "/api/users/login")) {
                Log.d(TAG, "Login detected, clearing cookies");
                preferences.clear();

                // Clear cookies from WorldScopeAPIService
                WorldScopeAPIService.resetCookie();
            }

            Request.Builder builder = chain.request().newBuilder();
            for(String cookie: preferences) {
                builder.addHeader(cookiesHeaderTag, cookie);
                Log.d(TAG, "Header: " + cookiesHeaderTag + "=" + cookie);
            }

            return chain.proceed(builder.build());

        }
    }

    // SaveCookiesInterceptor saves cookies received
    public class SaveCookiesInterceptor implements Interceptor {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());

            Log.d(TAG, "Response: " + originalResponse.toString());

            if(!originalResponse.headers(setCookiesHeaderTag).isEmpty()) {
                HashSet<String> cookies = new HashSet<>();

                for(String header: originalResponse.headers(setCookiesHeaderTag)) {

                    // Remove the extra "; HttpOnly; Path=/" behind cookie
                    int cookieEndIndex = header.indexOf(";");
                    if(cookieEndIndex > 0) {
                        header = header.substring(0, cookieEndIndex);
                        cookies.add(header);

                        // Set the cookie in WorldScopeAPIService
                        WorldScopeAPIService.setCookie(header);

                        Log.d(TAG, "Cookies saved: " + header);
                    }
                }

                PreferenceManager.getDefaultSharedPreferences(context).edit()
                    .putStringSet(cookiesSetTag, cookies)
                    .apply();
            }
            return originalResponse;
        }

    }
}


text:
apt-get -y install redis-server
systemctl start redis-server
systemctl enable redis-server
echo "Configuration: /etc/redis/redis.conf"